Inspiration
MY own idea
IT will be a toy that is the car had a rgb led blinking with diffirent light colors win the sensor sense a motion and the buzzer send a voice when on-off switch is turned on.
Software
online platform to smiulate circuits and 3d designs.
Cardboard
to make the physical prototype
Breadboard
Ultrasonic sensor (INPUT)
to sense the motion so the lamp is lighting in a diffirent colors
Arduino Uno
to control the circuit
RGB LED Light RED, GREEN, Blue (OUTPUT)
to smiulate the Police car lamp.
ON-OFF Switch (INPUT)
to control the buzzer
Resistor
Buzzer (OUTPUT)
To smiulate the police car siern sound.
Slider Switch to add options to open light only or light and sound.
9V adapter
the power supply
crocodile wires
to connect the pushbutton to the jumper wires.
male-male jumper wires
to connect the components to the breadboard.
Step 1
Open Tinkercad in any browser
Step 2
Create a circuit
Step 3
Start searching for components
First, we will use breadboard
so in searching bar will wright breadboard and then drag it in workspace.
Step 4
Then add the Arduino.
Step 5
Then add the RGBLed light
it is an action component
Step 6
Add 3 Resistor each one is 220 oam
Step 7
Start to connect the Ground of the arduino to the breadboard and thenConnect the arduino 5v Pin to the breadboard and connect the ground of the LED to the arduino ground.
Step 8
1-connect the RED Terminal of LED to the Pin 3 of arduino ,
2-GREEN terminal to Pin 5,
3-BLUE terminal to Pin 6.
Step 9
REAPET the ssteps on phsiycal circuit
Add the led
2-add the 3resistors
3-connect arduino ground and 5v to breadboard
4-then connect yhe cathode terminal of the led to arduino ground.
5-connect the RED Terminal of LED to the Pin 3 of arduino ,
6-GREEN terminal to Pin 5,
6-BLUE terminal to Pin 6.
Finall led circuit
Step 10
Start the first code
ADD int
int R = 3;
int G = 5;
int B = 6;
Step 11
in void setup
pinMode (R , OUTPUT);
pinMode (G , OUTPUT);
pinMode (B , OUTPUT);
Step 12
digitalWrite(G, HIGH);
digitalWrite(B, LOW);
digitalWrite(R, LOW);
delay_ms(500); //
digitalWrite(R, HIGH);
UPLOAD THE CODE
Step 13
1-Add ultrasonic sensor
2-connectVCC terminal to arduino 5v on breadboard.
3-Connect Sensor GND to arduino ground.
4-Connect trig pin to pin9
5-Connect echo pin to pin 10on arduino.
Step 15
Then adding ultrasonic sensor control the RGB LED
on the phsiycal Circuit
Step 16
Update the code so you can control the RGB LED by the sensor.
https://www.instructables.com/Ultrasonic-Distance-Sensor-Arduino-HC-SR04/
I use some hints from the previous link
Make int for trigger pin
Make int for echo pin
Make the trigger pin output
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
Step 17
void setup()
{
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}
Define pin 3, 5 and 6 which connected to led as output pins.
Step 18
I will control the led by the sensor so the code order is
if the distance bigger than 10
make the green light high and the other light low then delay for 1000 mllsecond
then make the red light high and the other lights low and repeat the delay
then make the blue light high and the other light low
void loop()
{
if (0.01723 * readUltrasonicDistance(9, 10) > 10) {
digitalWrite(3, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
delay(100); // Wait for 1000 millisecond(s)
digitalWrite(3, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
delay(100); // Wait for 1000 millisecond(s)
digitalWrite(3, LOW);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
delay(100); // Wait for 1000 millisecond(s)
} else {
digitalWrite(3, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
delay(100); // Wait for 1000 millisecond(s)
}
}
UPLOAD THE CODE AND TEST IT
Step 19
1-On tinker cad add the buzzer
2-connect the + terminal to pin 12
3-connect - terminal to arduino ground
Step 20
1-On tinker cad add the on off switch to control the buzzer
2-connect the + terminal to pin 7
3-connect - terminal to arduino ground
Step 21
Build the phsyical circuit
-connect the + terminal to pin 12
-connect - terminal to arduino ground
add the on off switch to control the buzzer
-connect it with crcodile wires
-connect the + terminal to pin 7
-connect - terminal to arduino ground
Step 22
Build the code
https://lisha.ufsc.br/teaching/esl/exercises/buzzer.html
I use the code in pervious link with adding edits to fit the function of the product.
Step 23
The first code is from the referance the second code is which i build
1-i changed the pin numbers
2-instead of high and low I use tone to make a police car siern.
3-between each tone i use delay for 10.
Step 24
const int pinBuz = 12; //Buzzer Pin
const int Switch = 7; // Push-button pin
void setup() {
pinMode(pinBuz,OUTPUT); //Defines pinBuz as an Output
pinMode(Switch,INPUT); // Defines pinSwi as an input
}
void loop ()
if (Switch == 1) // Pressed button, logic State HIGH (5V)
{
tone(pinBuz, 123, 150); //Switch pressed, buzzer on
delay(10);
tone(pinBuz, 160, 150); //Switch pressed, buzzer on
delay(10);
tone(pinBuz, 110, 100); //Switch pressed, buzzer on
delay(10);
tone(pinBuz, 150, 100); //Switch pressed, buzzer on
delay(10);
}else
{
digitalWrite(pinBuz,0); //If the switch isn’t pressed, buzzer off.
}
}
Step 25
mix the code of controling the led by sensor with code of controlling the buzzer by on off switch to make a circuit with 2output and 2input
Step 26
Add a slider switch to make it easy to control the two opreations.
Add it on pin 13
Step 27
THE mixxed code will be
void setup()
{
pinMode(13, INPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(pinBuz,OUTPUT); //Defines pinBuz as an Output
pinMode(Switch,INPUT); // Defines pinSwi as an input
}
void loop()
{
if (digitalRead(13) == HIGH) {
if (0.01723 * readUltrasonicDistance(9, 10) <= 20) {
digitalWrite(3, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
delay(100); // Wait for 1000 millisecond(s)
digitalWrite(3, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
delay(100); // Wait for 1000 millisecond(s)
digitalWrite(3, LOW);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
delay(100); // Wait for 1000 millisecond(s)
} else {
digitalWrite(3, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
}
}
else {
int botao; // To save the last logic state of the button
botao = digitalRead(Switch); //Put the reading value of the switch on botao
Serial.println(botao); //Shows the logic state of the input on Serial Monitor
if (botao == 1) // Pressed button, logic State HIGH (5V)
{
tone(pinBuz, 123, 150); //Switch pressed, buzzer on
delay(10);
tone(pinBuz, 160, 150); //Switch pressed, buzzer on
delay(10);
tone(pinBuz, 110, 100); //Switch pressed, buzzer on
delay(10);
tone(pinBuz, 150, 100); //Switch pressed, buzzer on
delay(10);
}
else
{
digitalWrite(pinBuz,0); //If the switch isn’t pressed, buzzer off.
}
}
}
another OUTPUT and input without using slide switch by control the led light with ultrasonic sensor and control the buzzer with on-off switch
the 2actions takeplace in the same time.
below is the code and vedio.
it will be use in the same enclouser.
// C++ code
//
const int pinBuz = 12; //Buzzer Pin
const int Switch = 7; // Push-button pin
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
void setup()
{
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(pinBuz,OUTPUT); //Defines pinBuz as an Output
pinMode(Switch,INPUT); // Defines pinSwi as an input
}
void loop()
{
if (0.01723 * readUltrasonicDistance(9, 10) <= 20) {
digitalWrite(3, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
delay(100); // Wait for 1000 millisecond(s)
digitalWrite(3, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
delay(100); // Wait for 1000 millisecond(s)
digitalWrite(3, LOW);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
delay(100); // Wait for 1000 millisecond(s)
} else {
digitalWrite(3, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
}
int botao; // To save the last logic state of the button
botao = digitalRead(Switch); //Put the reading value of the switch on botao
Serial.println(botao); //Shows the logic state of the input on Serial Monitor
if (botao == 1) // Pressed button, logic State HIGH (5V)
{
tone(pinBuz, 123, 150); //Switch pressed, buzzer on
delay(10);
tone(pinBuz, 160, 150); //Switch pressed, buzzer on
delay(10);
tone(pinBuz, 110, 100); //Switch pressed, buzzer on
delay(10);
tone(pinBuz, 150, 100); //Switch pressed, buzzer on
delay(10);
}
else
{
digitalWrite(pinBuz,0); //If the switch isn’t pressed, buzzer off.
}
}
The code errors after compilling
the error was that ' ; ' at the end of ( int triggerpin, int echopin) wasn't correct so i delete it and the compiling was done.