This is an Arduino-based theremin that uses two ultrasonic sensors to control the volume and frequency of the sound output.
Devices and components
Arduino Uno Rev3
Jumper wires (generic)
Ultrasonic sensor - HC-SR04 (generic)
Speaker: 3 W, 4 ohms
Breadboard (generic)
Non-volatile digital potentiometer, 10 kohm
Materials and tools
Hot glue gun (generic)
Soldering iron (generic)
Project description
This
is
a
simple
project
for
creating
a
theremin
which
uses
ultrasonic
sensors
for
user
input.
It
works.
Theremin code
arduino
1
2//In this project we create a theremin with two ultrasonic sensors, one for volume and another for tone. We use the digital potentiometer X9C103P.
3// Two ultrasonic sensors are used to create a theremin. One sensor controls volume, the other controls frequency.
4// This project is an iteration on a previous project I found for a theremin. The previous project had library conflicts (two libraries used the same timer) and used a different type of poteniometer. I have also added some other small features.
5// Project which provided original inspiration: https://create.arduino.cc/projecthub/opus9/theremino-f72d32
6// Resource on controlling similar potentiometer using SPI protocol, provides example code and electrical setup: http://www.learningaboutelectronics.com/Articles/MCP4131-digital-potentiometer-circuit.php
7
8 *
9 */
10//NewTone is used to avoid the timer incompatibiliy of Tone with NewPing.
11#include <NewTone.h>
12#include <NewPing.h>
13#include <SPI.h>
14#define MIN_DISTANCE 35
15
16NewPing sonar(5, 6, 35); //sonar(pin, pin, distance), this controls volume
17
18// name pins for sensors and the potentiometer
19
20
21// For SPI
22byte address = 0x00;
23int CS= 10;
24
25int echo = 3;
26int trigger = 4;
27
28int distance;
29int PotValue;
30
31unsigned long TimeResponse;
32float distance2;
33float tone1;
34
35void setup() {
36// set all the pins
37Serial.begin(9600);
38pinMode(trigger, OUTPUT); //Tone sensor
39pinMode(echo, INPUT); //Tone sensor
40pinMode (CS, OUTPUT);
41SPI.begin();
42
43//This has the speaker emit a start up sound from Low to High to Low
44digitalPotWrite(255);
45for (int i = 50; i <= 400; i++)
46{
47pinMode(9, OUTPUT);
48 NewTone(9,i);
49 delay(10);
50}
51delay(500);
52for (int i = 400; i >= 50; i--)
53{
54pinMode(9, OUTPUT);
55 NewTone(9,i);
56 delay(10);
57}
58
59
60}
61
62void loop() {
63
64 digitalWrite(trigger, HIGH);
65 delayMicroseconds(10); // creates a 10 microsecond pulse
66 digitalWrite(trigger, LOW);
67 TimeResponse = pulseIn(echo, HIGH); // records how long it took to receive echo of the pulse
68 distance2 = TimeResponse/58; //calculates distance in centimeters
69
70
71 if (distance2 < MIN_DISTANCE) {
72 //Conversion of distance into a value for a sound
73 tone1 = 50.0*pow(2,(distance2/12.0)); //calculate frequency, range of about 50-360 Hz
74 pinMode(9, OUTPUT);
75 NewTone(9,tone1);
76 }
77 else {
78 pinMode(9, OUTPUT); // This sets the sound to a high pitched noise when no distance is detected
79 NewTone(9,400);
80 }
81
82distance = sonar.ping_cm(); //Uses the library NewPing to calculate distance
83
84//distance is converted to Potentiometer relevant values
85
86int PotValue = map(distance, 0, 35, 240, 255); // I only use a range of resistance which is very low otherwise the speaker is too quiet becuae it is only a 4 ohm speaker
87
88if (distance == 0) // sets volume to max when no hand is detected or if hand is too far away
89{
90 PotValue = 255;
91}
92
93digitalPotWrite(PotValue);
94delay(10);
95
96
97}
98
99int digitalPotWrite(int value)
100{
101digitalWrite(CS, LOW); //This uses SPI protocol to communicate with the potentiometer and sets the resistance
102SPI.transfer(address);
103SPI.transfer(value);
104digitalWrite(CS, HIGH);
105
106
107}
108
Downloadable files
Theremino circuit
Theremino circuit
Theremino circuit
Theremino circuit
Note: Content and images are from: https://projecthub.arduino.cc/, with some modifications.
If you want it removed due to copyright reasons, please leave a comment. Thank you.
I want to share this article more widely so that everyone knows about Arduino and your project.