Components and supplies
Arduino UNO
Jumper wires (generic)
Speaker: 0.25W, 8 ohms
Non Volatile Digital Potentiometer, 10 kohm
Solderless Breadboard Half Size
Ultrasonic Sensor - HC-SR04 (Generic)
Project description
Code
Arduino Theremin code
arduino
1/* 2 This projects is a theremin created with two ultrasonic sensors, one controls the frequency (tone) and the other controls the amplitude (volume). 3 This project is a fork of two projects: 4 - https://create.arduino.cc/projecthub/opus9/theremino-f72d32 (this project had library conflicts) 5 - https://create.arduino.cc/projecthub/jvmccall/ultrasonic-theremin-033d6f (this project had typo errors in the code and a mistake in the schema) 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 Potentiometer conecctions: 11 1 - Arduino pin ~9 12 2 - Speaker + 13 3 - GND 14 4 - 5V 15 5 - Arduino pin ~10 16 6 - Arduino pin 13 17 7 - Arduino pin ~11 18 8 - GND 19 More info https://app.ultralibrarian.com/details/23D04BE7-10A0-11E9-AB3A-0A3560A4CCCC/Microchip/MCP4161-103E-P 20*/ 21 22// NewTone is used to avoid the timer incompatibiliy of Tone with NewPing. Download https://bitbucket.org/teckel12/arduino-new-tone/downloads/ and import .zip 23#include <NewTone.h> 24#include <NewPing.h> 25#include <SPI.h> 26 27#define MIN_DISTANCE 35 28 29NewPing sonar(5, 6, 35); // the first and the second number are the pins of the sensor of volume, the third is the maximum distance 30 31// For SPI 32byte address = 0x00; 33int CS= 10; 34 35int echo = 3; 36int trigger = 4; 37 38int distance; 39int PotValue; 40 41unsigned long TimeResponse; 42float distance2; 43float tone1; 44 45void setup() { 46 // set all the pins 47 Serial.begin(9600); 48 pinMode(trigger, OUTPUT); // tone sensor 49 pinMode(echo, INPUT); // tone sensor 50 pinMode (CS, OUTPUT); 51 SPI.begin(); 52 53 // this has the speaker emit a start up sound from Low to High to Low 54 digitalPotWrite(255); 55 56 for (int i = 50; i <= 400; i++) 57 { 58 pinMode(9, OUTPUT); 59 NewTone(9,i); 60 delay(10); 61 } 62 63 delay(500); 64 65 for (int i = 400; i >= 50; i--) 66 { 67 pinMode(9, OUTPUT); 68 NewTone(9,i); 69 delay(10); 70 } 71} 72 73void loop() { 74 digitalWrite(trigger, HIGH); 75 delayMicroseconds(10); // creates a 10 microsecond pulse 76 digitalWrite(trigger, LOW); 77 TimeResponse = pulseIn(echo, HIGH); // records how long it took to receive echo of the pulse 78 distance2 = TimeResponse/58; // calculates distance in centimeters 79 80 if (distance2 < MIN_DISTANCE) { 81 // conversion of distance into a value for a sound 82 tone1 = 50.0*pow(2,(distance2/12.0)); // calculate frequency, range of about 50-360 Hz 83 pinMode(9, OUTPUT); 84 NewTone(9,tone1); 85 } else { 86 pinMode(9, OUTPUT); // this sets the sound to a high pitched noise when no distance is detected 87 NewTone(9,0); 88 } 89 90 distance = sonar.ping_cm(); // uses the library NewPing to calculate distance 91 92 // distance is converted to Potentiometer relevant values 93 int PotValue = map(distance, 0, 35, 240, 255); // only use a range of resistance which is very low otherwise the speaker is too quiet because it is only a 8 ohm speaker 94 95 if (distance == 0) // sets volume to max when no hand is detected or if hand is too far away 96 { 97 PotValue = 255; 98 } 99 100 digitalPotWrite(PotValue); 101 delay(10); 102} 103 104int digitalPotWrite(int value) { 105 digitalWrite(CS, LOW); // this uses SPI protocol to communicate with the potentiometer and sets the resistance 106 SPI.transfer(address); 107 SPI.transfer(value); 108 digitalWrite(CS, HIGH); 109}
Arduino Theremin code
arduino
1/* 2 This projects is a theremin created with two ultrasonic sensors, one controls the frequency (tone) and the other controls the amplitude (volume). 3 This project is a fork of two projects: 4 - https://create.arduino.cc/projecthub/opus9/theremino-f72d32 (this project had library conflicts) 5 - https://create.arduino.cc/projecthub/jvmccall/ultrasonic-theremin-033d6f (this project had typo errors in the code and a mistake in the schema) 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 Potentiometer conecctions: 11 1 - Arduino pin ~9 12 2 - Speaker + 13 3 - GND 14 4 - 5V 15 5 - Arduino pin ~10 16 6 - Arduino pin 13 17 7 - Arduino pin ~11 18 8 - GND 19 More info https://app.ultralibrarian.com/details/23D04BE7-10A0-11E9-AB3A-0A3560A4CCCC/Microchip/MCP4161-103E-P 20*/ 21 22// NewTone is used to avoid the timer incompatibiliy of Tone with NewPing. Download https://bitbucket.org/teckel12/arduino-new-tone/downloads/ and import .zip 23#include <NewTone.h> 24#include <NewPing.h> 25#include <SPI.h> 26 27#define MIN_DISTANCE 35 28 29NewPing sonar(5, 6, 35); // the first and the second number are the pins of the sensor of volume, the third is the maximum distance 30 31// For SPI 32byte address = 0x00; 33int CS= 10; 34 35int echo = 3; 36int trigger = 4; 37 38int distance; 39int PotValue; 40 41unsigned long TimeResponse; 42float distance2; 43float tone1; 44 45void setup() { 46 // set all the pins 47 Serial.begin(9600); 48 pinMode(trigger, OUTPUT); // tone sensor 49 pinMode(echo, INPUT); // tone sensor 50 pinMode (CS, OUTPUT); 51 SPI.begin(); 52 53 // this has the speaker emit a start up sound from Low to High to Low 54 digitalPotWrite(255); 55 56 for (int i = 50; i <= 400; i++) 57 { 58 pinMode(9, OUTPUT); 59 NewTone(9,i); 60 delay(10); 61 } 62 63 delay(500); 64 65 for (int i = 400; i >= 50; i--) 66 { 67 pinMode(9, OUTPUT); 68 NewTone(9,i); 69 delay(10); 70 } 71} 72 73void loop() { 74 digitalWrite(trigger, HIGH); 75 delayMicroseconds(10); // creates a 10 microsecond pulse 76 digitalWrite(trigger, LOW); 77 TimeResponse = pulseIn(echo, HIGH); // records how long it took to receive echo of the pulse 78 distance2 = TimeResponse/58; // calculates distance in centimeters 79 80 if (distance2 < MIN_DISTANCE) { 81 // conversion of distance into a value for a sound 82 tone1 = 50.0*pow(2,(distance2/12.0)); // calculate frequency, range of about 50-360 Hz 83 pinMode(9, OUTPUT); 84 NewTone(9,tone1); 85 } else { 86 pinMode(9, OUTPUT); // this sets the sound to a high pitched noise when no distance is detected 87 NewTone(9,0); 88 } 89 90 distance = sonar.ping_cm(); // uses the library NewPing to calculate distance 91 92 // distance is converted to Potentiometer relevant values 93 int PotValue = map(distance, 0, 35, 240, 255); // only use a range of resistance which is very low otherwise the speaker is too quiet because it is only a 8 ohm speaker 94 95 if (distance == 0) // sets volume to max when no hand is detected or if hand is too far away 96 { 97 PotValue = 255; 98 } 99 100 digitalPotWrite(PotValue); 101 delay(10); 102} 103 104int digitalPotWrite(int value) { 105 digitalWrite(CS, LOW); // this uses SPI protocol to communicate with the potentiometer and sets the resistance 106 SPI.transfer(address); 107 SPI.transfer(value); 108 digitalWrite(CS, HIGH); 109}
Downloadable files
Arduino Theremin circuit design
Arduino Theremin circuit design
Comments
Only logged in users can leave comments
2 Team members on this project
0