Reading data from a voltage sensor module
It is time to learn how to code a program that will read the information from the voltage sensor and display its reading on the serial monitor.
Let's write the program to receive the sensor data from the STM32 Blue Pill:
- Declare which pin of the STM32 Blue Pill card will be used as input of the sensor data:
const int sensorPin = 0;
The input pin will be the
0
(labeled A0 on the Blue Pill). - Next, in the
setup()
part, start the serial data transmission and assign the speed of the transfer to 9600 bps, and indicate to the microcontroller the type of pin assigned to A0:void setup() { Serial.begin(9600); pinMode(sensorPin, INPUT); }
- Now, in
loop()
, first read the input pin's data sensor, send its value to the serial port, and wait for a second:void loop() { int sensorValue = analogRead(sensorPin); Serial.print("Voltage: "); Serial.println(sensorValue...