DEV Community

Cover image for IIC
QuecPython
QuecPython

Posted on

IIC

Briefing on IIC

IIC (Inter-Integrated Circuit) bus is a two-wire serial bus developed by PHILIPS. It’s used to connect micro-controller and its peripherals. IIC and I2C are the same thing, it’s just the name difference. The serial bus, composed by Serial Data (SDA) wire and Serial Clock (SCL) wire, is able to send and receive data. An IIC device can either be a master or a slave. Normally, a master would be paired with one or more slaves and responsible for starting and ending a communication session. At the time being, QuecPython cellular modules can be a master only.

IIC Topology:

Data (SDA): To transmit data. Clock (SCL): To sync the data transmission.

• Every device on the bus owns a unique address. As a result, the communication between micro-controller and devices can be implemented in accordance with the timing of the corresponding address.

• As bidirectional lines, both SDA and SCL are connected to a positive supply voltage via a current-source or pull-up resistor. When the bus is idle, both lines are in high level.

• The transmission rate over the bus is up to 100 kbit/s in the Standard-mode, up to 400 kbit/s in the Fast-mode, and up to 3.4 Mbit/s in the High-speed mode.

IIC Communication Timing Introduction

Idle Level: SCL and SDA both are in high level.

START Condition: A HIGH to LOW transition on the SDA line while SCL is HIGH (IDLE).

STOP Condition: A LOW to HIGH transition on the SDA line while SCL is HIGH (IDLE).

Acknowledge Condition: The acknowledge signal sent by the receiving end after it received valid data. After every byte (8 bits) of data, at the ninth clock, the transmitting end releases SDA line to receive acknowledge signal from the receiving end.

If SDA is low, the Acknowledge (ACK) is valid and the transmission is successful. If SDA is high, it will be a Not Acknowledge (NACK) and indicates the transmission has failed.

Data Transmission: Data bits are allowed to change while SCL is LOW. After each 8 bits of data is tramsmitted, the slave would pull down the SDA (ACK) or pull up the SDA (NACK).

Software Simulated IIC

Software Simulated IIC simulates IIC protocol by controlling GPIO pin levels in software. If the hardware IIC are not enough or are multiplexed as other functions, it is available to use simulated IIC to implement IIC communication.

IIC Function Illustration

This chapter will introduce how we can use IIC driver’s functionalities and data types to build communication between QuecPython series modules and other IIC devices. The typical programming workflow consists of following parts:

  1. Create an object

  2. Send data

  3. Receive data

For specific introduction on API, please refer to machine.IIC in detail.

Create an Object

It is mandatory to specify IIC channel and working mode when creating an IIC object.

class machine.I2C(I2Cn, MODE)

For the parameters introduction and pin relationships, please refer to machine.IIC

When creating an object, please note following aspects.

• In a project, only one object can be created for one channel. When communicating with multiple IIC peripherals via one channel of IIC, it is only necessary to create one IIC object in one of the peripheral drivers.

• In standard mode, the IIC communication baud-rate is 100k. While In fast mode, it is 400k instead. Note that the baud-rate shall never exceed the maximum supported baudrate of IIC peripheral devices.

end Data

I2C.write(slaveaddress, addr, addr_len, data, datalen)

When sending data over IIC, a couple of points should be noted:

• The slaveaddress is a 7-bit address. When the master sends the slave address, the address will be shifted 1 bit to the left. After that, when writing data to the slave, the Least Significant Bit (LSB) will be a zero supplemented. When reading data from the slave, the LSB will be a one supplemented instead. For example, an IIC peripheral device has an address of 0x23, the master should send 0x46 if it want to send data to the slave, otherwise 0x47 shall be sent to read data from the slave.

• The addr_len refers to the address length of the register. You can get this information from the peripheral’s datasheet. Commonly used IIC peripherals usually have an register address length of 1 byte such as QMA7981 3-Axis Accelerometer.

Receive Data

I2C.read(slaveaddress, addr, addr_len, r_data, datalen, delay)

When receiving data over IIC, a couple of points should be noted:

  1. Buffer management: It is vital to manage buffer correctly. If data is not read before being overwritten by new data, there might be a data loss. Therefore, ensure data are read and processed in time before it’s overwritten.

  2. The delay parameter: After receiving commands from the slave, some IIC peripherals may need some delay before returning data correctly. So as a master, a delay is needed between sending command and reading data. See next figure.

Application Examples

AHT10 Temperature amp; Humidity Sensor

The AHT10 sensor is equipped with a newly designed ASIC, an improved MEMS semiconductor capacitive humidity sensing element and a standard on-chip temperature sensing element. It can be used in a variety of application such as: HVAC, Dehumidifiers, Test and Inspection Equipment, Consumer Products, Automotive, Automation, Data Loggers, Weather Stations, Appliances, Humidity Conditioning, Medical, and other related temperature and humidity detection and control.

AHT10 works by:

• Powering up the sensor. The sensor will take up to 20ms (SCL will be at HIGH) to get into idle status after being powered, then the sensor is ready to take orders from the master (Cellular Module).

• The master must communicate with AHT10 via IIC and the slave address of AHT10 is 0x38. After the master issues the initialization command (‘11100001’=Initialization, ‘10101100’=Measurement), it must wait for the measurement till the end. Basic commands are listed in the below table.

Below is a table regarding the return value of the sensor.

Issue the collect data command (0xAC) to trigger AHT10 to collect temp & humidity data. Sleep for at least 75ms to wait for the sensor and read collected data from AHT10 afterwards.

After acquiring the data form AHT10, a conversion should be made on the raw data to get desired information.

Relative Humidity (RH) Can be calculated from the relative humidity signal SRH from SDA using the formula below

Temperature (T)T can be calculated from the temperature signal ST using the formula below

FAQ#
IIC is a very simple and straightforward communication protocol. There isn’t much issue when it comes to practical use.

  1. Fail to receive or send data

This might be caused by something like wrong baud-rate and wrong HW connection. You may check as below.

• Check if module IIC baud rate is above the peripheral limit.

• Check hardware connection. Make sure SDA/SCL and GND lines are connected properly.

  1. It is normal to transmit/receive, however, the data are wrong

It could be:

• Voltage level of SDA/SCL in master are different from that in slave. When the master uses 3.3V while the slave uses 1.8V, a level of 1.8V is considered HIGH by peripheral but controller would think a level higher than 1.65V is considered HIGH, resulting in confusion probably.

• The pull-up resistors in IIC circuit are high enough to hinder its ability to pull up. This will stretch the rising edge as well as the falling edge, leding to error in master.

Top comments (0)