BS2p icon BS2pe icon BS2px icon

I2CIN / I2COUT Example




Syntax: I2COUT Pin, SlaveID, {Address {\LowAddress},} [OutputData]


Send data to a device using the I2C® protocol.

Quick Facts

  BS2p, BS2pe, and BS2px
Value of Pin 0 8
I/O Pin Arrangement 0: Serial Data (SDA) pin

1: Serial Clock (SCL) pin
8: Serial Data (SDA) pin

9: Serial Clock (SCL) pin
Transmission Rate Approximately 81 kBits/sec (BS2p), 45 kBits/sec (BS2pe), 83 kBits/sec (BS2px) -- not including overhead.
Special Notes Both the SDA and SCL pins must have 4.7 kΩ pull-up resistors.
Related Commands



The I2C protocol is a form of synchronous serial communication developed by Philips Semiconductor. It only requires two I/O pins and both pins can be shared between multiple I2C devices. The I2COUT command allows the BASIC Stamp to send data to an I2C device.

The following is an example of the I2COUT command:

  I2COUT 0, $A0, 5, [100]

This code will transmit a "write" command to an I2C device (connected to I/O pins 0 and 1), followed by an address of 5 and finally will transmit the number 100.

The example above will write a byte of data to location 5 of a 24LC16B EEPROM from Microchip. The figure below shows the proper wiring for this example to work. The SlaveID argument ($A0) is both the ID of the chip and the command to write to the chip; the 0 means write. The Address argument (5) is the EEPROM location to write to.

Note: The 4.7 kΩ resisters are required for the I2COUT command to function properly.

The I2COUT command's OutputData argument is similar to the DEBUG and SEROUT command's OutputData argument. This means data can be sent as literal text, ASCII character values, repetitive values, decimal, hexadecimal and binary translations and string data as in the examples below. (Assume the 24LC16B EEPROM is being used).

value   VAR     Byte

  value = 65
  I2COUT 0, $A0, 0, [value]             ' send ASCII character "A"
  I2COUT 0, $A0, 0, [REP value\5]       ' send "A" five times, i.e., "AAAAA"
  I2COUT 0, $A0, 0, [DEC value]         ' send "6" and "5"
  I2COUT 0, $A0, 0, [HEX value]         ' send "4" and "1"
  I2COUT 0, $A0, 0, [BIN value]         ' send "1000001"

The tables below list all the available conversion formatters and special formatters available to the I2COUT command. See the DEBUG and SEROUT commands for additional information and examples of their use.

Conversion Formatter Type of Number Notes
DEC{1..5} Decimal, optionally fixed to 1 - 5 digits 1
SDEC{1..5} Signed decimal, optionally fixed to 1 - 5 digits 1,2
HEX{1..4} Hexadecimal, optionally fixed to 1 - 4 digits 1
SHEX{1..4} Signed hexadecimal, optionally fixed to 1 - 4 digits 1,2
IHEX{1..4} Indicated hexadecimal, optionally fixed to 1 - 4 digits 1
ISHEX{1..4} Signed, indicated hexadecimal, optionally fixed to 1 - 4 digits ($ prefix) 1,2
BIN{1..16} Binary, optionally fixed to 1 - 16 digits 1
SBIN{1..16} Signed binary, optionally fixed to 1 - 16 digits 1,2
IBIN{1..16} Indicated binary, optionally fixed to 1 - 16 digits 1
ISBIN{1..16} Signed, indicated binary, optionally fixed to 1 - 16 digits 1,2
  1. Fixed-digit formatters like DEC4 will pad the number with leading 0s if necessary; ex: DEC4 65 sends 0065. If a number is larger than the specified number of digits, the leading digits will be dropped; ex: DEC4 56422 sends 6422.
  2. Signed modifiers work under two's complement rules.


Special Formatter Action
? Displays "symbol = x' + carriage return; where x is a number. Default format is decimal, but may be combined with conversion formatters (ex: BIN ? x to display "x = binary_number").
ASC ? Displays "symbol = 'x'" + carriage return; where x is an ASCII character.
STR ByteArray {\L} Send character string from an array. The optional \L argument can be used to limit the output to L characters, otherwise, characters will be sent up to the first byte equal to 0 or the end of RAM space is reached.
REP Byte\L Send a string consisting of Byte repeated L times (ex: REP "X"\10 sends "XXXXXXXXXX").


The I2C protocol has a well-defined standard for the information passed at the start of each transmission. First of all, any information sent must be transmitted in units of one byte (8-bits). The first byte, we call the SlaveID, is an 8-bit pattern whose upper 7-bits contain the unique ID of the device you wish to communicate with. The lowest bit indicates whether this is a write operation (0) or a read operation (1). Here is the SlaveID format:

7 6 5 4 3 2 1 0
A6 A5 A4 A3 A2 A1 A0 R/W


The second byte, immediately following the SlaveID, is the Address. It indicates the 8-bit address (within the device) where the OutputData is to be written (the first location if more than one byte is written). Note that the Address argument is optional and may be left unspecified for devices that don't require an address parameter.

Some devices require more than eights bits of address. For this case, the optional LowAddress argument can be used for the low-byte of the required address. When using the LowAddress argument, the Address argument is effectively the high-byte of the address value. For example, if the entire address value is 2050, use 8 for the Address argument and 2 for the LowAddress argument (8 * 256 + 2 = 2050).

Following the last address byte is the first byte of data. This data byte may be transmitted or received by the BASIC Stamp. In the case of the I2COUT command, this data byte is transmitted by the BASIC Stamp and received by the device. Additionally, multiple data bytes can follow the address, depending on the I2C device. Note that every device has different limitations regarding how may contiguous bytes they can receive or transmit in one session. Be aware of these device limitations and program accordingly.

Every I2C transmission session begins with a Start Condition and ends with a Stop Condition. Additionally, immediately after every byte is transmitted, an extra clock cycle is used to send or receive an acknowledgment signal (ACK). All of these operations are automatically taken care of by the I2CIN command so that you need not be concerned with them. The general I2C transmission format is shown below.


Since the I2COUT command is intended for output only, it actually overrides the "R/W" bit (bit 0) in the SlaveID argument. This is done to avoid device conflicts should the value be mistyped. Put simply, this means commands such as:

  I2COUT 0, $A0, 10, [0] 


  I2COUT 0, $A1, 10, [0]

...both transmit the same thing ($A0, then 10, then the data). Even though the I2COUT command really doesn't care what the value of the SlaveID's LSB is, it is suggested that you still set it appropriately for clarity.

Also note that the I2COUT command does not support multiple I2C masters and the BASIC Stamp cannot operate as an I2C slave device.

Go to PBASIC Language home page

Open Getting Started with Stamps in Class

Open Connection Troubleshooting

BASIC Stamp Help Version 2.5.4

Copyright © Parallax Inc.