Writing a program to read the gas concentration over the sensor board
In this section, we will learn how to code a program to read data from our gas sensor and show it on the serial monitor if gas is present in the environment.
As in the previous section, we'll first learn how to read data digitally and also in analog form.
Coding for digital reading
Let's start writing the code:
- Define which pin of the STM32 Blue Pill microcontroller will be used as input for reading the data from the sensor. Here's the code that shows how to do that:
const int sensorPin = PB12; boolean sensorValue = true;
The selected pin was
PB12
(labeled B12 on the Blue Pill board). A Boolean variable was declared and initialized totrue
. This variable will be used for storing the sensor data. - Next, in the
setup()
part, we need to start the serial data transmission and assign the speed of the transfer (9600
bps as a standard value):void setup() { Â Â Serial.begin(9600)...