As part of a university project, we had to create a project of our choice in programming. Without even thinking twice, I decided to reproduce the instrument known as the theremin. This project means a lot to me because I really love music and I’m a musician myself.
Being a beginner in programming, I struggled quite a bit, but I’m actually proud of what I managed to accomplish in just a few weeks.
Here are the libraries I used :
- #include <Arduino.h>
- #include "Adafruit_VL53L0X.h"
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_ILI9341.h>
The goal was to produce a sound using two sensors :
- VL53L0X to modify the pitch (operates in I2C)
- HC-SR04 to control the speed of the sound
I used 3 * 10 kΩ resistors for the ECHO pin of the HC-SR04 sensor to adjust the voltage. Indeed, this makes it possible to reduce the voltage from 5V to 3.3V, since the STM32 pins operate with a logic level of 3.3V.
How to create a frequency ?I take the distance from the VL53L0X sensor and calculate a frequency according to the following equation: 5 * (distance - 20) + 150.
I obtain a frequency ranging from 50 Hz to 2000 Hz.
To limit the values, I used the constrain()function, which was very helpful.
To generate a sound, I used SP-6120 speakers. These are simple speakers that allow for safe testing. I used a mini-jack adapter/cable and connected it to pin PA4 : const int GENE_AUDIO = PA4;
To generate a sound, I used an easy and practical function : tone(GENE_AUDIO, frequence); and noTone() to stop the sound.
How to change the speed ?To change the speed, I used the HC-SR04 sensor. To calculate its distance, I had to use a relation linking speed, distance, and time :
float distanceHC = duree * 0.0343 / 2.0; //We divide by two because we want to know the one-way distance (not the round-trip).
Three distance intervals will correspond to three different duration values, resulting in three distinct speeds and this interval will be added to the current time value to generate the sound.
How can two sensors be operated simultaneously?
The entire code uses a timer (millis()andmicros()) to prevent blocking and reduce slowdowns. No delay() was used except for the HC-SR04 sensor, where a microsecond delay is minimal and required for correct functioning.
After integrating the two sensors, I added an LED whose color changes depending on the distance measured by the VL53L0X sensor. This provides a visual indication in addition to the display.
To produce a gradient, I employed a very handy function :
map(distanceVL, 50, 200, 0, 255);
Explanation : distanceMin / distanceMax / valueMin / valueMax
I used 3 * 330 Ω resistors, one for each color of the RGB LED (Red - Green - Blue). This is essential to control the current flow.
Here is a short video :
Finally, I used an SPI TFT display to show the following values :
- VL53L0X distance (50 to 500mm)
- HC-SR04 distance (10 to 100cm)
- The frequency (150Hz to 200Hz)
- The speed (100ms to 300ms to 800ms between each beep)














Comments