This week's task is to build and program a smart circuit using Arduino UNO which reads a signal from an input component to control an action component using code blocks.
I got the inspiration from the regular compass , so I decided to make a device that tells the right and left directions.
I used Tinkercad web app to design and simulate my circuit.
Electric circuit components:
Jumper wires
Arduino UNO
9V adaptor
breadboard
LCD screen
Tilt sensor
Avometer for troubleshooting.
I used Arduino IDE software to run and upload my code
Open Tinkercad and click on create tab and select circuit
Use the bar on the right to search for the components
Search for the first component "breadboard" then drag it
Then drag the rest of the components of the circuit
Breadboard
Arduino UNO
Tilt sensor
LCD screen
The input component is tilt sensor
The action component is LCD screen
I will power the Arduino using 9V adaptor and the 5V from the Arduino to power the circuit.
Connect the LCD screen VCC and GND to the 5v and GND of circuit and SDA and SCL to the Arduino analogue pins A4, A5
Now, connect the tilt sensor to power and GND and to Arduino digital pin 2 like this
Here's my circuit
Now, the coding part;
Click the Code tab
Select control menu
Drag the "if, then, else" condition to the right
From input menu select read digital pin and the pin which the sensor was connected to, it was pin 2
From Math menu select = on this tab then drop those tabs in each other then to the if condition.
From Output menu
select those tabs and drop them under "then" slot :
Adjust position on the LCD screen as shown.
Print the word "Right"
On LCD select turn off autoscrolling.
Right click here and select Duplicate
Then drop the duplicate tabs under "else" slot
Change the word printed on LCD screen to "Left"
Here's our final code blocks arrangement
Now, press "Start Simulation"
Here's the simulation video.
Now, you need to copy the code to Arduino IDE
Click on Code.
Choose Blocks+ Text
Copy that text
Here's the link to my circuit on Tinkercad: circuit link
Prepare all the electric circuit components to build the circuit.
Build the circuit as designed on Tinkercad.
Connect the Arduino board to your laptop and make sure it is selected on the port tab on Arduino IDE.
Paste the text on Arduino IDE
Click on verify then click upload
It will upload and compile if everything is correct.
When I ran the code there was no readings on the LCD screen, so something must be incorrect and it is not a compilation error.
I adjusted the code because the lcd library in the first code(Adafruit_LiquidCrystal.h) wasn't the correct one for my I2C LCD screen in my kit.
I included (LiquidCrystal_I2C.h) instead after installing it.
Adjust the code as follows:
Compile and Upload to the Arduino Board.
Upload was successful.
Connect the adaptor.
The circuit is working perfectly.
I also made a small box shaped enclosure for my circuit, mounted and fixed it using tape.
Here is the final result🎉
A challenge I faced was when I uploaded the code to the Arduino but one of the circuit components "LCD screen" didn't function as simulated on Tinkercad (No words appeared on the LCD)
So this is how I solved the problem:
First of all, it was not a syntax error as shown here. compilation was done successfully.
I then checked the electrical circuit again and again. It was connected adequately.
So what could it be 🤔
I googled the name and type of this LCD screen it was called I2C-LCD screen
The lcd library that was included by tinkercad was (Adafruit_LiquidCrystal.h)
I installed LiquidCrystal_I2C.h library and made sure it was included
I then adjusted the code so that it is compatible with my library
After compiling and uploading the code, the circuit worked perfectly.