Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
DIY Microcontroller Projects for Hobbyists

You're reading from  DIY Microcontroller Projects for Hobbyists

Product type Book
Published in Jul 2021
Publisher Packt
ISBN-13 9781800564138
Pages 320 pages
Edition 1st Edition
Languages
Authors (2):
Miguel Angel Garcia-Ruiz Miguel Angel Garcia-Ruiz
Profile icon Miguel Angel Garcia-Ruiz
Pedro Cesar Santana Mancilla Pedro Cesar Santana Mancilla
Profile icon Pedro Cesar Santana Mancilla
View More author details

Table of Contents (16) Chapters

Preface Chapter 1: Introduction to Microcontrollers and Microcontroller Boards Chapter 2: Software Setup and C Programming for Microcontroller Boards Chapter 3: Turning an LED On or Off Using a Push Button Chapter 4: Measuring the Amount of Light with a Photoresistor Chapter 5: Humidity and Temperature Measurement Chapter 6: Morse Code SOS Visual Alarm with a Bright LED Chapter 7: Creating a Clap Switch Chapter 8: Gas Sensor Chapter 9: IoT Temperature-Logging System Chapter 10: IoT Plant Pot Moisture Sensor Chapter 11: IoT Solar Energy (Voltage) Measurement Chapter 12: COVID-19 Digital Body Temperature Measurement (Thermometer) Chapter 13: COVID-19 Social-Distancing Alert Chapter 14: COVID-19 20-Second Hand Washing Timer Other Books You May Enjoy

Chapter 8: Gas Sensor

An indoor environment with good air quality is essential to guarantee a healthy environment (Marques and Pitarma, 2017). The MQ-2 gas sensor can be an excellent way to measure the quality parameters of indoor air or as an early fire detection system. In this chapter, you will learn how to build a practical system for detecting gases in the environment (which we will call a gas sensor) and connect the MQ-2 gas sensor to a Blue Pill microcontroller card.

The following main topics will be covered in this chapter:

  • Introducing the MQ-2 gas sensor
  • Connecting a gas sensor to the STM32 microcontroller board
  • Writing a program to read the gas concentration over the sensor board
  • Testing the system

At the end of this chapter, you will know about the operation of an MQ-2 gas sensor, and you will be able to connect it correctly to the STM32 microcontroller card and view the data obtained from the sensor. You will be able to apply what you have...

Technical requirements

The hardware components that will be needed to develop the gas sensor are as follows:

  • One solderless breadboard
  • One Blue Pill board
  • ST-Link/V2
  • One MQ-2 breakout module
  • Seven male-to-male jumper wires
  • One LED 8x8 matrix
  • One 7219 breakout board
  • A 5 V power source

These components are widespread, and there will be no problems in getting them easily. On the software side, you will require the Arduino IDE and the GitHub repository for this chapter: https://github.com/PacktPublishing/DIY-Microcontroller-Projects-for-Hobbyists/tree/master/Chapter08

The Code in Action video for this chapter can be found here: https://bit.ly/2UpGDGs

Let's first start by describing the characteristics of the MQ-2 gas sensor.

Introducing the MQ-2 gas sensor

In this section, we will get to know the details of the main hardware component to build our gas sensor: the MQ-2 sensor. This sensor is recommended to detect LPG, propane, alcohol, and smoke, mainly with concentrations between 300 and 10,000 parts per million (ppm). So, we can say that it is a sensor to detect smoke and flammable gases.

Concentration refers to the amount of gas in the air and is measured in ppm. That is, if you have 2,000 ppm of LPG, it means that in a million gas molecules, only 2,000 ppm would be LPG and 998,000 ppm other gases.

The MQ-2 gas sensor is an electrochemical sensor that varies its resistance when exposed to certain gasses. It includes a small heater to raise the circuit's internal temperature, which provides the necessary conditions for the detection of substances. With the 5 V connection on the pins, the sensor is kept warm enough to function correctly.

Important note

The sensor can get very hot, so it...

Connecting a gas sensor to the STM32 microcontroller board

In this section, we will build a gas sensor device utilizing the STM32 Blue Pill microcontroller board and a gas sensor module using the hardware components listed in the Technical requirements section. The gas sensor breakout board connects to the STM32 Blue Pill with four pins:

  • Analog output (AO): This pin generates an analog signal and must be connected to an analog input of the microcontroller.
  • DO: This pin generates a digital signal and must be connected to a digital input of the microcontroller.
  • VCC: Pin to supply power to the sensor (5 V).
  • GND: Ground connection.

For this project, you will learn how to interface the MQ-2 module with the STM32 board to acquire data in a digital and analog way. Let's start with the digital option.  

Interfacing for digital reading

Now we are going to connect the electronic components to the breadboard, do the wiring, and finally connect everything...

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:

  1. 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 to true. This variable will be used for storing the sensor data.

  2. 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)...

Testing the system

In this last section of the chapter, we will connect an 8x8 LED matrix to display an alert if the sensor detects the presence of gas in the environment.

An LED matrix is a set of LEDs grouped into rows and columns. By turning on these LEDs, you can create graphics or text, which are widely used for billboards and traffic signs.

There is an electronic component for small-scale projects called an 8x8 LED matrix. It is composed of 64 LEDs arranged in eight rows and eight columns (see Figure 8.14):

Figure 8.14 – LED matrix 8x8

Figure 8.14 – LED matrix 8x8

As you can see in the previous figure, the 8x8 LED matrix has pins to control the rows and columns, so it is impossible to control each LED independently.

This limitation implies having to use 16 digital signals and refreshes the image or text continuously. Therefore, the integrated MAX7219 and MAX7221 circuits have been created to facilitate this task; the circuits are almost identical and interchangeable...

Summary

We had so much to learn in this chapter! First, we learned how to connect the MQ-2 gas sensor to the STM32 Blue Pill microcontroller board, both digitally and with an AO reading. We then wrote two pieces of code to read digital and analog sensor values. Last, we tested the device to understand its operation, displaying the sensor data in the serial console.

This project gave us the skills to read different kinds of sensor data to use this knowledge according to our needs. For instance, you can display some sensors in a room to monitor the environment in real-time.

In the next chapter, we will enter the fascinating world of the so-called Internet of Things. With the knowledge that we will acquire, we will create projects that connect to the internet and access our information remotely.

Further reading

Marques G. & Pitarma R. (2017). Monitoring Health Factors in Indoor Living Environments Using Internet of Things. In: Rocha Á., Correia A., Adeli H., Reis L., & Costanzo S. (eds) Recent Advances in Information Systems and Technologies. WorldCIST 2017. Advances in Intelligent Systems and Computing, vol. 570. Springer, Cham. https://doi.org/10.1007/978-3-319-56538-5_79

lock icon The rest of the chapter is locked
You have been reading a chapter from
DIY Microcontroller Projects for Hobbyists
Published in: Jul 2021 Publisher: Packt ISBN-13: 9781800564138
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}