Champaign-Urbana Community Fab Lab
Champaign-Urbana Community Fab Lab

Drew Zelac – Arduino Intro

I already had a couple Arduino Unos and sensors lying around my apartment from an IoT class my apartment-mates and I are taking this semester and from a demo I participated in a couple years ago. I first decided to just go simple and use an ultrasound distance sensor with a multi-colored LED, but then I decided I wanted to do something more interesting.

I went with a ultrasound sensor and a Piezo speaker/buzzer to create a backup alarm for a car. The point of this device is to mimic the backup sensors in most cars in the last decade or so, but with just an Arduino and a couple cheap devices attached instead of the multi hundred dollar sensor array that is installed in the cars.

I found a tutorial online for the Piezo speaker and got that working first. I used a 120ohm resistor and plugged the Piezo speaker into GND and Pin 8. With the code from the tutorial, it worked immediately.

I then added in the ultrasound sensor. I found another tutorial online about how to wire this and some code to get it to work. For this, I attached VCC to 5V, Trig to Pin 11, Echo to Pin 12, and GND to GND. This also went together without any problems. I just combined the code for the Piezo speaker and the ultrasound sensor and it worked.

I added in some conditional statements in the code to play a different frequency sound at different intervals, depending on the distance from the ultrasound sensor to the object it is sensing. After playing around with the frequencies and delay intervals, I thought it was doing a good job or sensing objects and alerting the user of the approximate distance to the object. All that was left was to clean up the code a bit, add in some more comments like how to wire the Arduino and devices, and credit my sources.

For this week’s project, everything seemed to go well. For the setup I did with the Piezo speaker and ultrasound sensor, I don’t think there was anything to be improved with the wiring. Code can almost always be improved, so I could probably clean it up a little bit more. Since I had done this kind of work before, it was fairly simple for this week. I’m looking forward to next week where the requirements will be more challenging and I can learn more!

I then created a storyboard for my invention.

My code is below:

//The ultrasound sensor code is by the Rui Santos
/*
* created by Rui Santos, https://randomnerdtutorials.com
*
* Complete Guide for Ultrasonic Sensor HC-SR04
*
Ultrasonic sensor Pins:
VCC: +5VDC
Trig : Trigger (INPUT) – Pin11
Echo: Echo (OUTPUT) – Pin 12
GND: GND
*/

// ULRASOUND SENSOR

/*
* https://programmingelectronics.com/an-easy-way-to-make-noise-with-arduino-using-tone/
Piezo Speaker Pins:
Positive – Pin8
GND: GND
*/

int trigPin = 11; // Trigger
int echoPin = 12; // Echo
long duration, cm, inches;


// PIEZO SPEAKER

// A sketch to demonstrate the tone() function
// Specify digital pin on the Arduino that the positive lead of piezo buzzer is attached.
int piezoPin = 8;

void setup() {
// ULTRASOUND SENSOR

// Serial Port begin
Serial.begin (9600);
//Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

}

void loop() {
// ULTRASOUND SENSOR CODE BELOW

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
duration = pulseIn(echoPin, HIGH);

// Convert the time into a distance
inches = (duration/2) / 74; // Divide by 74 or multiply by 0.0135

Serial.print(inches);
Serial.println(“in”);

// PIEZO SPEAKER CODE BELOW

/* Tone needs 2 arguments, but can take three
1) Pin#
2) Frequency – this is in hertz (cycles per second) which determines the pitch of the noise made
3) Duration – how long teh tone plays
*/
if(inches < 2){
tone(piezoPin, 4500, 100);
delay(100);
}
else if(inches < 4){
tone(piezoPin, 4000, 100);
delay(300);
}
else if(inches < 8){
tone(piezoPin, 3000, 100);
delay(600);
}
else if(inches < 12){
tone(piezoPin, 2500, 100);
delay(1000);
}
else{
tone(piezoPin, 1500, 100);
delay(2000);
}

}

Tags: ,