Exercise: Introduction
In this article, let’s do the exercise for I2C master (STM) and slave (Arduino) communication. When the button on the master is pressed, the master should send data to the Arduino slave connected. The data received by the Arduino will be displayed on the Arduino serial port.
- Use I2C SCL=100KHz (Standard mode)
- Use internal pull-up resistors for SDA and SCL lines. No need to use external pull-up resistors.
Things needed for the exercise:
- Arduino board
- ST board
- Some jumper wires
- Breadboard
- 2 pull-up resistors of value 4.7kΩ (only if your pin doesn’t support internal pull-up resistors).
Connections:
Connections for this exercise are shown in Figure 1.
- ST board and Arduino board are connected over I2C pins.
- The data will be sent from the ST board to the Arduino, and this sketch will then forward the message to the serial monitor, which displays data sent by the master.
Step 1: Connect Arduino and ST board SPI pins, as shown in Figure 2.
- Here the I2C1 peripheral of the STM32 microcontroller is used. SCL and SDA are PB6 and PB9, respectively. You can use any pins according to your board, but if you are using STM32F4 discovery, then surely you can use PB6 and PB9.
- Connect the ground point of the discovery to the ground point of the Arduino board.
- If you are using a logic analyzer, connect channel 0 to the SCL and channel 1 to SDA to probe the SCL and SDA line, respectively, and connect the ground channel of the logic analyzer to the ground.
Step 2: Power the Arduino board and download the I2C slave sketch to Arduino. The name of the sketch is 001I2CSlaveRxString.ino. This sketch must be downloaded into the Arduino board to convert it into a slave.
Refer to the datasheet to find out the GPIO pins over which I2C1 can communicate. Observe Figure 3, PB7 can act as an I2C1_SDA line if you keep this pin in alternate function mode 4, and the PB9 can also act as I2C1_SDA. The pin PB6 and PB8 can act as I2C1_SCL line when it is in alternate function mode 4.
While using I2C2, PB10 and PF1 are used as I2C2_SCL.
In the following article, let’s do the coding for this exercise.
FastBit Embedded Brain Academy Courses,
Click here: https://fastbitlab.com/course1