First Step: Arduino + LED + Piezo + Servo + Pez dispenser.

The first step is to set up the circuit, connecting the Arduino to all the different components.

The goal is to make the circuit work and write the code to create a simplified version of the interaction.



Push the button > make a sound with the Piezo > Blink of a LED > Move Servo 180º and return to its original position.




Breadboard Arduino PEZ dispenser TEST


Circuit Set Up.


  • 1 Pez Dispenser
  • 1 Arduino UNO
  • 1 Breadboard
  • 1 Servo motor
  • 1 Piezo
  • 1 LED
  • 1 PushButton



PEZ arduino croquis



Writing the code:

In order to write the code, I’ve been looking for different examples. I have merged the code for a Push Button + Piezo, an example of PushButton + LED  and PushButton + Servo. (Also, this tutorial of the instruction “Sweep” can be very useful for the future).


This is the result:

Arduino + pushButton+LED+Piezo+Servo

#include <Servo.h>

// constants won’t change. They’re used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
int Buzzer1 = 9;
int ledPin = 13;

Servo myservo; //name of the servo
int pos = 0; // start position of the SERVO.

boolean toggle = true;
int press = 0;

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the piezo as output:
pinMode(Buzzer1, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
// initialize the LED as output
digitalWrite(2, HIGH); //enable pullups to make pin high


void loop()
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// play th Music

// Turn ON the LED
digitalWrite(ledPin, HIGH);
delay(500); digitalWrite(ledPin, LOW);

// SERVO 2

toggle = !toggle;
delay (500);

toggle = !toggle;
delay(500); //delay for debounce


// SERVO #1

for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(50); // waits 15ms for the servo to reach the position

for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees

myservo.write(pos); // tell servo to go to position in variable ‘pos’


/* Questions:

Servo: To make it return to its first position. DONE!

After pushing the button, the Servo returns to origin, but the motor doesn’t stop, it gets stuck. How to fix it?
ORDER OF THE ELEMENTS: PIEZO and LED switch on at the same time. SERVO waits until the LED has finished.

How to make them move all together?

How to make it with more elements PIEZO (or MP3) + Servo + LED (x7) to create the keyboard. Will I need to buy a Conductiveboard or can I do it in Arduino?

How many power do I need to use 7 Servos and LEDS? What components do I need in order to scale the project?





Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: