Smart Candy Box
The 7th-week assignment of the Maker Diploma is about making things smarter using Arduino code structure and writing Arduino C code syntax, variables, and different functions. The required is designing and programming a smart device that performs a certain function or solves a problem. The smart device should have multiple input components that control multiple output components using Arduino.
I decided to make a Smart Candy Box to implement my final project idea as a cardboard prototype. What inspired me to make this box is the need for creative ideas to grab visitors' attention during events and exhibitions. My current job requires my team and me to participate in many events, and we always need to come up with fun and innovative ideas to attract visitors to our booth.
I used Tinkercad to design and simulate the Smart Candy Box's circuit to make sure it was functional before being prototyped on an actual breadboard. After that, I used Arduino IDE to write and program Arduino C text code; then uploaded the code to the Arduino board.
To build the Smart Candy Box, I used the following components and materials:
Breadboard
Jumper wires
Arduino UNO
Arduino USB cable
DC motor - Hobby
L298D dual H-bridge motor driver (I used the L293D motor driver in the wiring diagram because there's no L298N driver in Tinkercad)
Ultrasonic sensor
Pushbutton
NeoPixel stick - 8 LED strip
Piezo buzzer
LCD 16x2
9V-2A power source adapter
For the Smart Candy Box enclosure, I used the following materials and tools:
Cardboard
Glue gun
Cutter and scissors
Pencil and ruler
Bottle cap (for the DC motor)
Steel wire for the coil spring (vending machine coil).
The idea for the Smart Candy Box is for the user to activate the LCD screen characters when he/she comes close to the box. Then, push the button when the buzzer makes a beep sound. After that, the buzzer will make a second sound, the DC motor will rotate, the attached spring coil will push the candy bar, and the NeoPixel sticks will turn on and change colors.ย
To do that, I started by designing and simulating the circuit for the Smart Candy Box using Tinkercad. I dragged the components from the menu bar and used the L293D motor driver because there's no L298N driver in Tinkercad. The motor driver controls the DC motor's rotational direction and speed. Also, I used an Arduino UNO board to control the smart device inputs and outputs as follows:
Inputs:ย
1- Ultrasonic sensor to measure the distance between the user and the sensor, then activate the LCD screen
2- Pushbutton for the user to push it to rotate the DC motor, make a sound, and turn on the NeoPixels.
Outputs:ย
1- The LCD screen will display this sentence when activated from the ultrasonic sensor: "When you hear the beep, press to win a treat!"ย
2- The buzzer will make a beep sound, and when the user pushes the button, it'll make a second sound
3- The DC motor will rotate with the spring coil attached to it, and it'll push the candy bar out
4- ย The NeoPixel sticks will turn on, emit light, and change colors.
Then, I connected the components to my circuit and used the following pins on the Arduino:
5V pin: on the Arduino to the 5V positive channel of the breadboard
VIN pin: on the Arduino to the 9V positive channel of the breadboard (for the L298N motor driver)
GND pin: on the Arduino to the negative channel of the breadboard
Pin 6 & 7: motor driver to control the DC motor's rotational direction
Pin 3: echo ultrasonic sensor
Pin 4: trig ultrasonic sensor
Pin 2: pushbutton
Pin 9: buzzer
Pin 8: NeoPixel 8 LED strip
Pin A4: SDA of the LCD screen
Pin A5: SCL of the LCD screen
Finally, I started writing the Arduino C code to simulate some of the components in Tinkercad to make sure they were working properly.
I started implementing and breadboarding the physical circuit with an Arduino board and components. I wired the components together following my Tinkercad design. I wired the Arduino 5V pin to the positive channel of the breadboard, the VIN pin to the 9V positive channel, and the GND to the negative channel. Then, I wired the ultrasonic sensor GND, VCC, Echo, and Trig pins. Also, the pushbutton to pin 2.ย
After that, I wired the buzzer to pin 9 (positive) and 2 NeoPixels to pin 8. Then, I wired the LCD screen to the Arduino's analog pins, SDA to pin A4 and SCL to pin A5. Finally, I connected the L298D motor driver to the DC motor and Arduino pins 6 & 7.
Once the wiring for the circuit was complete, I connected the Arduino board to my laptop and continued working on the Arduino C text code using Arduino IDE.
To build the code that would be uploaded to the Arduino board, I installed 2 libraries and used their examples: the LiquidCrystal_I2C for the LCD screen and Adafruit_NeoPixel for the NeoPixel 8 LED strip. Then I defined the Arduino pins and added the ultrasonic sensor code from week 6 assignment. After that, in the void setup that runs only once at the beginning, I set the function pinMode to either input or output (the pushbutton is input_pullup). Next, in the void loop, which's a function that executes indefinitely until the power is off, I set the ultrasonic distance to 20 cm to activate the LCD screen. The LCD screen will display this sentence when activated from the ultrasonic sensor: "When you hear the beep, press to win a treat!". After that, there'll be 3 seconds delay, and the buzzer will make a beep sound. Then, if the pushbutton is pushed (low), three actions will happen simultaneously:ย
The buzzer will make a second sound with a different tone
The motor will rotate in one direction for one second
The NeoPixel 8 LED strip will emit light and change colors (red, green, yellow, blue, none) one by one.
Finally, I compiled the code and resolved the bugs, then uploaded the code to the Arduino board and it worked perfectly.
For the enclosure, I used cardboard, a glue gun, a cutter, and a bottle cap for the DC motor to attach to the coil spring. Then, I measured the parts and components, glued cardboard parts together, and mounted the components onto the box enclosure. Finally, I powered the box and it was fully functional.
I faced 2 challenges while doing the assignment. The first challenge was writing the code for the LCD screen and the ultrasonic sensor. I used an ultrasonic sensor in week 6 assignment, but the difficult part was making it an input for the LCD. Also, installing the LiquidCrystal_I2C library for the LCD was complicated and it didn't work at all on my Mac laptop. Luckily, I figured it out with trial and error and watched the tutorial below, And the LiquidCrystal_I2C library worked fine when I installed it on a Windows laptop. I made the needed adjustments, and everything worked well.
The second challenge was mounting the components into the box. There were too many wires, the box was small, and the wires kept wrapping around the coil spring when the DC motor rotated. I had to take out all the components several times to troubleshoot the wiring. Eventually, I used the Avometer continuity test to ensure connectivity in the circuit without taking everything out of the box when there was an issue. Also, I used a lot of adhesive tape to make sure everything was in place and away from the coil spring.