Ultrasonic Sensor M&M Dispenser by Leah and Jojo
by PHS_Engineering in Circuits > Arduino
235 Views, 0 Favorites, 0 Comments
Ultrasonic Sensor M&M Dispenser by Leah and Jojo
This is an M&M dispenser that is controlled by and Arduino Uno. There is an ultrasonic sensor that detects the distance from a persons hand to the device. If the hand is within a certain range, then the windmill inside the dispenser will rotate and M&Ms will be released.
Gather Materials
Materials:
- Ultrasonic sensor
- Vex DC motor
- Arduino Uno
- 11 wires
- 220 Ohms resistor
- Transistor
- Diode
- Jar
- Funnel (made of cardboard)
- Gear and axis
- 3D printed circular cage (for gear)
- Nuts and bolts
- Paper
- M&Ms!
Extra:
- Hot glue gun and glue
- Box cutter
- Wood pieces
Assemble Windmill
Add 6 paddles onto a sprocket
Insert an axle through the wheel
attach to the motor
3D Print
Design a cylinder to fit your windmill piece
Print two of them
Put the windmill piece through the holes
Enclose using hot glue
You may need to use bolts to hold it into place
Wire Windmill (Part 1)
Attach a wire going from pin 6 to the breadboard
Add a 270 Ohm resistor connected to the wire
Add a transistor with the middle prong connected to the other end of the resistor
Add a wire going from the negative column to the left end of the transistor
Put one end of a diode in the same row as the right prong on the transistor
Wire Windmill (Part 2)
Add a white wire going from that same column (with the right prong of the transistor) to a different row
Add a wire going from the right end of the diode to the positive column
Add a wire going from that column (with the right end of the diode) to next to the white wire
Finally, plug the motor (with the windmill) into the breadboard so that it lines up with the white wire and the most recently placed wire
Wire the Ultrasonic Sensor
Connect a blue wire from pin 2
Connect a white wire from pin ~3
Line a black and red wire up with the ones in the + and - column
The ends of all these wires should go together in the sequence of
--------red---white---blue---black-------
Sensor
Your sensor has 4 prongs line each of them up in the same row as shown in the picture above
Your circuit should now look like this
Create Base
For the base we constructed a metal triangle
In the middle of the triangle we added a plate to fit the motor on with the 3D printed case
Then we constructed a funnel out of paper and duct tape for the M&Ms to go through
Code
#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement
int motorPin = 6; // variable for the motor pin
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an output
pinMode(echoPin, INPUT); // Sets the echoPin as an input
Serial.begin(9600); // Sets up the serial monitor
pinMode (motorPin,OUTPUT); // Sets the motor pin as an output
}
void loop() {
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Finds the distance between the object (hand in this case) and the sensor
distance = duration * 0.034 / 2;
// Displays the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
if (distance <= 10) // Asks if the distance is less than or equal to 10 cm
{
digitalWrite(motorPin, HIGH); // If so, apply power to the motor pin
}
else
{
digitalWrite(motorPin, LOW);
// If not, don’t apply power to the motor pin
}
}