Stoplight and Parking Aid

by kaufy59 in Circuits > Arduino

64 Views, 1 Favorites, 0 Comments

Stoplight and Parking Aid

Capture.JPG

This is a beginner level project to make a stop light/ parking sensor.

Supplies

  1. 1x red LED
  2. 1x green LED
  3. 1x yellow LED
  4. 1x Arduino Atmege 2560
  5. 1x buzzer
  6. 1x ultrasonic sensor
  7. 1x switch
  8. 20x wires
  9. box of resistors
  10. 1x breadboard

Getting Started

Gather your components

In this tutorial project, we will construct a functional stoplight and parking aid sensor. To get started, we will need to select the components that we want to include in the project. In your kit, please locate the following:

  1. 1x red LED
  2. 1x green LED
  3. 1x yellow LED
  4. 1x Arduino Atmege 2560
  5. 1x buzzer
  6. 1x ultrasonic sensor
  7. 1x switch
  8. 20x wires
  9. box of resistors
  10. 1x breadboard

Once you have found these components, neatly set them out on a nonstatic conductive surface to prevent charge build-up. Keep the resistors in the box until calculations are complete. 

Getting to Know Your Breadboard

CaptureA.JPG

Getting to know your breadboard

The breadboard can be intimidating to look at for those who do not have prior experience with electronics and hardware design. This section will go over a brief summary of how the breadboard works so that you can understand what you are building.

This is the breadboard that you have with you in your kit. There are many varieties of breadboards, but this one is the most common one you will use if you pursue a future in electrical or computer engineering. The red lines that you see above some lines of pin holes are there to label your power buses. Each power bus is separate, so on this board, you have 4 different power buses, one in each corner of the board. It is called a bus because each pin hole in the bus is connected. This means that if you connected a pin to one of the holes in the bus with say 5 volts of power, you could then use the other pin holes in that bus to distribute those 5 volts somewhere else. The blue lines with the minus sign directly underneath some pin holes mark your ground buses. Like the power buses you have 4 ground buses, one in each corner. Again, like the power buses, all of these pin holes are connected in each bus. This means that you can ground all of the components that need to be grounded to a single ground bus. If you ground components to different buses, you could cause your circuit to not work properly so if you would like to use more than one bus for the same function, you can bridge the buses by connecting them with wires. Now for the center of the breadboard, you can see that it is split down the middle longways. This is important because this means that the top half of the board and the bottom half are not connected to each other. If you look at the left-hand side, you will notice the letters A-E and F-J. In that first column of pinholes, the pins lined up with A-E are in the same bus and the pins lined up with F-J are in the same bus, but remember, A-E and F-J are separate buses. Each column of pin holes on this breadboard is its own bus. This will be helpful to know for building the circuit later on. 




Calculations

In this section, we will teach you what calculations to perform to determine the resistor values you need for your circuit to minimize current. Current is basically how fast the electricity is flowing through the circuit. In most circuits, if there is too much current, the components will be damaged, while too little current will cause the circuit to either run poorly or not at all. Too much current can also be dangerous for people too, as it can cause injury. This is why resistance and resistors are important. The LEDs in this circuit can only handle a maximum of 25 mA(milliamps), which is 0.025 Amps of current. To calculate this, you will rearrange the equation below. 

V = I x R


This is your voltage equation which is I (current) times R(resistance). We already know the voltage that can be output by your Arduino, and that is 5V (Volts). R is your resistance value, and I is your current value. Since you have your voltage and current values, you will rearrange the equation to look like this. 

R = V/I

This is your resistance equation, which is V(voltage) divided by I(current). This is what it looks like with the numbers plugged in. 

R = 5 / 0.025

R = 200 ohms

Ohms are the unit of resistance, also commonly shown as the Greek symbol Ω (Omega). Since you do not have any 200 Ω resistors in your kit, you will use a 220 Ω resistor. This is the resistor with the Red, Red, Brown, and Gold color code on it. Use the card in your kit to help you find various resistor values for future projects. Now that you have one resistor for each LED, you are ready to construct your circuit!



Building the Circuit

CaptureB.JPG
CaptureC.JPG
CaptureD.JPG
CaptureE.JPG

This is where the fun begins as you start to put your circuit together. Remember to keep everything off of a static conductive surface as you build it to avoid accidentally adding a static charge to the circuit. 


  1. On one half of your breadboard, place the LEDs into the 2 adjacent pinholes on separate buses like in the figure below. Remember, the positive end of the LED is the longer pin, and the negative end is the shorter pin. This is important as it affects the circuit. 
  2. Next, you will connect power, ground, and the arduino output pins as well as the current regulating resistors to the breadboard. This will be shown in the figure below. 
  3. Now, you will connect the buzzer and the ultrasonic sensor, as shown in the next figure. You will also bridge the power and ground on all of the buses. The pins you are plugging the components into on the Arduino will make more sense once you get to the coding segment. 
  4. Lastly, you will add the control switch and connect it to the Arduino. This is shown in the figure below. 

Great! Now that your circuit is all set up it's onto the coding segment. 



Arduino Code

Arduino Code
With the circuit fully built and your introduction to electronic hardware out of the way, it’s time to introduce you to the software side of things. Below is a fully functional code for the stop light and parking sensor that can be toggled via the switch you installed. Each line has been commented on with an explanation for its use and functionality.

//Defined Variables
#define RED_pin 9 //This gives the variable “RED_pin” the value 9
#define GREEN_pin 11 //This gives the variable “GREEN_pin” the value 11
#define YELLOW_pin 12 //This gives the variable “YELLOW_pin” the value 12
#define buzzer_pin 10 //This gives the variable “buzzer_pin” the value 10
#define ultrasonicsensor_pin 7  //This gives the variable “ultrasonicsensor_pin” the value 7
#define ultra_echo 8 //This gives the variable “ultra_echo” the value 8
#define switch_pin 6 //This gives the variable “switch_pin” the value 6

//Functions
//This is the function to initialize all of the pins in the arduino that are being used as either input pins or output pins. 
void setup()
{
Serial.begin(9600);

//This is the processing speed for the arduino. You will learn more about this later in your electronic career. It will be called the Baud rate. 

//Pins set as OUTPUT can send out anything from signals to power to data.
//Pins set as INPUT usually only receive data or signals
pinMode(RED_pin, OUTPUT);
pinMode(GREEN_pin, OUTPUT);
pinMode(YELLOW_pin, OUTPUT);
pinMode(buzzer_pin, OUTPUT);
pinMode(switch_pin, INPUT);
pinMode(ultrasonicsensor_pin, OUTPUT);
pinMode(ultra_echo, INPUT);
}

//This function has the stop light work in a looping fashion, much like they do in real life. It also has the ability to act as a parking sensor.
void loop() 
{
//PINS set to LOW are turned off while PINS set to HIGH are turned on
digitalWrite(RED_pin, LOW);
digitalWrite(GREEN_pin, LOW);
digitalWrite(YELLOW_pin, LOW);

// the byte readValue function reads whether the switch is toggled on or off
byte readValue = digitalRead(switch_pin);

//declares duration and inches as separate long variables
// the long variable type allows for decimal computation in coding
long duration, inches;
//declares ledNumber as an integer
int ledNumber;

//while loops function by performing certain duties “while” a certain parameter is true. In this case if the switch value read by readValue is High aka On then the duties in this while loop will be performed. 
while (readValue == HIGH)
{
digitalWrite(GREEN_pin, LOW);
digitalWrite(YELLOW_pin, LOW);
digitalWrite(RED_pin, HIGH);
//the delay functions are automatically set to milliseconds so this would be 2 seconds after conversion
delay(2000);
digitalWrite(RED_pin, LOW);
digitalWrite(YELLOW_pin, LOW);
digitalWrite(GREEN_pin, HIGH);
delay(2000);
digitalWrite(RED_pin, LOW);
digitalWrite(GREEN_pin, LOW);
digitalWrite(YELLOW_pin, HIGH);
delay(2000);
digitalWrite(GREEN_pin, LOW);
digitalWrite(YELLOW_pin, LOW);
digitalWrite(RED_pin, HIGH);
readValue = digitalRead(switch_pin);
}

//this while loop kicks in if the switch is turned off
while (readValue == LOW)
{
//This block of code turns the ultrasonic sensor on and off very quickly or sends out “pulses”. This is similar to how bats and dolphins use echolocation.
digitalWrite(ultrasonicsensor_pin, LOW);
delayMicroseconds(2);
digitalWrite(ultrasonicsensor_pin, HIGH);
delayMicroseconds(10);
digitalWrite(ultrasonicsensor_pin, LOW);

//the duration variable measures how long it takes for the pulse to bounce back to the sensor
duration = pulseIn(ultra_echo, HIGH);
// converts the duration into how far away in inches the object is
inches = duration /74 / 2;
//this function will print out the value and show it to you in the arduino ide terminal
Serial.println(inches);
delay(100);

//If the object is over a foot away it will only show the green light
if (inches >= 12 )
{
digitalWrite(RED_pin, LOW);
digitalWrite(YELLOW_pin, LOW);
digitalWrite(GREEN_pin, HIGH);
delay(100);
}

//Between 12 and 8 inches the light will shine yellow
else if (inches >= 8 )
{
digitalWrite(GREEN_pin, LOW);
digitalWrite(RED_pin, LOW);
digitalWrite(YELLOW_pin, HIGH);
delay(100);
}

//Between 8 and 4 inches the light will shine red
else if (inches >= 4 )
{
digitalWrite(YELLOW_pin, LOW);
digitalWrite(GREEN_pin, LOW);
digitalWrite(RED_pin, HIGH);
delay(100);
}


// Under 4 inches and the buzzer will continually sound while the red light shines
while (inches < 4)
{
digitalWrite(YELLOW_pin, LOW);
digitalWrite(GREEN_pin, LOW);
digitalWrite(RED_pin, HIGH);

// Send 1KHz sound signal for 1 second
tone(buzzer_pin, 1000); 
digitalWrite(ultrasonicsensor_pin, LOW);
delayMicroseconds(2);
digitalWrite(ultrasonicsensor_pin, HIGH);
delayMicroseconds(10);
digitalWrite(ultrasonicsensor_pin, LOW);
duration = pulseIn(ultra_echo, HIGH);
inches = duration /74 / 2;
Serial.println(inches);
delay(100);
}

//The no sound from the buzzer until the sensor reads under 4 inches
noTone(buzzer_pin); 
delay(100); // ...for 1sec
readValue = digitalRead(switch_pin);
}
}