Theremin - Arduino Project

Digital Theremin

The digital theremin is a simple Arduino based project which plays certain sounds depending on the distance of an object from an ultrasound distance sensor. It is a simple project which I designed in the span of about 45 to 60 minutes.





All you will need for this project is:

  • An Arduino UNO
  • An Ultrasound Distance Sensor (Specifically HC-SR04)
  • Wires
  • And the open-source Arduino Software (You can download it for free at https://www.arduino.cc/en/Main/Software)
  • A 220  Ω  Resistor
  • 3 differently coloured LEDs
  • A piezo
You first wire up your components as shown in the diagram below.



Even though I showed you the diagram of how the device should be wired, you should check what to plug in where on your device. I will explain why everything is wired in the way it is.

Let's start with the Ultrasound Distance Sensor. It first needs to be connected to the circuit. You first connect the Ultrasound Distance Sensor to the GND wire and then you connect it with the 5V wire (These wires have already been connected to the breadboard. This means that you just need to plug the Ultrasound Distance Sensor into the right holes in the breadboard.) You then connect the Ultrasound Distance Sensor's Trig pin into the digital inlet 8 . You then connect the Ultrasound Distance Sensor's Echo pin into the digital inlet 9

The piezo will be connected to the ground inlet through the ground pin and the digital inlet 10 through the other pin.

The LEDs will all be wired up the same way. The short pin will be connected to the ground wire though a 220 Ω resistor and the long pin will be connected to the digital inlets on the arduino (I used inlets 5, 6, 7).

Here are some pictures of how I wired up my arduino.








When your board is wired up you can start coding. Here is the simplest version of the code.

#define trigPin 8
#define echoPin 9
#define ledPin1 7
#define ledPin2 6
#define ledPin3 5

void setup() {
  Serial.begin(9600);
  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
}

void loop() {
 int frequency = measureDistance() * 50; 

  tone(10, frequency);
  if(frequency <= 150 ){
    Serial.println("1");
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
  } else if(frequency > 150 && frequency <= 650) {
    Serial.println("2");
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
  } else if(frequency > 650 && frequency <= 1150) {
    Serial.println("3");
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, LOW);
  } else if(frequency > 1150 ) {
    Serial.println("4");
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH);
  }
  Serial.println(frequency);
  delay(10);

}

float measureDistance() {
  float duration, distance;
  digitalWrite(trigPin, LOW);  
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  return (duration/2) / 29.1;

}

Komentáre