Wearable switch for sound

In this assignment i want to make a switch that closes a circuit that produces sound as output. The switch is an analog sensor that i will connect with a NodeMCU and a breadboard. For the sound as output i will use a Piezo Buzzer.

I will make the switch from cardboard and copper tape. The switch would have to fit in a serie of products as shown in the image below.

For my idea i want to make the switch as an wearable with two rings that will close the circuit whenever they touch eachother.

For prototyping purposes i am using gray cardboard and for the finished version i will use black cardboard.

First i draw some lines and than i cut out the unnecessary space. I leave a strip in the middle that connects the rings with each-other and to make it impossible to lose one ring.

After cutting i rolled up the strips that i will use as rings and secured it with adhesive tape. With a black marker I've marked the spots where i want to secure the copper tape.

This is how it looks like this far.

I taped some copper tape and can now start connecting everything. To make sure the switch works, I try it out with a LED.

Thanks to Kaj and Loes i discovered that i used a 10K ohm resistor and instead i should use a 220 ohm resistor. I switched the resistors and the LED was brighter.

Now comes the tricky part, connecting it with a buzzer instead of a LED and figuring out how to connect everything.

I've drawn a schematic from my first try.

Then the coding started, that was easier because I've worked with Arduino before and still remembered a lot. For the speaker i want it to play Sandstorm by Darude so I searched on the internet for the code of the notes that are in the song. I came across this video and copied the code of the notes that the maker left in the description.

The code was written for the Arduino UNO so i had to change everywhere where he defined "pin 8" to the D2 pin where i connected my buzzer. Then i wrote the code for the switch and used if and else with the sound. If i press the button music will play, else there is no music.

This is how my code looked so far:

const int buttonPin = D0;
const int speakerPin = D2;

#include "pitches.h"

int buttonState = 0;

 int shortTone = 80;
 int longTone = 200;
 int standardDelay = 50;
 int delayBetweenBars = 50;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  pinMode(speakerPin, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  buttonState = digitalRead(buttonPin);
  Serial.println(buttonState);

  if(buttonState == HIGH){
tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
   delay(shortTone);
  noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_B3, shortTone);
  delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_B3, longTone);
 delay(longTone);
 noTone(D2);
 delay(delayBetweenBars);
 
    tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
   delay(shortTone);
  noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_B3, shortTone);
  delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_B3, longTone);
 delay(longTone);
 noTone(D2);
 delay(standardDelay);
 
     tone(D2,  NOTE_E4, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2, NOTE_E4, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_E4, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_E4, shortTone);
   delay(shortTone);
  noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_E4, shortTone);
  delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_E4, shortTone);
 delay(shortTone);
 noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_E4, longTone);
 delay(longTone);
 noTone(D2);
 delay(standardDelay);
 
 
     tone(D2,  NOTE_D4, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,NOTE_D4, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_D4, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_D4, shortTone);
   delay(shortTone);
  noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_D4, shortTone);
  delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_D4, shortTone);
 delay(shortTone);
 noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_D4, longTone);
 delay(longTone);
 noTone(D2);
 delay(standardDelay);
 
tone(D2, NOTE_A3, longTone);
delay(longTone);
 noTone(D2);
 delay(standardDelay);
 
 tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
   delay(shortTone);
  noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_B3, shortTone);
  delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_B3, longTone);
 delay(longTone);
 noTone(D2);
 delay(delayBetweenBars);
 
    tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
   delay(shortTone);
  noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_B3, shortTone);
  delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_B3, longTone);
 delay(longTone);
 noTone(D2);
 delay(standardDelay);
 
 tone(D2, NOTE_E4, longTone);
 delay(longTone);
 noTone(D2);
 delay(standardDelay);
 
 tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
   delay(shortTone);
  noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_B3, shortTone);
  delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_B3, longTone);
 delay(longTone);
 noTone(D2);
 delay(delayBetweenBars);
 
    tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
   delay(shortTone);
  noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_B3, shortTone);
  delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_B3, longTone);
 delay(longTone);
 noTone(D2);
 delay(standardDelay);
 
tone(D2, NOTE_E4, longTone);
 delay(longTone);
 noTone(D2);
 delay(standardDelay);
  }

  else {
    noTone(D2);}
  }
 

In the top of the code you see that i included a library named "pitches". That is a library with different notes that will come out of the buzzer. Otherwise there will be no sound.

This is what the library looks like:

/*************************************************
 * Public Constants
 *************************************************/

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978

You can find this on the website from Arduino. https://www.arduino.cc/en/Tutorial/toneMelody

After uploading the code to the NodeMCU it did not work. The speaker was playing the song but the switch was not working. I was sure that there was nothing wrong with the code so i had to figure it out with the cables to make the right circuit.

After almost 2 hours of debugging it finally worked out, sort of, but it was good enough.

I looked in the previous slides from Micky's tutorial with the button and LED and how to connect it with digital pins.

Now my schematic looks like this:

Now i will start with the black cardboard.

First i measured and drew lines where i want to cut.

The result after cutting everything:

I also cut out a piece from the stripes on each side so the rolling will go easier. After that i put some glue on the short sides and rolled it, holding it for a few seconds until it dried.

Now i cut out 2 pieces of copper tape. One is 4cm and the other 3cm. The 4cm piece is the piece what gets two paperclips for connecting. After placing the copper tape i'm done and ready to connect for the final test.

After connecting and turning it on again i discovered that it wasn't working properly. So i tried to switch the D0 to the A0 pin and changing it in the code as-well. I also changed the if and else statements after looking in the serial monitor what the values were. After uploading to the NodeMCU and testing it finally worked how i wanted it to work.

The final code:

const int buttonPin = A0;
const int speakerPin = D2;

#include "pitches.h"

int buttonState = 0;

 int shortTone = 80;
 int longTone = 200;
 int standardDelay = 50;
 int delayBetweenBars = 50;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  pinMode(speakerPin, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  buttonState = analogRead(buttonPin);
  Serial.println(buttonState);

  if(buttonState > 100){
tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
   delay(shortTone);
  noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_B3, shortTone);
  delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_B3, longTone);
 delay(longTone);
 noTone(D2);
 delay(delayBetweenBars);
 
    tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
   delay(shortTone);
  noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_B3, shortTone);
  delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_B3, longTone);
 delay(longTone);
 noTone(D2);
 delay(standardDelay);
 
     tone(D2,  NOTE_E4, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2, NOTE_E4, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_E4, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_E4, shortTone);
   delay(shortTone);
  noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_E4, shortTone);
  delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_E4, shortTone);
 delay(shortTone);
 noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_E4, longTone);
 delay(longTone);
 noTone(D2);
 delay(standardDelay);
 
 
     tone(D2,  NOTE_D4, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,NOTE_D4, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_D4, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_D4, shortTone);
   delay(shortTone);
  noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_D4, shortTone);
  delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_D4, shortTone);
 delay(shortTone);
 noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_D4, longTone);
 delay(longTone);
 noTone(D2);
 delay(standardDelay);
 
tone(D2, NOTE_A3, longTone);
delay(longTone);
 noTone(D2);
 delay(standardDelay);
 
 tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
   delay(shortTone);
  noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_B3, shortTone);
  delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_B3, longTone);
 delay(longTone);
 noTone(D2);
 delay(delayBetweenBars);
 
    tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
   delay(shortTone);
  noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_B3, shortTone);
  delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_B3, longTone);
 delay(longTone);
 noTone(D2);
 delay(standardDelay);
 
 tone(D2, NOTE_E4, longTone);
 delay(longTone);
 noTone(D2);
 delay(standardDelay);
 
 tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
   delay(shortTone);
  noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_B3, shortTone);
  delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_B3, longTone);
 delay(longTone);
 noTone(D2);
 delay(delayBetweenBars);
 
    tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
   delay(shortTone);
  noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_B3, shortTone);
  delay(shortTone);
 noTone(D2);
 delay(standardDelay);
 tone(D2,  NOTE_B3, shortTone);
 delay(shortTone);
 noTone(D2);
  delay(standardDelay);
tone(D2,  NOTE_B3, longTone);
 delay(longTone);
 noTone(D2);
 delay(standardDelay);
 
tone(D2, NOTE_E4, longTone);
 delay(longTone);
 noTone(D2);
 delay(standardDelay);
  }

  else if (buttonState < 99) {
    noTone(D2);}
  }

I'm not sure if this was the right way of debugging but it worked.

Video of the final result:

I made another variant of the wearable switch that would fit better with the rest of the series. Instead of cutting the unnecessary space i only cut the strips to make two rings. Now the design is more square and would fit better with the rest. In addition the copper tape goes around the ring.

Last updated