Arduino Programming

Objectives:

Be able to Interface Input and Output devices such as Potentiometer, LDR, LEDs (to perform something like fade), DC motor connection to a switch to the Arduino Board's Microcontroller.

------------------------------------------------------------------------------------------------------------

Task 1. Input devices

a.     Interface a Potentiometer Analog Input to Maker UNO board and measure its signal in serial monitor Arduino IDE

Step 1. Open TinkerCad

Type ‘tinkercad.com’ in your web browser and sign in using your Autodesk fusion360 account (your ichat email account). à  Select "circuits" and "create new circuit" to start creating your simulation.



This is what you should see:



 

Step 2: On your right hand side, where icons of components are shown e.g. LED etc, Click and Drag on:

-          BreadBoard

-          Maker Uno Board

-          1 LED

-          1 Resistor

-          Potentiometer

As these are the components we will be using. This is what you should see:



 

Step 2: Connect the 5V and GND of the Arduino board to the breadboard.



Step 3: Connect the LED anywhere on the middle portion of the breadboard à the resistor connect one end to the LED and one end to (‘-‘ GND).



Step 4: Connect the Anode of the LED to PIN 13 as an Output.



 

Step 5: Connect the Potentiometer anywhere on the middle part of the breadboard à Middle PIN connect to Analog Pin A0 on Arduino Board à Rightmost PIN connect anywhere on Positive Row (5V) of breadboard à Leftmost PIN connect anywhere on Negative Row (GND) of breadboard.


 


 Now Click ‘Code’ which should show ‘Blocks’ à Delete any existing blocks shown into the bin on bottom right.



Step 6: Start Adding Blocks to form the code by: Clicking ‘Variables’ à Click ‘Create Variable’à in the box that pops up type ‘sensorValue’ à it will show in the available blocks.



Step 7: Drag out a ‘Set’ block



Step 8: Click ‘Input’ à Drag ‘read analog pin A0’ block and place below the earlier block in Step 7.



Step 9: Click ‘Output’ à Select ‘ set built-in LED HIGH’ block and place below the earlier block in Step 8.



Step 10: Click ‘Control’ à Drag out a ‘Wait’ block and place below the earlier block in Step 9.



Step 11: Click ‘Variables’ à Select ‘sensorValue’ block and place inside ‘Wait’ block in Step 10à Change seconds to milliseconds



Step 12: Click ‘Output’ à Select ‘set built-in LED HIGH’ block à Drag below all the blocks from earlier à Change ‘High’ to ‘Low’



 

Your blocks should look like this:



Step 13: Start Stimulation to test out of your circuit works.



 My Tinkercad Stimulation:



Step 14: Download the code and open it in your Arduino IDE software.


Step 15: Modify the Code as follows to configure the Board to a serial monitor in Ardunio IDE:

 Check and upload the code

Step 16: Open the serial monitor upon connecting the Arduino board to your laptop as the code is running and as you turn the knob of the potentiometer, notice how the signal in the serial monitor changes: (increases as you turn clockwise and decreases as you turn anticlockwise)


*Note: To connect the various components physically, just refer to the tinkercad stimulation you did earlier and follow exactly the same.

Videos of Serial Monitor: https://drive.google.com/file/d/1h9ZGWA8oiIdZNLfhlmzSPzoDQ4UQXJGq/view?usp=sharing

Videos of the Circuit: https://drive.google.com/file/d/1h51-HsVPPTpvqA5ldpaEvtoxOeGr_FTH/view?usp=sharing

Code: https://drive.google.com/file/d/1PNd-SYUwEmG2mjuAoEuoUGvCytr4AKqV/view?usp=sharing

Modified Code: https://drive.google.com/file/d/1PLCDQUce0KWzEa0Dt-lrYvgVROsqetW5/view?usp=sharing


b. Interface a LDR to maker UNO board and measure its signal in serial monitor Arduino IDE

Step 1. Open TinkerCad

Type ‘tinkercad.com’ in your web browser and sign in using your Autodesk fusion360 account (your ichat email account). à  Select "circuits" and "create new circuit" to start creating your simulation.



This is what you should see:



 

Step 2: On your right hand side, where icons of components are shown e.g. LED etc, Click and Drag on:

-          BreadBoard

-          Maker Uno Board

-          1 LED

-          2 Resistors

-          Light Dependent Resistor

As these are the components we will be using. This is what you should see:



 

Step 3: Connect the 5V and GND wires to the breadboard



 

Step 4: Connect the LED to anywhere on the middle part of the breadboard à Connect one end of the resistor to the LED, the other end to the PIN you want to set the LED as an output to on the Arduino Board in this case I chose Pin 9.



 

Step 5: Add the LDR to anywhere on the middle of the breadboard à Connect Terminal 1 to 5V power on the positive row of the Breadboardà Connect Terminal 2 to A0 analog INPUT pin


Step 6: Add a resistor (Set resistance to 4.7k Ohms) to Terminal 2 of the LDR à Connect one end to GND negative row on the Breadboard.



 

Step 7: Now click ‘Code’ which would show ‘Blocks’ à Delete all blocks shown currently by dragging them into the bottom right bin. This is what you should see:



 

Step 8: Click ‘Variables’ à Click ‘Create variable’ à Type in the box that appears ‘photosensor’ à drag ‘set photosensor to..’ block to the empty space:



 

Step 9: Click ‘Input’ à Drag ‘read analog pin A0’ block into the earlier Variable block:







Step 10: Click ‘Output’ à Drag ‘print to serial monitor…’ block and release below the block ‘set photosensor..’ like so:



 

Step 11: Click ‘Variables’ à Drag ‘Photosensor’ block and drop into the ‘Print to serial monitor’ block like so:



 

Step 12: Click ‘Output’ à Drag ‘set pin.. to 0’ block and release below the earlier ‘print to serial monitor.. block’ (make sure to change Pin number to the Pin you configured your LED at i.e. for me PIN 9)



Step 13: Click ‘Math’ à Drag ‘map…’ block into the earlier Output ‘set pin 9..’ blockà Change the range such that it will be 0 to 255.



 

 

Step 14: Click ‘Variables’ à Drag ‘photosensor’ block into the GREEN MATH block which should lie in the BLUE output ‘set pin 9..’ block from earlier steps.



 

Step 15: Click ‘Control’ à Drag the ‘wait..’ block below the BLUE OUTPUT ‘set pin 9..’ block and change seconds to ‘milliseconds’.



 

Overall your blocks should look like this:



 

Then you can press the ‘start stimulation’ button and test out your circuit.



Here is my Tinkercad stimulation: 



Step 16: Now you can download your Arduino Code and open it in Arduino IDE


Step 17: Check and Upload your code to your Arduino Board à see the LED light up with the LDR in action à Click on the Serial Monitor in Arduino IDE to see the signal of the LDR (see green circle)


*Note: How I set up my board was exactly the same as the virtual TinkerCad Stimulation, just follow.

Images of Circuit:

Video of the Circuit + with Serial Monitor’s signals shown: https://drive.google.com/file/d/1hB13zzVj8YPI-ADJ0VQ0zF6hbCTx024S/view?usp=sharing


2. Output Devices

a. Interface 3 LEDs (Red, Yellow, Green) to maker UNO board and program it to perform something (fade or flash etc)

As LEDs can potentially fuse if connected wrongly, it is important to test out our code on TinkerCad Stimulator.

Step 1: Open TinkerCad

Type ‘tinkercad.com’ in your web browser and sign in using your Autodesk fusion360 account (your ichat email account). à  Select "circuits" and "create new circuit" to start creating your simulation.


This is what you should see:



Step 2: On your right hand side, where icons of components are shown e.g. LED etc, Click and Drag on:

-          BreadBoard

-          Maker Uno Board

-          3 LEDS

-          3 Resistors

As these are the components we will be using.

This is what you should see:


 

Step 3: On the Arduino Board, Click on ‘5V’ and connect it to the Positive pin of the Breadboard.

Step 4: On the Arduino Board, Click on ‘GND’ (ground) and connect it to the Negative pin of the Breadboard.


Step 5: To tidy up our Circuit thus far, to drag out the wires so that they don’t obstruct the way, double click on the wires and drag them out to bend them accordingly.

Step 6: To change the colour of our wires because typically 5V(red), GND (black), Click on a wire à Click the top bar Colour option à Select red.


So far, your circuit should look like this:

Step 7: Connecting the LEDs. Drag the LEDs and place them anywhere on the middle section of the breadboard (making sure the anode i.e Longer wire of the LED) is placed after the cathode:


Step 8: Connect the resistors (set resistance to 220 ohms) to the LEDs on the breadboard:

Ensure one end of the resistor is connected to the LED and the other end is connected to the GND    (‘-‘    :ground) on the breadboard.


Step 9: Connect the Anode of the LEDs to the various PINS on the LED board with ‘~’ e.g. PIN 9, PIN 6, PIN 3 as this will set the LED as an OUTPUT and these PINS can perform functions like in this case, Fading.


Step 10: Now Click ‘Code’ which should show ‘Blocks’


Step 11: Delete all the current Blocks we have by dragging them into the bin

Step 12: Click ‘Control’ à Click and drag the Block ‘Count up to …’


Step 13: Set ‘Count up by 5 for ‘brightness’ from 0 to 255



This is what you should see for the block eventually:


Step 14: Add a Output block by Clicking ‘Output’ à ‘Set pin…’ Block à Drag into the previous command block. Change the PIN number to the PIN you are connecting the LED to on the Arduino Board i.e. PIN 9 for the first LED. This is what you should see:


Step 15: Set the Output Block i.e. ‘Set pin 9 to …’ the variable i.e. Brightness by à Clicking Variables à Select Brightness block à Drag into the Output block this is what you should see:


Step 16: Add another Control Block i.e. ‘Wait …’ by : Click ‘Control’ à Select ‘Wait..’ Block à Drag to below the Output Block à set the duration to  ‘30 milliseconds’


Now we have done the setting for PIN 9 to fade up. But to see the effect of it fading, there must be a Fade down.

Step 17: Right Click on the Blocks à Click ‘Duplicate’ 

This is what you should see:


Step 18: Change ‘Count UP’ à ‘Count DOWN’


This is what you should see:


Now we have completed configuring for one PIN and one LED. Repeat Steps 12-18 twice and change the PIN to 5 and 3 respectively for each time.

Once done, this is what you should see:


Step 19: Now that we have set the codes,  On the top right, Click ‘Start Stimulation’ to see if it works.


Here is my tinkercad stimulation:


Step 20: Now we can Click ‘Code’ à ‘Block + Text’ à Download the Arduino Code


Step 21: Open the Arduino File, Check it and upload to your board.


Step 22: Connect the LEDs to the Arduino Board by following exactly how the components are connected in the virtual TinkerCad Stimulation which you have done earlier like so:


Arduino Code: https://drive.google.com/file/d/1xUavIT_i6LJPMhzQHOEol8HOcSCDE-sV/view?usp=sharing

Video of the 3 LED fading: https://drive.google.com/file/d/1h2hKsaVH2lDGZuXL8p9vetz6ERBMwu68/view?usp=sharing


b. Interface the DC motor to maker UNO board and program it to on and off using push button on the board


Step 1: Open TInkerCad --> drag out and select the following components to use for building our circuit:
1. Breadboard
2. Arduino board
3. DC motor
4. Resistor
5. Push Button


2. Connect the arduino board to the breadboard by connecting the GND of the power to the negative terminal of the breadboard

Step 3: Insert the push button onto the breadboard then connect terminal 1b to the wire and remember to add a resistor when building the circuit to perform cross-over of the circuit: (see red lines)

Step 4: Build the connection to the DC Motor by connecting Terminal 2B of the push button to the power supply on the board (5V port) and the DC motor terminal --> this creates a switch for the DC Motor to turn on or off:


Step 5: Next build in the connection for terminal 1A and the direct pins on the arduino board, connection between the digital pin and another terminal of the motor: (see light blue wires)


This is what the final circuit should look like ^^^^

Step 6: Programming the components via code: I planned to program the button such that when the button is turned on (pushed), the motor will rotate, if the button is not pushed, the motor will stop rotating. Hence by programming it as such we will let the software know that we are programming both components as input, and the button is connected to PIN 13 while the motor is set to PIN 7:


Step 7: Then, now that we have defined the connection of the components, we need to SET UP their actions i.e. the button being an INPUT and the MOTOR being the output --> when the button is pushed (input), the motor rotates (output), we must define them under 'Void Setup':

Step 8: Enable arduino software to differentiate the programmable button such that it can detect if the button is pushed (on) or not (off) --> use 'buttonState' for this as our variable under 'Void Loop'

Step 9: Hence, when no one presses the button i.e 1, if someone presses the button i.e 0. Hence the button is programmed such that when the button is ON, the motor WILL ROTATE when the signal is 'low' the motor is OFF. If someone presses the button, buttonState variable turns to 0 the motor turns ON. 
Step 10: Copy this code into Arduino IDE, check and upload the code. Then set up the DC motor to the arduino board and breadboard exactly the way you constructed it virtually and see it work!

Tinkercad:
 

Link to code: https://drive.google.com/file/d/12xqPwKCfqTMCiEPNoVMr_Zt5YWJumLcJ/view?usp=sharing

Link to video of it working: 
https://drive.google.com/file/d/1AAwWQOZsTMoofQkxp9isKqPzuqHJmqwy/view?usp=sharing






--------------------------------------------------------------------------------------------------------------

Interfacing in general refers to the process of connecting a Sensor or Actuator to a microcontroller is called Interfacing. Input devices are often called Sensors. In the maker-uno board and kit, the input devices are button on the board, LDR and potentiometer. Output devices are often called Actuators. In the maker-uno board and kit, the output devices are LED on the board, buzzer on the board, LED (Red, Yellow, Green),DC motor. Microcontrollers (e.g., on Arduino Board) receive inputs, processes the information received and produced the desired output accurately. 


What I've learnt from Interfacing an input device:

-  In order to configure the Input devices, one needs to connect one end of the Input Device to an Analog Pin and A0 is the default. 

- Resistors should have one end connected to the device in use and the other end to the GND (ground)

For the Potentiometer, it is a variable resistor with three pins. The variable resistor is easier to be adjusted by turning the know clockwise or anti-clockwise. When connecting the Potentiometer on the Breadboard, the center pin has to be connected to Analog Input Pin A0 in order to configure it as an Input object on the Arduino Board. The leftmost pin should be connected to GND (ground) and the rightmost pin should be connected to a Power i.e. 5V. When the knob is turned clockwise, the signal on the serial monitor increases vice versa. 

For the LDR, Light dependent resistor (LDR) or photoresistor is a resistor whose resistance decreases with increasing incident light intensity. It can also be referred to as a photoconductor. When connecting an LDR on the breadboard, one end has to be connected to the power source (5V) and the other end has to be connected Analog Input Pin A0 in order to configure it as an Input object on the Arduino Board. The signal on the serial monitor increases when the surroundings are darker vice-versa.


What I've learnt from Interfacing an output device:

For LEDs, I've learnt that it is necessary to have a resistor in the circuit if not it will fuse as Resistor is an electronic component that limit the electrical energy in a circuit and will make voltage and current changes as a results. However since Resistor does not have any polarity they can be connected with their stripes facing any order it wouldnt affect the circuit. The way one connects the LED matters if not it wouldnt light up no matter how long you stare at it because LED has polarity which is POSITIVE(+) and NEGATIVE(-). Positive pin must be connected to the power source (eg : 5V), while negative pin must be connected to the ground. 

Problems experienced:
- As I forgot that LEDs have polarity, i assumed that it would work no matter how i positioned it on the breadboard and i waited for close to 30 minutes frustrated on why my circuit wouldnt work and only after googling online i realised the problem. 

- My LEDs would fade one after the other consecutively and i realised it was because the delay i set was 0 for the interval between each LED so in a millisecond (very quickly) the other two just lit up and thats it. Moreover initially as i followed the video tutorial in a bit to hasten the process i listened to it on 2x speed and hence i missed out that we had to duplicate the blocks in the tinkercad stimulation for the other 2 LEDs and so only one LED was programmed to fade and the other two didnt which only led me to realise why some time later and to rectify the issue.

- For the Potentiometer, i didnt know how to connect it via the Tinkercad stimulation to the serial monitor and and hence i was stressing out about it because this was one of the delieverables for task 1 however soon i realised, i had used a serial monitor in the Pre-Prac assignment tasks before so i referred to my earlier documentation and modified the code from Tinkercad to include the serial monitor which eventually worked. 

Reflections:

Throughout of Week 4 and 5, we have been focusing on Arduino Programming. Whereby the flow was first we were exposed to the software itself and had to complete 4 tasks to get more familiarised with the various Output devices eg., LED, how to code them, and interpret the codes in order to modify them according to our needs. We then documented these information and the processes such that we would be able to refer to them later for our competency test and for the practical as we were still freshly exposed to the software so it was safe to have a guide to fall back to. Then came the practical whereby we had the opportunity to apply the insights we gained from using the software for the pre-practical tasks and had to make the unicorn's wings flap. So we modified the Servo code we learnt to our needs. It was more fun because now that we had some background, we werent as intimidated and the challenge was something that allowed us to take a trip down memory lane for ICPD. Finally, comes to this Blog entry -> Post practical tasks. The objective of these tasks is for us to go one step beyond our comfort zones and now not only configure Output devices but Input ones as well. Moreover, we had to use a stimulation software - Tinkercad to test our virtually built circuits and form the codes via blocks such that we knew they would work when we built them physically. However that was another challenge i.e, building the circuit physically because we didnt have to do this so extensively in the previous two stages. Hence, the module team wanted us to be completely competent and confident in using the softwares, building the circuit so that we would be able to seamlessly use Arduino in our prototypes later on. 

What made the tasks interesting was the Practical - Unicorn because it brought some freshness into a rather dull journey whereby we had to just sit in front of our laptops, code and use the circuit to be honest it was getting quite mundane. However the unicorn brought some fun into our Arduino learning. The tasks were very tedious because we had so many tasks to complete and document extensively yet, i felt that they were important and would certainly add some value to our skill-set. They were important in broadening our skill-set and confidence because many of us have this fear of not being able to program for me i felt this way simply because I had no connection to it whatsoever or couldnt see the link between programming and Chemical Engineering whereby it is mainly got to do with chemical reactions, process control and optimisation etc. However, the fact that we were extensively exposed to this, though the process was difficult and having faced alot of obstacles and frustration when my code didnt work or if my circuit would light up or achieve its desired outcome, after figuring out how to solve these it broke my fears of programming as a whole because I knew that though i struggled i managed to do something tough. And so this sense of confidence and having no fear of trying something new was an important reassurance to me because in the future days/years to come whenever anyone asks me to do something out of my comfort zone i will no longer hesistate allowing me to grow. Moreover, it was also important because programming is such an important skill to have since we can make something behave the way we want and this would set me apart from a typical chemical engineer. Arduino programming would be useful when I would prototype my chemical product as I would require a CO sensor to indicate high levels of CO for a fan to turn on and all this is only possible with built-in electronics for the user's convenience. Hence, arduino programming is the solution here. Overall, I used to think that I suck at anything technology related and so I though Arduino Programming would be difficult and a misery for me. Now I think that it is still difficult because I have yet to explore so much more of its features however, knowing that i could complete majority of the tasks using the software has allowed me to gain more confidence in using the software. So next I will continue to practice using the software whenever i am free i.e. term break by referring to youtube videos so that i am more equipped to use it for my chemical product or even FYP in Year 3. 

 





 



Comments

Popular Posts