For Arduino Programming, I have been tasked to complete 4 different challenges.
This activity was tricky, because the programmable buttons do not naturally exist as toggle buttons, so I had to use a code that can switch on the motor and leave it running after letting go of the button. For this activity, the way that the button and motor is interfaced goes like this:
1a. Interface a Potentiometer Analog Input to maker UNO board and measure its signal in serial monitor Arduino IDE
1b. Interface a LDR to maker UNO board and measure its signal in serial monitor Arduino IDE
2a. Interface 3 LEDs (Red, Yellow, Green) to make UNO board and program it to perform something (fade or flash etc)
2b. Interface the DC motor to maker UNO board and program it to on and off using push button on the board
Before I began with the tasks, I had to make sure I selected the correct board and port.
To check for the correct board, since I was using the Arduino Maker Uno Board, "Arduino Uno" should be selected. I did this by dropping down that "Tools" List, clicked on "Board", which drops down another list. From this list, I clicked on "Arduino Uno".
By default, the port would not be selected and since I only had 1 port, I have no choice but to select it. I did this by dropping down the "Tools" List again and clicked on "Port" which drops down a short list. From this list, I selected "COM3".
Now we are ready to start the tasks.
1a.
As you can see in the image above, this is the exact way that I have interfaced the potentiometer to the Maker UNO board with te help of a few Dupont wires and a small breadboard. However, this is only half of the task.
Now, I have to figure out how to measure its signal in the serial monitor in arduino IDE. So, I just scolled through arduino IDE examples, where I found "AnalogInOutSerial".
The code is almost complete. I just have to make sure that the input pin is the correct pin.
Since the analog input pin I used is A5, I changed "const int analogInPin = A0;" to "const int analogInPin = A5;"
The code is complete. I did not make anymore changes because the example code already has "Serial.print" which will display the measurements made by the Maker UNO board. After opening the serial monitor, upon adjusting the potentiometer, the changes in values for "sensor" and "output" are seen. When the potentiometer knob is turned all the way clockwise, both the sensor and output signal is 0, but turning it all the way anticlockwise displays a sensor signal of 1023 and an output signal of 255.
That is Activity 1a done!
1b.
This is the exact way I have interfaced the LDR to the Maker UNO board. The resitor connected to the LDR has a resistance of 10k ohms. Now we will get to the code.
The code I used for measuring the signal of the LDR is exactly the same as that of the potentiometer, even the use of pin A5 as the input.
However, to truly know that the LDR is working and sending signals, I opened up the serial monitor and started playing around with my handphone's flash light. Without introducing any other light sources other than the lights in my room, the sensor signals around 270 while the Maker UNO converts this to around 70 for the output. When I cover the LDR with my finger, the sensor signal drops to 77 and is converted to output of 19. When I shine the light from my phone, the sensor measures 639 and the output is converted to 159. Thus, I can confirm that the Maker UNO is measuring the signals from the LDR.
Code for both activities 1a and 1b here.
After conducting these first two activities, I have learnt that interfacing components onto the Maker UNO can be very confusing, especially if you do not know which end of the LDR/Potentiometer is which, and to which pin each end is to be connected to. Which is why, I have also learn that the breadboard is a very useful tool to connect components together because you can easily see where the connections lead to and can make corrections easily when the interfacing is done incorrectly.
Onto the next activity!
2a.
Above, again, shows the exact set-up I have for making 3 LEDs fade after one another. For this activity, I made the red, yellow and green LED fade in and out. The red LED fades in and out, then the yellow LED fades in and out, and lastly the green LED follows.
The code I used for this simulation was made from Tinkercad using the code blocks editor. The code blocks used for this activity is taken from this YouTube video.
However, since there are 3 LEDs, I repeated the same code blocks used for the two other LEDs, but changed the code blocks according to the pin each LED uses. For this activity, the red LED is connected to pin 11, yellow to pin 10 and green to pin 9.
After editing the code blocks, I translated it into text using Tinkercad. Code link is here.
The teacher explains really well how the code works, but here is my take: To make the lights fade in and out, the LED has to first be connected to a pin that has a tilde (squiggle) because they can simulate the adjustment of a potentiometer from sensor signals of 0 to 1023 and output signals of 0(off) to 255(on). This means that the fade effect in the LEDs looks much smoother and natural, as if controlled by a potentiometer.
As for the code, we have the void setup and loop. The void setup indicates which pins are the output. In my case, I have three outputs; pin 9, 10 and 11. The void loop uses 2 "for" loops for each LED; the first "for" loop to crank the brightness up and the second to turn it down.
Video of how the completed activity looks like is here.
2b.
This activity was tricky, because the programmable buttons do not naturally exist as toggle buttons, so I had to use a code that can switch on the motor and leave it running after letting go of the button. For this activity, the way that the button and motor is interfaced goes like this:
1. Input Pin(Programmable button) is pin 2
Programmable button is connected to 5V
2. Output Pin(DC Motor) is pin 13
DC Motor in connected to Ground.
The reason why I had to use a programmable button in Tinkercad is because the Arduino UNO Board in Tinkercad does not have a built-in push button. Thus, I had to use an external push button in the simulation to represent the built-in push button on my Maker UNO Board.
Also, in doing this activity, I was forced to use a new operator which is "boolean". Basically a "boolean" is similar to an integer, "int" as it stores data. However, it only stores true or false data unlike an "int" or "const" which can store all types of data. The reason why I used "boolean" is because the button and motor can only have 2 different states, which are either on or off for the DC Motor or either closed or open for the push button. For example, when the button is not pressed, the button state is low, but when it is pressed, it is high.
Take a look at the code to gain an understanding of how the toggle button works.
Code link with explanation is here!
After completing these two other activities, I realised how much harder it is to come up with your own code to use for the output, because the Arduino Board can program the output components to do many different things. Thus, I got to learn at least one variation of the actions the output components can do.
With regards to interfacing, I didn't really find much of an issue. The problems/difficulties only come with the programming of the board. I found it difficult to express in code, the way I want my input and output components to work. Thus, I had to overcome this feeling of being lost by doing some research and replicating others codes, but tailoring it to my needs by making changes in the code. To put it simply, "gaining inspiration".
I think these activities are important because we get to practice programming on Aruino and I must say, its not very easy to do. But because I had practice, I will be able to apply this knowledge on a different project.
I used to think that Arduino Programming is unnecessary, as a chemical engineering student, because chemical engineers focus more on chemical plants and process engineering. Now, I understand that this skill is useful because chemical engineers must also learn how to develop useful products and without this skill, the range of products that I can ideate and develop will be limited. So next, I will use this new knowledge to further improve on an idea for a project.





Comments
Post a Comment