Arduino Programming

For this entry, I will be bringing you through my own experience when learning basic Arduino Programming and some of my thoughts on it.

Interfacing Input device to Arduino Board:

I was tasked to do the following individual exercise on interfacing the input and output device to the Arduino board.


Figure 1: Workscope

Potentiometer Analog

To begin off, I will be interfacing the Potentiometer Analog Input to the UNO board and measuring its signal. I used the following video below to help guide me on this exercise.

'

Figure 2: Guide on making potentiometer analog on TinkerCad


After following the video, this is the setup that I got from making it on TinkerCad, and right below is how the code runs when in simulation. The Arduino Analogue Digital Converter (ADC) converts the incoming analog signal between 0 and 5V between a range of numbers from 0 to 1023ms.

The code is written such that the analog pin A0 is the input device and pin 13 is the output. Inside the void loop, there's a new function of analogRead which listens to the pin state. The value for delay will change as we turn the knob, and the LED will flash at a rate of 0 to 1023ms.



Figure 3: Video of potentiometer analog code running.

CODE (POTENTIOMETER)


Light Dependent Resistor (LDR)

Next, I will be interfacing a Light Dependent Resistor (LDR) to maker UNO board and measure its signal in serial monitor Arduino IDE. I made use of the following video to help me on setting up the Arduino Board on TinkerCad once again.


Figure 4: Tutorial Video on LDR sensor

By following the video step by step I was able to come up with this setup, and below is how the code runs and what happens when I adjust the LDR is that the light bulb will dim or brighten depending on the direction I adjust the resistor at.


Figure 5: Video of LDR working


The code works similarly to the potentiometer analog, where the sensor value would be taken from analog A0 which is the input of the setup, the LDR then can be controlled such that when it is dim the LED will not light up and when it is bright the LED will light up as the resistance through the system is lower at a higher light intensity which results in the LED from lighting.


Interfacing output device to Arduino Board:


3 LED to UNO Board (Fade)

After interfacing the input device to Arduino Board, I then moved on to doing the output device to Arduino Board. First up, I tried to interface 3 Led to maker UNO board and program it to fade. With the help of the video below, I begin the process.


Figure 6: Tutorial on interfacing LED to UNO Board.

After following through the video step by step, I manage to make the same setup that is shown in the video but I made some minor additions to the initial setup to change the code. By following the initial code used in the video, I duplicated the blocks highlighted in red twice such that I can create 2 more loops for 2 more different LEDs, the pin number would change to the corresponding LED pin number that you chose to put your LED on, for me, I chose the pin 10 and pin 11, so you would realize that the code written below is for pin 10 and pin 11 respectively.

Figure 6: Modification made to code

Another thing to take note of is when connecting a new LED light to the UNO board, make sure that there is a resistor in front of the LED, or else there is a high chance your setup would not work when trying to imitate this exact setup in reality, as the voltage would run straight to the LED and it will spoil the LED straightaway.


Figure 7: Setup of LED Fade


Lastly, a small video to illustrate the code written. What the code is doing here is lighting up the LED in the order of (Red, Yellow then Green) where it will light up one LED color at a time and slowly fade off before the next LED color lights up and slowly fade off once more, this cycle will be repeated until the program/simulation is being stopped.


Figure 8: Video of LED fade simulation


DC motor to on/off with push button

To start off, I first found a video that illustrates how to use the button to on/off the DC motor.



Figure 9: Tutorial video on DC Motor with on/off button

Unlike previous tasks which I can find the tutorial videos which guides me all the way through, this task did not have those easier tutorials readily made on TinkerCad, but it did not take long for me to find the Tinkercad Design that was designed by another person [DC Motor Setup]. I then tried to understand how he tried to create the setup and afterward I crafted out my own setup as well. Below is the steps that I took in order to craft out the setup.

To begin off, select the ready-made setup for "Breadboard" under "Arduino"



Next, select the "Pushbutton" which can be found under "Components → Basic" and connect it to the breadboard as shown below.



Next,  connect the power supply of 3.3V to the Pushbutton. 


After that, connect a resistor to the PushButton with one end of the resistor with 10㏀ being connected to Pin 7.



Next,  connect a DC motor onto the Breadboard.



Next, connect a resistor of 150Ω to pin 8 and the other end will be connected to the positive end of the DC Motor.


Lastly, connect the ground wire to the Pushbutton and DC Motor respectively and this is what the final setup will look like.




Now, I will proceed to do the coding portion. The code is available here

DC Motor Code; Tutorial of code

The code below is written such that the Arduino system reads the position of the button and when it is pressed, currentButtonState = High, and if it is not pressed the currentButtonState = Low. Then when the statement of lastButtonState == HIGH and currentButtonState == LOW is met. The !motorState which means that if the motorState == Low, the motorState = HIGH else it will be low. This then allows the DC Motor to be turned on when the button is pressed, and off when the button is pressed again.








Example video below on how the code works


Summary of learning:

Interfacing Input and output device to Arduino Board:

After going through the exercises for the interfacing input device to Arduino Board, I learned that interfacing input device to Arduino Board will allow us to collect the data that we need from the input device to the Arduino Board itself and these data would then be fed into an output device through the Arduino Board, which will then react according to the data given by the input device.

For instance, the input device could be an infrared sensor and the output device can be a DC motor. When the input device collects the relevant data, it would then be fed to the output device which is a DC motor over here. This data would then be converted to a signal for the DC motor to carry out an action, this action can be turning the motor on/off depending on the data that was being fed into it.

Problems faced:

When trying to write the code for the programs, I tried to use both the "blocks + text" and "text" function in Tinkercad and I found myself struggling hard when trying to write the code as I would leave out a comma or a bracket sometimes and this resulted in my code not being able to run but I was able to spot the mistake after relooking at the code for a few more times.

It was also hard when trying to fix the setup on Tinkercad as I had no prior knowledge to Arduino Programming, thus the challenges that were being tasked to us seemed peculiar and weird but after doing a bit of research, I realized that it was actually not as tough as it sounds and I was able to come up with the setup on Tinkercad and it is running smoothly as well.

Reflection

Through this Arduino Programming activity, I was able to learn more about the UNO board and how it is capable of doing simple programs such as the LED fade or the DC motor on/off which are activities that I have covered on top. I would not say that it is easy to learn but the online tutorial videos that are made by other people certainly did help to make this learning process much easier and enjoyable. 

Tinkercad also has the function of allowing us to code using the blocks and these blocks can later then be converted to text which you can download as a '.ino' file which can be open on Arduino IDE and the setup can be done in real life as well.

Out of all the activities that I have covered in this entry, I enjoyed the DC motor with the on/off button the most, as it was quite interesting how when I press the button, the DC motor would be turned on and when the button is not pressed the DC motor is off.

Overall, I think I will continue to learn about the other functions that the UNO board has to offer and apply it where relevant in the subsequent projects.




Comments