Similarly, when a button is pressed in Arduino, an LED in STM32 will light up.The LAN9303 has some peculiarities when accessing its registers. When a button is pressed in the master i.e. The STM32 is acting as master in the I2C Communication while Arduino UNO is used as a slave device. Int buttonValue = digitalRead(buttonpin) Ī simple project for demonstrating I2C in STM32F103C8T6 MCU based STM32 Blue Pill Board. Similarly, if a write operation is initiated by the master, the Wire.onReceive function is invoked which in turn will call the receiveFun. Hence, if the master performs a read operation, then Wire.onRequest function is called and this in turn calls the specified requestFun.
#2 wire library for stm code#
slave address mentioned in Slave Code and Master Code must match.Īnother important thing to remember is a Slave in I2C Communication cannot initiate the transfer and it can only comply with read or write requests from the master. This address must be same as the one defined in the master code (or vice-versa) i.e. Since, Arduino is the slave, we must begin the I2C Communication by using the slave address. First, define the LED Pin as Digital IO pin 13 and Button Pin as Digital IO pin 6 and initialize them as OUTPUT and INPUT respectively. Now coming to Arduino, it must be configured as Slave device. } Programming Arduino for I2C Communication Int buttonValue = digitalRead(BUTTONPIN) one for reading the button pin and sending corresponding data and the other part for reading the incoming data and turning the LED ON or OFF (based on the received data). In the loop function, divide the operation into two parts i.e. LED Pin as OUTPUT and Button Pin as INPUT and begin the I2C Communication. Also, define the Slave Address of Arduino as 0x8. Define the LED pin and Button Pin as PC13 and PA0. Programming STM32 for I2C Communicationįirst, let us start with the I2C Master device i.e. Make sure that GND of both Arduino and STM32 is made common. We can use the on-board LEDs of both the boards, which are connected to PC13 in STM32 and Digital IO pin 13 in Arduino. Connect the other end of the push button to 5V. connect a push button to Digital IO pin 6 and pull this pin to GND using a 10KΩ resistor. Connect the other end of the push button to 3.3V. Now, connect a push button to PA0 pin of STM32 and also pull this pin to GND using a 10KΩ resistor. Pull the SDA and SCL pins HIGH using two 4.7KΩ resistors. PB6 to corresponding SCL pin of Arduino i.e. Similarly, connect the SCL pin of STM32 i.e. PB7 with corresponding SDA pin of Arduino i.e. The connections for demonstrating the use of I2C in STM32F103C8T6 is very simple. The following image shows the circuit diagram of using I2C in STM32F103C8T6 to communicate with Arduino over I2C Bus.
#2 wire library for stm serial#
I2C, which is short for Inter Integrated Communication, is developed by Phillips as a simple two wire serial communication protocol for data transfer between Microcontroller and some low speed peripherals like Memory, Timers, Sensors, IO, ADC, DAC etc. Let us quickly recall some critical information about I2C Protocol. We have already seen several I2C Communication related projects implemented with Arduino. Programming Arduino for I2C Communication.Programming STM32 for I2C Communication.