I2C Slave…Coming soon!


In our previous blog article we discussed I2C and SPI busses; their practical applications, limitations and typical usages. In this article we will discuss some new Flowcode development work we are currently in the process of undertaking which will enable users to benefit from I2C slave mode.

On an I2C bus, there are two types of devices; Master and Slave. There is typically only one master device in a system, and multiple slave devices. The master device controls the clock line of the I2C bus. A slave device can never initiate a transfer over the bus, but instead the master device controls that. Since we have had I2C master devices in Flowcode for a while now we will not cover them in this article. Instead, we will discuss the new developments regarding the slave component.

Slave devices come in two varieties, those embedded within dedicated hardware and those slave devices that are controlled by a microcontroller. Devices with dedicated hardware perform one operation, and sit on the I2C bus until data is requested from the master. This may be a sensor in a system for example, which measures the speed of a rotating gear. The master device determines when the data is required and the slave listens and waits. Slave devices on dedicated hardware are always ready to send data whenever the master device requests it.

Using a microcontroller in the slave device allows more flexibility. For example, the microcontroller can perform a range of other tasks, of which sending I2C data is only one. However, such flexibility comes at the expense of being slightly more complicated. If a microcontroller is used, it may be in the middle of processing other data when an I2C command is received from the master. If this occurs, the I2C protocol needs a method of holding the master device in a state of limbo until the slave has finished its current task and is ready to receive the command. This will ensure no data is lost. This process is referred to as Clock Stretching. During this time the slave module will hold the clock signal, alerting the master that it is not ready to process data. When the slave is ready it will release the clock signal and master to slave communications can begin. The new Flowcode slave component features clock stretching to ensure that commands from the master component are not missed by the slave.

The new Slave component within Flowcode features 4 new Macro calls. Two calls are used to initialise and un-initialise the module, allowing communications to be sent. These are particularly important when the slave device is controlled by a microcontroller as the device can be used for many other functions (more on this later), and keeping the slave module enabled can slow-down other processes which the microcontroller may need to execute.

The other two Macro calls available are RxByte and TxByte. As stated earlier, these commands can only be executed upon request from the master device and are responsible for transmitting and receiving data when the master requests.

We created a sample program to demonstrate the I2C master and slave in action. Both the master and slave in this example use our EB006 programmer board, coupled to a PIC16f1937 device. The master device reads the status of the switches connected on PORTD of the microcontroller. If no switches are pressed the program sits in a state of waiting. If the user presses a switch on PORTD, the value of the switch pressed is sent over I2C to the slave device and presented on PORTB via a bank of LEDs. This demonstrates the sending of data from the master to the slave.

The master device in the example program also has a read request function. Each time an I2C command is sent to the slave device a counter is incremented on the slave device, again showing the usefulness of using a microcontroller to power a slave device. After sending a write command, the master device is set to issue a read command from the slave, where the value of the counter is sent from slave to master, and presented on a bank of LEDs connected to the master.


The slave device in this example sits and waits for commands from the master. Within an infinite loop the slave device has a decision statement. If a write command is issued from master to slave, it will receive the data, present it on the LEDs as previously stated, and increment a simple counter. If a read command is issued from master to slave, the device will return the count variable to the master device.


In the following image you will see our two I2C devices connected via a pair of wires for SDA and SCL. The prototype board is used to host the pull-up resistors required to hold both lines at 5V. A small video is also attached at the end of the article to demonstrate the example project in action.

I2C for blog

This example demonstrates a simple master-slave application where the slave device is operated from a microcontroller. Most new microcontrollers now feature dedicated I2C bus connections, which allow for simple creation of I2C master and slave devices, and with Flowcode soon to support I2C slave functionality you’ll be able to create your own microcontroller based I2C slave devices.

I2C slave functionality within Flowcode is coming soon!

7,439 total views, 4 views today

One Comment

Leave a Reply