top of page
  • Writer's picture1/4" Jack Of All Trades

Physical Computing Week 2 - Fun With Ultrasonic Sensors

Updated: Nov 3, 2020

- A Dystopian Gadget for the Discerning Citizen -

Group design concept that was come up with was the idea of a wearable COVID alarm utilising an ultrasonic sensor to detect the proximity of other people. Some form of alarm or alert would then play to alert the wearer to anti-mask nutcases (or ninja-assassins).

The prototyping for my own build of the project went relatively smoothly, I added some LEDs to the design found on the sensor's tutorial page, although I was having an issue with some fairly wild anomalies appearing in the readings:

At first I considered the idea of attempting to find an average and base the conditional logic off that, but then took a better look at the code I had copied and noticed a fairly simple optimisation I could make: my code was only using the centimetre value, not the inches, so I deleted the inches variable and the resulting code ran a lot smoother and stopped producing such wild anomalies, although objects moving forwards and backwards quickly in the alert zone can still cause the green LED to light up. This might end up being an issue for my proposed musical version of this project (see end section), but is close enough to functional at this point. I also found this interesting thread about planning Arduino projects and optimising code which suggests that the delay() function is not optimal for use in the loop() function - as it holds up the whole code, the author prefers millis() which delays the interaction in time but allows rest of code to function, but I need to do more research to fully understand this new function, so stuck with the inefficient alternative.

Here is my altered version of the code that reduces errors:

const int pingPin = 7; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 6; // Echo Pin of Ultrasonic Sensor
const int ledPin1 = 3; // LED Pin
const int ledPin2 = 4; // LED Pin
const int safeDistance = 20; // social distancing bro

void setup() {
   Serial.begin(9600); // Starting Serial Terminal
   pinMode(ledPin1, OUTPUT);
   pinMode(ledPin2, OUTPUT);

void loop() {
   long duration, cm; //deleted the inches variable and now it runs smoother
   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   digitalWrite(pingPin, HIGH);
   digitalWrite(pingPin, LOW);
   pinMode(echoPin, INPUT);
   digitalWrite(ledPin1, LOW);
   digitalWrite(ledPin2, LOW);
   duration = pulseIn(echoPin, HIGH);
   cm = microsecondsToCentimeters(duration);
   if(cm < safeDistance)
    digitalWrite(ledPin2, HIGH);
    Serial.println("Whoa there buddy!");
    else if(cm > safeDistance)
     digitalWrite(ledPin1, HIGH);

long microsecondsToCentimeters(long microseconds) 
   return microseconds / 29 / 2;

void playAudio()
  //some code to play an alarm


Next Steps & Future Projects

Next step would be to add audio, I have found this interesting resource for Arduino based synthesis. A synthetic tone, rich in harmonics and unnatural to the human ear, is the a great way to grab people's attention - alarms transcend language barriers (allowing ease of use abroad). Calculating the speed of any oncoming assailants/objects could also have it's uses, not least for additional ninja-assassin protection.

Inspired by this dystopian product idea I have also started researching a personal project which will predictably utilise the ultrasonic sensor to make noises. I intend to build a Theremin-like instrument that utilises granular synthesis and bring a unique form of analogue interaction to a very digital form of synthesis (perhaps using multiple ultrasonic sensors). Some form of SD card reading capabilities would be desirable. I would also love make the controller semi modular/eurorack CV compatible (+/- 5v) if possible. The proposed idea would also be a COVID safe music instrument.

EDIT: I intend to build this wavetable synth and go from there. I also thought about experimenting with the motion sensor to create gate signals - could be interesting if arranged in arrays?

Further Research:

Eurorack Arduino Dev Board - Too expensive for Arduino culture? Could I make my own one of these?

Simple Granular Synth w/ Arduino - Seems like good start for mine - how to integrate ultrasonic sensor(s)?

62 views0 comments


bottom of page