entrada-POST

Welcome to the latest entry of the Programming with Arduino and Protocoder for makers course! In this lesson, we will be using two ultrasound bat sensors to calculate the distance from the hand, and then we will play a sound using the buzzer.

List of materials

  • ZUM BT-328 controller board or one compatible with Arduino.
  • 2 x ultrasound bat sensors
  • Buzzer module

Concepts that we will learn

In this entry we will be learning how to use the ultrasound sensors to calculate the distance in centimetres and we will use the Arduino Tone function to emit sounds from the buzzer. To connect the ultrasound bat sensors, we will follow the indications given in the table:

Bat 1 module Bat 2 module
VCC VCC VCC
GND GND GND
ECHO Pin 8 Pin 9
TRIGGER Pin 10 Pin 11

You can see how to connect the modules to the board in the image below:conexion_theremin

The code

Now we will start declaring the global variables in Arduino code, which in this case will only relate to the connections of the two pins.

Then we start the program with the setup function:

In this function, we indicate which pins will function as INPUT and OUTPUT. The code for the loop function is:

First we declare the frec_maxima local variable, which tells Arduino the maximum frequency for sounding the buzzer. Then we declare the distancia_frecuencia and distancia_tiempo variables, which store the distance read on the ultrasound sensors, meaning the distances to be used to modify the frequency of the sound, and the time for playing each “note”, respectively. To read the distance, we use the distance function which takes the values that we send to it from the principal code, the number of the trigger pin and the echo of the ultrasound sensor as arguments. If you want to know more about the ultrasound sensor, check out this tutorial on the Arduino website. The code for the distance function is:

You will see how this function calls the TP_init function,  which is sent the data of the pins that we have stated first in the loop function as arguments, and we can keep sending data from one to another with no problem. The code for TP_init function is as follows:

Within this function, we change the status of the pins and send various signals of 2 and 10 microseconds, via the Trigger pin of the ultrasound sensor, and then we use the  pulseIn function on the Echo pin to calculate the time the pin takes to change its value from high to low. The said time corresponds to the time taken by the pulse to be received after being sent. To find out more, you can visit the Arduino reference page for the pulseIn function. The TP_init function returns the value given by the pulseIn function in microseconds and within the distance function, we use the value received in microseconds to calculate the distance from the object. Once we have calculated the distances for the ultrasound sensors, we will use the constrain function shown in the previous tutorial, to make sure that the readings we have made stay within certain margins. Remember to change the values so that you get more practice and learn more. Once we have used the constrain function, we will use the map function, which is also shown in the previous tutorial, to modify the range of values. The distancia_tiempo variable could multiply the value by 10, so the map function was not absolutely necessary. Finally, we make the buzzer sound using the tone command:

The tone function is what makes the tone play on the buzzer. Its parameters are the pin connected to the buzzer, the frequency and time that the tone will be played. Next we will create a delay for the same time as the tone, so that it is played in full. As an exercise, have a go at modifying the minimum and maximum frequency values, modify the scale that the values are mapped to and how it affects the buzzer.

Bye for now!