Introduction: Arduino Mini Candy Vending Machine That Plays Music

For this project, I made a mini candy vending machine that plays a song when you turn the vending machine on. The vending machine can be refilled by opening the top wood piece, and there's an opening covered by see-through glass so you can see the contents. When you press the button on the vending machine, the buzzer will play a song for you and the hatch will open at the rhythm of the song, making candy drop into a little candy collecting box that you can open as well.

This is my first ever Arduino project, so I probably made some mistakes here and there. But the machine is functional! I wanted to make something that was easy and something I might still use after this school project.

Supplies

Electronic components

  • Arduino Uno
  • USB-A to USB-B 2.0 cable
  • SG90 Mini Analog Servo 1x
  • 5v passive buzzer 1x
  • PCB-Mount push buttons 1x
  • jumper wires 7x
  • printed circuit board 2x small

Materials for housing/vending machine

  • Wood plank 3mm
  • easily modifiable/cuttable see-through plastic surface
  • Wooden plank scrap
  • (optional) Paint
  • (optional) Decorative Stones

Tools

  • Soldering iron
  • Solder
  • Laser cutter (for wood)
  • Stripping pliers/scissors
  • Super glue (for wood)

Step 1: Designing and Testing

I started by making a design for my vending machine and thinking about the layout for the mechanical components as well. When I came to a design I was happy with, I started to experiment with the individual components I had. At first I had only planned to make a regular vending machine, but when I had gotten the servo rotating at the push of the button, I found the project to be too boring. I then figured that a vending machine that could play music and have the hatch open at the rhythm of the song would be more interesting, so I started to experiment with that layout to get it all working.


After I was finished with the layout, I polished up the code to have the servo turn with the rhythm of the song. The song I had to code in myself. The video shows one of my tests for this.

Step 2: (Finished) Code

#include <Servo.h>


// constants won't change
const int BUTTON_PIN = 7; // Arduino pin connected to button's pin
const int SERVO_PIN  = 9; // Arduino pin connected to servo motor's pin


// variables will change:
int angle = 0;          // the current angle of servo motor
int lastButtonState;    // the previous state of button
int currentButtonState; // the current state of button
Servo servoMotor;


void setup() {
  Serial.begin(9600);                // initialize serial
  pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
  servoMotor.attach(SERVO_PIN);           // attaches the servo on pin 9 to the servo object


  servoMotor.write(angle);
  currentButtonState = digitalRead(BUTTON_PIN);


  pinMode(5, OUTPUT); //For buzzer
}


void loop() {
  lastButtonState    = currentButtonState;      // save the last state
  currentButtonState = digitalRead(BUTTON_PIN); // read new state


  if(lastButtonState == HIGH && currentButtonState == LOW) {
    Serial.println("The button is pressed"); //This message shows up in the Serial Monitor if Arduino registers you pressing the button.


// //Plays a song
    tone(5, 587); //buzzer note D
    delay (250);


    tone(5, 659); //buzzer note E
    delay (250);


    tone(5, 689); //buzzer note F
    delay (250);


    tone(5, 784); //buzzer note G
    delay (250);


    tone(5, 689); //buzzer note F
    delay (250);


    tone(5, 659); //buzzer note E
    delay (250);


    tone(5, 587); //buzzer note D
    delay (250);


    noTone(5); //stops buzzer
    delay (250);


    tone(5, 587); //buzzer note D
    delay (250);


    tone(5, 659); //buzzer note E
    delay (250);


    noTone(5); //stops buzzer
    delay (250);


    tone(5, 554); //buzzer note C#
    delay (250);


        //This makes the servo turn
    for (int position = 0; position <= 100; position++) {
      servoMotor.write(position); //turns the servo to open the candy hatch
    }


    tone(5, 587); //buzzer note D
    delay (500);


// control servo motor arccoding to the angle
    servoMotor.write(angle); //turns the servo to close the candy hatch


    noTone(5); //stops buzzer
    delay (250);


//note: start sond part 2


    tone(5, 784); //buzzer note G
    delay (250);


    tone(5, 689); //buzzer note F
    delay (250);


    tone(5, 659); //buzzer note E
    delay (250);


    tone(5, 587); //buzzer note D
    delay (250);


    noTone(5); //stops buzzer
    delay (250);


    tone(5, 587); //buzzer note D
    delay (250);


    tone(5, 659); //buzzer note E
    delay (250);


    tone(5, 554); //buzzer note C#
    delay (250);


    tone(5, 587); //buzzer note D
    delay (250);


            //This makes the servo turn
    for (int position = 0; position <= 100; position++) {
      servoMotor.write(position); //turns the servo to open the candy hatch
    }


    tone(5, 392); //buzzer note G
    delay (500);


    // control servo motor arccoding to the angle
    servoMotor.write(angle); //turns the servo to close the candy hatch


    noTone(5); //stops buzzer
    delay (250);


//song part 3
    tone(5, 784); //buzzer note G
    delay (250);


    tone(5, 689); //buzzer note F
    delay (250);


    tone(5, 659); //buzzer note E
    delay (250);


    tone(5, 587); //buzzer note D
    delay (250);


    noTone(5); //stops buzzer
    delay (250);


    tone(5, 587); //buzzer note D
    delay (250);


    tone(5, 659); //buzzer note E
    delay (250);


    noTone(5); //stops buzzer
    delay (250);


    tone(5, 554); //buzzer note C#
    delay (250);


    noTone(5); //stops buzzer


//This makes the servo turn
    for (int position = 0; position <= 100; position++) {
      servoMotor.write(position);
     // if (position == 180) direction = 0;
      delay(3); //Alter this to decide how much candy you want per serving.
    }

    // control servo motor arccoding to the angle
    servoMotor.write(angle);


//part 4 song
    noTone(5); //stops buzzer
    delay (1250);


    tone(5, 293); //buzzer note D
    delay (500);


    tone(5, 330); //buzzer note E
    delay (250);


    tone(5, 350); //buzzer note F
    delay (250);


    tone(5, 392); //buzzer note G
    delay (250);


    tone(5, 440); //buzzer note A
    delay (250);


        //This makes the servo turn
    for (int position = 0; position <= 100; position++) {
      servoMotor.write(position); //turns the servo to open the candy hatch
    }


    tone(5, 100); //buzzer note ?
    delay (750);


// control servo motor arccoding to the angle
    servoMotor.write(angle); //turns the servo to close the candy hatch


    noTone(5); //stops buzzer
    delay (500);


    tone(5, 784); //buzzer note G
    delay (250);


    tone(5, 689); //buzzer note F
    delay (250);


    tone(5, 659); //buzzer note E
    delay (250);


    tone(5, 587); //buzzer note D
    delay (250);


    noTone(5); //stops buzzer
    delay (250);


    tone(5, 587); //buzzer note D
    delay (250);


        //This makes the servo turn
    for (int position = 0; position <= 100; position++) {
      servoMotor.write(position); //turns the servo to open the candy hatch
    }


    tone(5, 554); //buzzer note C#
    delay (750);


    //control servo motor arccoding to the angle
    servoMotor.write(angle); //turns the servo to close the candy hatch


    tone(5, 587); //buzzer note D
    delay (250);


    noTone(5);
    delay (500);


    tone(5, 880); //buzzer note A
    delay (250);


    tone(5, 689); //buzzer note F
    delay (250);


    tone(5, 784); //buzzer note G
    delay (250);


    tone(5, 659); //buzzer note E
    delay (250);


    tone(5, 554); //buzzer note C
    delay (250);


    tone (5, 466); //buzzer note B
    delay (250);

    tone(5, 659); //buzzer note E
    delay (225);  

    noTone(5); //buzzer stops
    delay (10);


    tone(5, 659); //buzzer note E
    delay (225);  

    noTone(5); //buzzer stops
    delay (10);

    tone(5, 659); //buzzer note E
    delay (500);


    noTone(5); //buzzer stops
    delay (750);


    tone (5, 466); //buzzer note B
    delay (250);


    tone(5, 554); //buzzer note C
    delay (250);


    noTone(5); //buzzer stops
    delay (250);


    tone (5, 440); //buzzer note A
    delay (250);


    tone(5, 659); //buzzer note E
    delay (250);


    noTone(5); //buzzer stops
    delay (250);


    tone (5, 880); //buzzer note A
    delay (250);


    noTone(5); //buzzer stops
    delay (500);


        //This makes the servo turn
    for (int position = 0; position <= 100; position++) {
      servoMotor.write(position); //turns the servo to open the candy hatch
    }


    tone (5, 587); //buzzer note D
    delay (750);


    //control servo motor arccoding to the angle
    servoMotor.write(angle); //turns the servo to close the candy hatch


    noTone(5); //buzzer stops
    delay (250);
  }


}

Step 3: Soldering

I stripped some wires, I then moved the design to a printed circuit board and soldered the parts together (buzzer and button). Soldering helps components stay on more secured.

Step 4: Building the Housing

I used my university’s laser cutter to cut out (most of the) components for the housing. The main vending machine box and the small candy collecting box’s design were generated by an online box generator and then downloaded as an SVG file. I then modified said file in a program called InkScape, to better fit my design. I then exported the file as a DXF file and used the laser cutter to cut out the design in wood. The wood I used for the design was 3cm. An estimated height and width of the wooden plank used for the design is 55 x 75cm. I had used the wooden scrap to create the opening from where the candy comes from.


A piece of wood was placed on the servo to block the entrance of where the candy falls down. I attached the servo to a 6cm width piece of wood, and attached the end of that wood to the inner wall. Now the servo was in the right position to fully block the opening. The buzzer and the button were glued to the inside of the vending machine, and stuck out a hole to be able to press. A hole was cut in the bottom of the vending machine to have the USB cable stick out to be able to attach it to a power source (You can also use a power bank and keep the cable inside, but I needed to connect my Arduino to my laptop as power source since I didn’t have a power bank to use). I then placed the see-through plastic surface inside the open square of the vending machine box to make a window, so people could see the candy.


Decorating your vending machine is optional, but very fun to do. Highly recommend it! I added a video to show how the mechanics works from the inside.

Step 5: Reflection

This was my first time using an Arduino, so in general I’m pretty satisfied with the end result. By the time I had finished this project, I realized the electricity for the buzzer and button should’ve gone through a resistor (The picture here is what I probably should've used as layout, or I probably should've started with creating the layout with a program from the start). I also didn’t fully design how the candy would fall into the box with wood, so I ended up using scrap wood to craft something that would make the candy lean towards the opening of the vending machine. Nevertheless, the project is still functional. Learning how to solder and how to use a laser cutter has also been very useful and fun. I’ve realized Arduino might not be something I’d want to do for a second time, but I’m still really happy with what I’ve built and I got to learn something new.