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 13: COVID-19 Social-Distancing Alert

When the world celebrated the arrival of the year 2020, a pandemic was arising due to a new disease: COVID-19. With the emergence of this pandemic, all human activities were affected to a lesser or greater degree.

The education sector has been one of the most affected in this sense. All schools worldwide temporarily suspended their activities, since the risk of contagion in these environments can be very high. After a few months of lockdowns, schools around the world gradually began to resume face-to-face activities, following rigorous standards of disinfection and protocols to ensure physical distancing between students and school staff (Global Education Cluster, 2020).

The recommendation of the World Health Organization (WHO) for physical distancing is to remain at least 1 meter (m) (3 feet (ft)) apart between people, with 2 m (6 ft) being the most general recommendation to minimize the risk of contagion in children (KidsHealth,...

Technical requirements

The hardware components that will be needed to develop the social-distancing alarm are listed here:

  • One solderless breadboard.
  • One Blue Pill microcontroller board.
  • One ST-LINK/V2 electronic interface is needed for uploading the compiled code to the Blue Pill board. Bear in mind that the ST-LINK/V2 interface requires four female-to-female jumper wires.
  • One HC-SR04 ultrasonic sensor.
  • One buzzer.
  • Male-to-male jumper wires.
  • Female-to-male jumper wires.
  • A power source.
  • Cardboard for the case.

As usual, you will require the Arduino integrated development environment (IDE) and the GitHub repository for this chapter, which can be found at https://github.com/PacktPublishing/DIY-Microcontroller-Projects-for-Hobbyists/tree/master/Chapter13

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

Let's begin!

Programming a piezoelectric buzzer

In this section, you will learn what a buzzer is, how to interface it with the STM32 Blue Pill, and how to write a program to build an alert sound.

A piezoelectric buzzer is a device that generates tones and beeps. It uses a piezoelectric effect, which consists of piezoelectric materials converting mechanical stress into electricity and electricity into mechanical vibrations. Piezoelectric buzzers contain a crystal with these characteristics, which changes shape when voltage is applied to it.

As has been common in these chapters, you can find a generic breakout module that is pretty straightforward to use, as shown in the following screenshot:

Figure 13.2 – Piezoelectric buzzer breakout board

Figure 13.2 – Piezoelectric buzzer breakout board

This breakout board connects to the STM32 Blue Pill microcontroller board with three pins, outlined as follows:

  • Input/Output (I/O): This pin must be connected to a digital output of the microcontroller.
  • ...

Connecting an ultrasonic sensor to the microcontroller board

Before moving ahead, we need to learn about the functionality of the HC-SR04 ultrasonic sensor, how to interface it with the STM32 Blue Pill, and how to write a program to measure the distance between the sensor and another object.

This sensor emits an ultrasonic wave. When this wave collides with an object, the wave is reflected and received by the sensor. When the reflected signal is received, the sensor can calculate the time it took to be reflected, and thus the distance of the collision object can be measured.

The sensor can be seen in the following screenshot:

Figure 13.5 – Ultrasonic sensor

Figure 13.5 – Ultrasonic sensor

This sensor board connects to the STM32 Blue Pill microcontroller board with four pins, outlined as follows:

  • Trigger: This pin enables the ultrasonic wave.
  • Echo: This pin receives the reflected wave.
  • VCC: The pin to supply power to the sensor (5V).
  • GND: Ground connection...

Writing a program for getting data from the ultrasonic sensor

In this section, you will learn how to write a program to gather data from the ultrasonic sensor. Let's start, as follows:

  1. First, we will define which pins of the STM32 Blue Pill card will be used to read the sensor data. Also, we will declare two variables to save the duration of the sound-wave travel and another for calculating the distance traveled, as illustrated in the following code snippet:
    const int pinTrigger = PC14;
    const int pinEcho = PC13;
    long soundWaveTime;
    long distanceMeasurement;

    The selected pins were the PC13 and PC14 pins (labeled C13 and C14 on the Blue Pill).

  2. Next, in the setup() function, begin the serial communication. You will set the trigger pin as an output pin and the echo pin as an input pin. We need to initialize the trigger in the LOW value. The code is illustrated in the following snippet:
    void setup() {
      Serial.begin(9600);
      pinMode(pinTrigger, OUTPUT...

Testing the distance meter

Before testing the distance meter, we will need to wire together the buzzer and the ultrasonic sensor to the SMT32 Blue Pill in the solderless breadboard. The following screenshot illustrates a complete circuit diagram including the STM32, ultrasonic sensor, and buzzer together in the solderless breadboard:

Figure 13.8 – Full circuit diagram of our social-distancing device

Figure 13.8 – Full circuit diagram of our social-distancing device

The following screenshot shows how everything should be connected in the actual system:

Figure 13.9 – The buzzer and ultrasonic sensor connections

Figure 13.9 – The buzzer and ultrasonic sensor connections

Now, to complete the connection of the complete social-distancing device, we will need to write a new script combining the Chapter13/buzzer and Chapter13/ultrasonic scripts. The new script will be named Chapter13/distance_meter. Follow these steps:

  1. We need to declare the constants and variables of both scripts and add a new script to define the safety distance between...

Summary

What did we learn in this project? Firstly, we learned how to connect a piezoelectric buzzer to our Blue Pill microcontroller board and code a program to play an audible alarm. Then, we wrote a program to measure the distance between our electronic device and another object.

We also learned how to combine the two projects to create a social-distancing device that can be used to maintain a safe physical distance in this COVID-19 pandemic—for example, in schools, because children are more distracted and are more friendly and sociable.

It is important to remind you that this project is intended for learning purposes and should not be used as a primary alarm for preventing the risk of COVID-19 contagion in any circumstances. This is mainly because, at this time, we know that the main risk is airborne.

In the next chapter, we will learn to build a 20-second hand-washing timer.

Further reading

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}