Microcontrollers like the ESP8266 and ESP32 offer support for a wide variety of peripheral interfaces with one of the most popular being Inter-Integrated Circuit (I²C or I2C). I2C uses two wires to communicate between the bus master and a slave: a SDA bi-directional serial data wire, and SCL clock line. The bus supports multiple masters and up to 117 slaves using the 7 bit address scheme or up to 1008 slave devices with the rarely used 10 bit address extension.
I2C is a popular interface for slower devices and sensors as it is cheap and requires minimum wiring for the device (typically VCC, GND, SDA, SCL).
With the abundance of microcontrollers available is can sometimes be hard to identify the controller I2C pins. They may be clearly labeled on the board or the manufacturer may have clear board pinout diagrams. A simple way to verify the microcontroller default hardware I2C pin assignments is with the following Arduino sketch snippet:
Serial.begin(115200);
Serial.println(SDA);
Serial.println(SCL);
For the ESP8266 this typically returns SDA as pin 4 and SCL as pin 5. For the ESP32 typically returning SDA as pin 21 and SCL as pin 22.
At the physical layer, SDA and SCL are open drain design thus a pull-up resistors (typically 4k7Ω) is required for these lines. These pull-up resistor make be integrated on the slave device but some of the cheaper devices do not include them. Also some pins on the microcontroller may include an internal pull-up resistor. This is something worth checking if your circuit is not working.
Each slave device on the I2C bus requires an unique address. This is typically a value in the range 0x00 to 0x7F, with certain values reserved, giving up to 117 unique addresses on a standard bus. Interestingly the 8 bit is used to indicate the data direction (ie master is writing to the slave address or reading from the the device). Board manufactures typically provided details of the board address in the documentation or in the datasheet for the board sensor/IC.
If in doubt about a slave address, a simple I2C scanner script can scan the bus and return the addresses of all connected slaves. An Arduino sample sketch is available here and a MicroPython version here.
If there is an address conflict some devices provide jumpers to change the slave address. Others provide a software reprogrammable address. If the device has a fixed address an I2C multiplexer like the TCA9548A can be used to connect up to 8 slave devices to a microcontroller.
An occasional issues I have encountered controlling some slave devices from an ESP32 is a problem reading a peripheral with the default I2C bus frequency of 400kHz. I solved this issue by lower the bus frequency to 50kHz when initialising the I2C bus in my Arduino or MicroPython scripts.
The final problem I have encounter is with connecting 3.3V microcontroller to a 5V slave device. A bidirectional logic level converter may help with this issue.
The final problem I have encounter is with connecting 3.3V microcontroller to a 5V slave device. A bidirectional logic level converter may help with this issue.
No comments:
Post a Comment