Menu Close

Digispark to Arduino Uno I²C Communications

I²C (I Squared C), also referred as I2C and IIC (Inter-Integrated Circuit) is a serial bus communication protocol. This is widely used in making ICs communicate with each other on the same circuit board, or from board to board in multi-board systems.

The communication happens over two lines, one for data SDA (Serial Data) and one for clock SCL (Serial Clock). SCL is sometimes written as SCK on some boards, like the Digispark I have. SDA and SCL remained pulled high when idle, transmission is achieved by ICs sinking the current, pulling the lines low, which can be detected by other ICs on the bus.

Digispark is a 3.3v device, while the Uno works on 5v, making them incompatible to communicated directly. Even though people often connect them directly, and I haven’t come across any reports of the a Digispark being damaged by doing so, it is still not a good practice. The risks are either getting the lower voltage device being damaged by higher voltage, or the voltage not being able to pull high enough to register is high on the higher voltage device.

Good practice would be to translate the voltage levels, which can be achieved in several ways as described here. I’ve used a level shifter module as shown in the diagram below.

In the above diagram, the Uno will act as a master device and the Digispark as a slave device. Digispark is on the 3.3v side of the I2C bus, and Uno on 5V side. The Digispark is also controlling the LEDs on the breadboard.

I have this slightly different level shifting module than the one I have shown in the fritzing diagram. This one doesn’t seem to be selling anymore.

Following programs demonstrate how I2C can be used in this configuration.

For I2C implementation on Digispark, you will need the TinyWire library, which you can download from here. You can download the zip file and unzip it in the libraries folder.

Testing Digispark

The first program I wrote was to test that all four LEDs are working with the Digispark.

Like me, you may find that the LED on Pin5 is not blinking. What this normally means is that the Digispark is a Chinese clone. On the Chinese clones, RSTDISBL fuse is unprogrammed. This can be programmed as described here to enable digital I/O on Pin5.

I²C Communication

In the following code for the Digispark, a message received on the I²C bus raises an interrupt which calls the processCommand() function attached to it. In the processCommand() function, if the received character happens to be a number between 1 and 4, then the corresponding LED is turned on and current time is noted.

In the loop(), turned on LEDs are turned off after half a second.

There is a rare possibility that the processCommand() is invoked close to the last half a second before the millis() overflows (in about 50 days from power-on) and starts from zero again, in which case that LED can remain on for a very long time. For an automated system that is expected to remain on for longer than 50 days, such issues should be fixed. For this particular problem, it can be fixed as below:

Preventing millis() overflow issue

The solution is simple, as described here. Modified code is given below:

Arduino Uno code

This Uno program listens to the serial port (RS232), and forwards any character received to the Digisark, which we have programmed above.

Usage

Open the appropriate COM port monitor in Arduino IDE and select 9600 baud rate. Sending any number from 1 to 4 will turn the corresponding LED on for about half a second. Timing for LEDs is independent of each other. If you send ‘1234’, it will turn on all 4 LEDs for half a second each. Sending ’31’ will turn on the first and third LEDs for half a second.

 1,141 total views,  4 views today

Leave a Reply

Your email address will not be published. Required fields are marked *