The idea of the assignment for this week is to design and program a smart device that perform a certain function or solve a problem.
I can use program Arduino UNO to read signals from multiple input components to control multiple action components using Arduino C (Text Code).
The idea is to make a flashlight to people who afraid from darkness. The flashlight should take two inputs: opening the switch and reading from ultrasonic sensor. The device's outputs should be producing sound from the buzzer and light the bulb.
Simulating program
Coding program
9V adapter
Jumper wires
Light bulb
3 Resistors 220 ohm
Arduino uno and its cable
Buzzer
Ultrasonic sensor
On/Off switch
Two channels relay
Breadboard
First I connected the ultrasonic with the buzzer and tested them together.
Second I connected the relay to the circuit to test the lamp, sensor and buzzer.
The inputs are opening the flashlight with a switch and the distance for ultrasonic to read.
The outputs are the buzzer produced light and the light bulb will open if the distance less than 15 cm.
Testing the buzzer and the sensor.
The simulation for the buzzer and the sensor.
First I set the code to test the ultrasonic sensor. I set that the buzzer will work if the distance is 15 or less than.
Code for trial.
Code for trial.
The circuit with the relay.
Second I added the code for the relay to open the 9 volt battery. When the distance is 15 cm or less the buzzer will produce alarming sound and the bulb will be fubctioned.
Code for testing the relay.
Code for testing the relay.
The circuit has many connections. First the connection of the ultrasonic sensor. The VCC of the sensor is connected to the 5V pin on arduino, trig pin is connected on pin 8 on arduino and echo pin is connected to pin 9 on arduino.
The relay connections are input 1 at the pin 7 on arduino and the other connection is with the bulb connected to 9V adapter. As the bulb works on 9V and arduino only supply 5V.
Last thing the connection of the buzzer. The buzzer is connected on pin 11. All the grounds of the components are connected to a common ground on the breadboard and this common ground is connected to teh arduino.
The whole cicuit.
Last thing to add was the switch. The switch is added to make the flashlight work. I put the condition of the ultrasonic and the bulb in if condition and named a variable 'switch_variable' to control the on/off operation of the flashlight.
The code was composed from 3 main parts:
1) The definitions parts:
This part consists of some constants to ease the coding part to catch the errors quickly without checking the all the connections. The definitions were for the trig and echo pins, relay, switch, buzzer and the variables required for the ultrasonic sensor like duration, distance and safetyDistance.
2) void setup:
The void setup part consists of setting each pin as input or output, like trig, echo, relay and buzzer are output, input, output and output respectively.
3) void loop:
Consists of the conditions and how the code should run. the condition was if the distance is <= 15 cm the relay should operate and the bulb should work and the buzzer should produce sound. else if the distance > 15 cm the relay should stop operating and the buzzer is shut down.
The code.
The code.
I followed the technique of divide and conquer.
First I tested the code of the ultrasonic sensor and the buzzer to make sure from the functionality of them.
The circuit has many connections. First the connection of the ultrasonic sensor. The VCC of the sensor is connected to the 5V pin on arduino, trig pin is connected on pin 8 on arduino and echo pin is connected to pin 9 on arduino.
The relay has many connections. The connections are two types: connections with arduino take 5V and connections with the 220V AC. The NO pin and the CM pin are connected with the bulb and the 220V AC. The connetions of the arduino are Vin connected with 5V on the arduino, GND connected to the ground pin on arduino and the IN pin is connected to pin 7 arduino to make the relay do its function.
Last thing the connection of the buzzer. The buzzer is connected on pin 11. All the grounds of the components are connected to a common ground on the breadboard and this common ground is connected to teh arduino.
Ultrasonic and buzzer circuit.
Ultrasonic and buzzer circuit video.
The relay is added to the circuit because the power from the arduino is not enough to power the bulb. So the circuit is fully functioned at this level when the condition came true.
The circuit with the relay.
The circuit video.
Last thing was adding the switch. The switch controls the operating of the flashlight. I added it on the arduino and set the code to make the flashlight to work when the on/off switch is opened.
The whole circuit.
The circuit video.
Enclosure time
6 sides 12*12 cm
Mounting the components.
Mounting the components.
Mounting components at the box.
Final product.
Final product.
There are many code obstacles that faced me in the circuit.
First the code of the ultrasonic sensor. It was made to produce sound if the distance >=15 cm but what I want was if the distance is less than 15 cm the buzzer should operate so I edited in the code.
The wrong logic for the code for ultrasonic.
The right logic for the code for ultrasonic.
Second it was the code of the relay. It was the same as the ultrasonic sensor so I edited it. This part was try and error so when I understand the right mode to make the relay work. The relay was set to be not working when the distance <= 15 cm so the code is edited to correct the logic. The relay is set to open when the distance is less than 15 cm.
The wrong logic for the code for relay.
The right logic for the code for relay.
I learned how to solve the codes problems and to troubleshoot to know where the problem is.
I have learnt the concept of house automation. I will never forget our happiness when the project is done.
This week we made a simple model of house automation.
We make a self lighting lamp. When it is too dark, the sensor lights it.
Photo for the circuit.
Video for the circuit wehn it is done.