Tinkering with microcontrollers has been on my “to try” list for quite some time. I recently stumbled upon Creation Crate. Creation Crate is a monthly tech education subscription box. Each month you receive a Arduino UNO R3 and parts to complete a project. The instruction book includes the source code for the project. The source code is licensed under the GNU General Public License. Each month the projects increase in difficulty. I thought Creation Crate would be a wonderful way to get my feet with wet with Arduino and programming electronics.
I was on the fence about signing up for the subscription box because I was not sure what to expect. I intend to write a blog post for each Creation Crate I receive in case there is a reader out there on the fence like I was.
The Project
The project for month 3 is a distance detector! With the help of an ultrasonic sensor, the Arduino will provide visual (LEDs) and audio (buzzer) indicators of how away an object is.
What’s in the Box
Inside of month three’s box we have:
- Arduino UNO R3
- Breadboard
- USB Cable
- LEDs (red, white, yellow and green)
- Resistors
- Jumper wires
- Buzzer
- Ultrasonic sensor
Building the Hardware
Assembling the project was pretty straightfoward. However, there is a fun twist in this month’s project instructions. Instead of providing a complete guide on how to wire up the project, the final steps on where to plug wires are omitted from the book. Based on the source code, you are encouraged to figure out how to assemble the rest of the project. (If you do get stuck there’s a way to see the complete diagram.)
Programming
The source code for the project is included in the instruction book. The sketch to implement the distance detector is fairly simple.
// License: GNU General Public License | |
// Creation Crate Month 3 - Distance Detector | |
#define red2 13 | |
#define red1 12 | |
#define yellow2 11 | |
#define yellow1 10 | |
#define white2 9 | |
#define white1 8 | |
#define buzzer 3 | |
#define trigPin 7 | |
#define echoPin 6 | |
int sound = 250; | |
long duration, distance; | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(9600); | |
pinMode(trigPin, OUTPUT); | |
pinMode(echoPin, INPUT); | |
pinMode(white1, OUTPUT); | |
pinMode(white2, OUTPUT); | |
pinMode(yellow1, OUTPUT); | |
pinMode(yellow2, OUTPUT); | |
pinMode(red1, OUTPUT); | |
pinMode(red2, OUTPUT); | |
pinMode(buzzer, OUTPUT); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
// send out pulse waves for measuring the distance of an object | |
digitalWrite(trigPin, LOW); | |
delayMicroseconds(2); | |
digitalWrite(trigPin, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(trigPin, LOW); | |
// the amount of time it took for the pulse to return | |
duration = pulseIn(echoPin, HIGH); | |
// distance in cm | |
distance = duration / 58.2; //(duration / 2) / 29.1; | |
// manipulate leds based on distance calculated | |
if (distance <= 40) { | |
digitalWrite(white1, HIGH); | |
sound = 900; | |
} | |
else { | |
digitalWrite(white1, LOW); | |
} | |
if (distance < 30) { | |
digitalWrite(white2, HIGH); | |
sound = 1000; | |
} | |
else { | |
digitalWrite(white2, LOW); | |
} | |
if (distance < 20) { | |
digitalWrite(yellow1, HIGH); | |
sound = 1100; | |
} | |
else { | |
digitalWrite(yellow1, LOW); | |
} | |
if (distance < 15) { | |
digitalWrite(yellow2, HIGH); | |
sound = 1200; | |
} | |
else { | |
digitalWrite(yellow2, LOW); | |
} | |
if (distance < 10) { | |
digitalWrite(red1, HIGH); | |
sound = 1300; | |
} | |
else { | |
digitalWrite(red1, LOW); | |
} | |
if (distance < 5) { | |
digitalWrite(red2, HIGH); | |
sound = 1400; | |
} | |
else { | |
digitalWrite(red2, LOW); | |
} | |
Serial.print("Duration: "); | |
Serial.print(duration); | |
Serial.print(", "); | |
Serial.print("Distance: "); | |
Serial.print(distance); | |
Serial.print(", "); | |
// if object is too far, turn buzzer off | |
if (distance > 40 || distance <= 0) { | |
Serial.println("Out of range"); | |
noTone(buzzer); | |
} | |
else { | |
Serial.println("in"); | |
tone(buzzer, sound); | |
} | |
delay(500); | |
} |
Final Thoughts
The Distance Detector is not my favorite project, but it was neat to finally play around with an ultrasonic sensor.
Christie says
Thank you for reviewing and doing a great job explaining the Creation Crate projects. I just subscribed this past month and am glad your post are here of the kits to help me as I get a new one each month. Many thanks!
Ashley says
Thank you for your comment. I was wondering if these posts were useful 🙂