This week’s assignment was to create a smart circuit that reads a signal from an input to control an action component but only using code blocks in tinkercad for the code . so for this assignment I created a simple circuit that has an IR that increment the 7 segment each time there it detect an object in front of it and buzzer emits sound . I wanted to use a decoder for the 7 segment( to save pins) but unfortunately I didn’t have a decoder at the time
We had to use tinkercad to simulate and create the code , I did not know previously that tinkercad had an interface where you could code using code blocks
The blocks options are divided into six criteria
- output (blue)
- is used to control an output component ( led , dc motor , servo motor , RGB, buzzer …)
· There is set the built in led which controls the small led inside the Arduino
· Set pin ( num) to either high or low
· There is also set pin ( 3,5,6,9,10,11) to ( number from 0 to 255) because those pins are PWM
· Rotate servo motor to ( ) degree usually in the degree you put map function to convert the outcome number that is between 0 to 255 to a degree between 0 to 180
· Set RGB on pins -- , --- , -- to the color you want easily
- Control
· Which is used for conditions and loops
Conditions like if / if lese
· Loops like repeat – times / repeat while
· There is also wait block that serves as a delay in the code
- Input
· Is used to read from an input component ( sensor , keypad , button …)
· Read digital if it’s connected to a digital pin ( from 2 to 13)
· Read analog if it’s connect to an analog pin from A0 to A5
· Read from serial if you are getting data from the serial monitor
Math
· Is usually used as the condition in the if statements or the while loops
The coding part
- first : the function of our circuit is to emit sound using buzzer(output) and increment the 7-segment(output) by 1 each time an object comes infront of the PIR (input) (IR IN REAL SIMUALTION)
1- get the if condition from control , from math the equal equation& the HIGH parameter , from input read digital pin beause out pir is a digital sensor connected to digital pin in arduino
2- from variable create a variable and name it a name that doesn’t start with a number or a symbol and isn’t a reserved name already has a function in coding ,
3- attach to if condition ( change x by 1 )
- The reason behind this line is to increment the variable by one each time my hand comes infront of the sensor and the displayed number would be in synchronous with the number of times I did the action .
4- get another if condition to check the counter value and based on the counter value we will make the the leds presenting the number high , also note that we will display number zero that means number zero will be displayed at counter 1 and number 1 displayed at counter two and so on
- So for the counter x =1 ( number 0) ( pin A & B & C & D&F == HIGH ) so we will set the pins representing in them in the arduino to high
First the circuit design
1- Adding the Arduino uno and the bread board
- Take the 5v pin in Arduino and connect it to the positive row and the GND pin to the negative row in the breadboard
2- Add the 7 – segment display but make sure that it’s common cathode not anode
( the difference between the two types is that common anode all the 8 leds positive leg are collected together at one pin that should be connected to VCC BUT common Cathode all the 8 leds negative leg are collected together at one pin that should be connected to GND)
3- In the picture of the 7 segment, you can see the letter representation of each pin
- Connect segment A with a 220-ohm resistor to PIN2
- Connect segment B with a 220-ohm resistor to PIN3
- Connect segment C with a 220-ohm resistor to PIN4
- Connect segment D with a 220-ohm resistor to PIN5
- Connect segment E with a 220-ohm resistor to PIN6
- Connect segment F with a 220-ohm resistor to PIN7
- Connect segment G with a 220-ohm resistor to PIN8
- CONNECT the common from one side to the GND
4- Get PIEZO from tinker cad & Connect the buzzer the positive leg (the longer one ) to pin 12 in Arduino and the negative leg to the GND
5- We will select PIR from tinkercad as a replacement for the IR that would be used in the real life simulation , but note that PIR is active HIGH while IR is active low
- Connect the negative pin to the GND
- Connect the positive pin to 5V on breadboard
- Connect signal pin to pin 11
-------------------------------------------
After connecting the circuit as explained, you click on the blocks section on the top and then choose blocks to text and after that you copy the text and paste in the Arduino software
You click on verify at the top , usually it would ask you to save you sketch with your chosen name
If there are any errors make sure to read them thoroughly because usually you can fix it easily by reading the error and know precisely which line has a problem
Then make sure from tools to select Board Arduino uno and port COMX , x is the number
Connect your Arduino to the pc using USB cable then click on upload at the top left corner
I personally like coding and I enjoy seeing how other people had reached the same goal but using different techniques , my fellow classmate used a decoder to illuminate the 7 segment so know you are using only 4 pins instead of 7 pins
In this picture you can see that
- See that the binary representation of the numbers from 0 to 9 would display the number on the 7 segment for example 0000 four inputs are zero would have a to f zero ( which is illuminated in case of anode 7 segment )
- In this picture you can see how to connect the BCD decoder to the 7 segment
I didn’t know that the IR sensor is active Low so it did take some trails to figure that out another thing is to know the resistance you are using by calculating it on resistor calculator site or using a mobile application because if you are using a very high resistance then it may cause that voltage suppield to the component isn’t enough
Also its important to know what type of 7 segment you are using because if it’s cathode that means all he negative legs of the leds are accumulated to one commen pin ( to the gnd ) and it’s active high meaning to have segment A illuminated it should be High
But if it’s commen anode then all the positive legs are accumulated in one commen pin ( vcc) and the other legs are active low means to illuminate it should be LOW
I think code blocks would be very useful as a code guide I could use it to know the servo motor functions and libraries that needs to be included , also I that I this interface is very efficient and money saving because now I can test the two servo motor together with the lcd on tinkercad and know that I would need an external power supply
this week in lab exercise was very fun to make we had to control the servo motor that rotatesthe hand first using a potiemeter using the map function is was my first time knowing / using it and then using an ultrasonic sensor and making a concition that when the object detect is on distance 5 cm or less the action happens