STM32 I2C Lecture 25- Exercise: Introduction

  • Post author:
  • Post category:Blog

 

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.

  1. Use I2C SCL=100KHz (Standard mode)
  2. Use internal pull-up resistors for SDA and SCL lines. No need to use external pull-up resistors.

Things needed for the exercise:

  1. Arduino board
  2. ST board
  3. Some jumper wires
  4. Breadboard
  5. 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.

 

Exercise: I2C Communication Between STM32 Master and Arduino Slave
Figure 1. Connections

 

Step 1: Connect Arduino and ST board SPI pins, as shown in Figure 2.

 

Exercise: I2C Communication Between STM32 Master and Arduino Slave
Figure 2. Connections for the pins of ST board and Arduino board.

 

  • 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.

 

I2C communication exercise
Figure 3. Table for alternate function mapping.

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

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.