Blog 3 (Arduino)
- Axrega Axrega
- Dec 3, 2022
- 12 min read
Input Devices:
Interface a potentiometer analog input to Maker UNO board and measure/show its signal in serial monitor Arduino IDE.
Interface a LDR to Maker UNO board and measure/show its signal in serial monitor Arduino IDE
Output Devices:
Interface 3 LEDs (Red, Yellow & Green) to Maker UNO board and program it do something (flash or fade, etc)
Include the pushbutton on the Maker UNO board to start/stop part 2.a above
For each of the tasks, I will describe:
The program/code that I have used and explanation of the code. The code is in writable format (not an image).
The sources/references that I used to write the code/program.
The problems I encountered and how I fixed them.
The evidence that the code/program worked in the form of video of the executed program/code.
Finally, I will describe:
5. My Learning reflection on the overall Arduino programming activities.
Input devices: Interface a potentiometer analog input to maker UNO
board and measure/show its signal in serial monitor Arduino IDE.
1. Below are the code/program I have used and the explanation of the code.
Code/program in writeable format | Explanation of the code |
int sensorValue = 0; | The variables for the sensor value is 0 |
voidsetup() | This is where we can setup the board's pin identity, where we can set any pin as Input or Output. The code under the void setup only runs once after each reset on the Arduino Uno. |
pinMode(A0, INPUT); | Declares that the analog pin, A0 is the Input |
pinMode(13, OUTPUT); | Declares that the pin13 (led pin) is the output |
voidloop() | Under the void loop, the code/program will run forever as long as the Maker UNO board is turned on |
sensorValue = analogRead(A0); | Reads the input value on the analog pin 0 |
digitalWrite(13, HIGH); | Turns the Led pin on |
delay(sensorValue); | delays the program/code for <sensorValue> milliseconds |
digitalWrite(13, LOW); | Turns the Led pin off |
2. Below are the hyperlink to the sources/references that I used to write the code/program.
3. Below are the problems that I have encountered and how I fixed them.
The problem I faced while doing this activity, was when trying to make the set up work. Initially, I thought that the potentiometer was not working or functioning properly since I followed the tutorial exactly. I double checked the codes to see if I had missed out on any important codes. I only realized the problem when I checked the connections to the potentiometer. I realized I did not connect the Analog pin, A0 to the center point of the Potentiometer. After changing this, the led started blinking.
Another problem I encountered was when trying to see if the led functioned properly. I connected the light bulb on the breadboard and clicked on upload the code to the Maker UNO board. However, the light bulb did not light up or blink when i turned the potentiometer. I realized that I had connected the Anode and Cathode of the light bulbs onto the incorrect connections on the breadboard. I fixed this problem by simply switching the connection of the light bulb and this enabled it to light up.
4. Below is the short video as the evidence that the code/program work.
Input devices: Interface a LDR to Maker UNO board and measure/show its signal in serial monitor Arduino IDE.
1. Below are the code/program I have used and the explanation of the code.
Code/program in writeable format | Explanation of the code |
const int ledPin = 5; | The LED is connected to digital pin 13, which cannot be altered. |
const int ldrPin = A0; | The LDR is connected to the analog pin 0, which cannot be altered. |
voidsetup() | This is where we can setup the board's pin identity, where we can set any pin as Input or Output. The code under the void setup only runs once after each reset on the Arduino Uno. |
Serial.begin(9600); | Establishes a serial connection between the Arduino board and another device, which is most likely the computer via a USB cable |
pinMode(ledPin, OUTPUT); | Declares that the LED pin is the Output |
pinMode(ldrPin, INPUT); | Declares that the LDR pin is the Input |
voidloop() | Under the void loop, the code/program will run forever as long as the Maker UNO board is turned on |
int ldrStatus = analogRead(ldrPin); | Reads the status of the LDR pin |
if (ldrStatus <= 200){digitalWrite(ledPin, HIGH); | If the LDR senses darkness, the LED pin will light up |
Serial.print("Darkness over here,turn on the LED :"); | It displays the text "Darkness over here, turn on the LED :" on the serial monitor. |
Serial.println(ldrStatus); | Displays the status of the LDR on the serial monitor. |
else { digitalWrite(ledPin, LOW); | If LDR senses light, the LED pin will not light up |
Serial.print("There is sufficient light , turn off the LED : "); | It displays the text " There is sufficient light, turn off the LED :" on the serial monitor |
Serial.println(ldrStatus); | Displays the status of the LDR on the serial monitor |
2. Below are the hyperlink to the sources/references that I used to write the code/program.
3. Below are the problems that I have encountered and how I fixed them.
When doing the setting up of the LDR on the breadboard, I incorrectly connected the wrong resistor to the set-up. The resistor which I had connected had a resistance of more than 220 ohms, which affected how the LDR functioned, since more voltage had to be supplied. I fixed this issue by switching the resistor with the correct resistor of 220 ohms, which enabled the LDR function to be displayed more smoothly on the serial monitor.
Another problem I encountered was trying to make the LED light up for the set-up. The LED did not light up when I covered the LDR completely. This meant that there was a false or poor connection among the wires which were connected. Sure enough, the connection to ground was not proper, and it only worked when I switched the wire out for another one.
I also faced the problem of trying to make the LED off when the LDR was not covered. When i shone light on the LDR, the LED did not turn off initially, which made me recheck the code. Upon checking the code again, I realized that I had typed in an incorrect code function where I wrote that the output was pin 13 when the physical connection on the breadboard was to pin 5. I switched the code to pin 5 and reuploaded it and it worked.
4. Below is the short video as the evidence that the code/program work.
Output devices: Interface 3 LEDs (Red, Yellow & Green) to Maker UNO board and program it to perform something (flash or fade, etc)
1. Below are the code/program I have used and the explanation of the code.
Code/program in writeable format | Explanation of the code |
voidsetup() | This is where we can setup the board's pin identity, where we can set any pin as Input or Output. The code under the void setup only runs once after each reset on the Arduino Uno. |
pinMode(8, OUTPUT); | Declares that Pin 8 is the Output |
pinMode(12, OUTPUT); | Declares that Pin 12 is the Output |
pinMode(13, OUTPUT); | Declares that Pin 13 is the Output |
voidloop() | Under the void loop, the code/program will run forever as long as the Maker UNO board is turned on |
digitalWrite(8, HIGH); | Turns the LED pin 8 on. |
delay(1000); | Delay the program/code by 1000 milliseconds |
digitalWrite(8, LOW); | Turns the LED pin 8 off. |
digitalWrite(12, HIGH); | Turns the LED pin 12 on. |
digitalWrite(12, LOW); | Turns the LED pin 12 off. |
digitalWrite(13, HIGH); | Turns the LED pin 13 on. |
digitalWrite(13, LOW); | Turns the LED pin 13 off. |
2. Below are the hyperlink to the sources/references that I used to write the code/program.
3. Below are the problems that I have encountered and how I fixed them.
Initially, I tried to program this by myself using what I had learnt. However, the code which I put in was incorrect, which made only 1 of the LED to light up. I rechecked the connections but could not find any error on the physical breadboard. Therefore to fix this, I went to Tinkercad and tried to do it there. It worked correctly on the Tinkercad, so I copy pasted the new code and tried it with the physical Arduino board. It finally worked. I figured out that the problem was when I wrote under the void loop, I failed to check if the digitalWrite code was correctly written.
4. Below is the short video as the evidence that the code/program work.
Output devices: Include pushbutton to start/stop the previous task
1. Below are the code/program I have used and the explanation of the code.
Code/program in writeable format | Explanation of the code |
voidsetup() | This is where we can setup the board's pin identity, where we can set any pin as Input or Output. The code under the void setup only runs once after each reset on the Arduino Uno. |
pinMode(8, OUTPUT); | Declares that Pin8 is the Output |
pinMode(12, OUTPUT); | Declares that Pin12 is the Output |
pinMode(13, OUTPUT); | Declares that Pin13 is the Output |
pinMode(2, INPUT_PULLUP); | Pin2 is an Input and enables the internal pull-up resistor. Thus, by default, Pin2 status will always be High |
void loop () | Under the void loop, the code/program will run forever as long as the Maker UNO board is turned on |
byte buttonstate = digitalRead(2); | Reads the Input from the button pin, which is in this case Pin2. |
if(buttonstate == LOW){ | When the button is pressed |
digitalWrite(8, HIGH); | Turns the LED on Pin8 on |
delay(1000); | Delay the program/code by 1000 milliseconds |
digitalWrite(8, LOW); | Turns the LED on Pin8 off |
digitalWrite(12, HIGH); | Turns the LED on Pin12 on |
digitalWrite(12, LOW); | Turns the LED on Pin12 off |
digitalWrite(13, HIGH); | Turns the LED on Pin13 on |
digitalWrite(13, LOW); | Turns the LED on Pin13 off |
else { digitalWrite(8, LOW); digitalWrite(12, LOW); digitalWrite(13, LOW); } | When the button is not pressed, turn off the LEDs on Pin8, Pin12 and Pin13. |
2. Below are the hyperlink to the sources/references that I used to write the code/program.
3. Below are the problems that I have encountered and how I fixed them.
When programming for this code, I was unsure of how to set the pushbutton for the LEDs to turn on. So, when I tried messing with some of the familiar codes I knew, I couldn't get anywhere. Thus, I tried searching online how to add in a program for the pushbutton and found one which was useful. With the code that I found, I merged it with the code for the 3 LEDs, which eventually enabled me to have the pushbutton for the LEDs.
4. Below is the short video as the evidence that the code/program work.
Below is my Learning Reflection on the overall Arduino Programming activities
Before kick starting on the activities that I did, I had done a little bit of background research on Arduino Programming. I realized that it is a type of programming which is similar to C++, which was something I was aware of. Doing programming work is known to be tedious and time consuming as a small mistake can take very long to be discovered. Therefore, I was a bit worried as of how it was going to turn out when I am introduced to the Arduino Programming activities. Nonetheless, I attended the lesson to know more about how to use or learn about the Arduino itself, since it's something very new to me.
On the first lesson for the Arduino programming, we had to make it perform a few functions. We were sat in our groups and had to pair up in the group since each group had 2 Arduino Uno Maker kit. When I first received the Arduino Uno, I was fascinated by it. I realized that it consisted of many different components. There were wires, the breadboard and some other equipment's such as LEDs and resistors. On the first lesson, we had to identify how the different resistors in the kit varied from each other. This can be determined with the help of the color indication on the resistors and the graph of information for what these colors and their orders meant. We found out that the resistors had very different values. I learnt later on about the importance of these resistors. They are useful in reducing the potential difference that is being transmitted to these LEDs, which prevent them from getting hot or worn out. Therefore, the resistors are useful in protecting the LEDs.
Then, we were allowed to use the Arduino Uno to perform some functions. Since we had no idea about what codes to use or how to put them, we followed the tutorial provided on Brightspace. With the tutorial, we were able to complete our first assignment, "Hello World!". According to the tutorial, we had to use the examples that were already provided, which was the Blink code. From there, we just had to upload the code to our Arduino. And there we go, we were able to get our Arduino board to blink. This was really interesting to me although I did not understand anything from the code. Therefore, I tried to take a look at the codes and was left confused. Luckily, the information on Brightspace covered what the codes meant, which was really useful for me. I was able to differentiate between void setup () and void loop (), which made me proud initially. I glanced through what the other codes meant and tried to carry on with the lesson. Some key codes such as pinMode and digitalWrite was important for me to know before moving on.
The next program we had to do was the programmable button, which was simply making the Arduino respond to the button when it is pressed. I learnt that the default pin for programmable button is Pin 2. Doing this program had much more complicated codes as compared to the previous activity which made me feel worried. How am I supposed to know what all these codes mean? I was questioning myself if learning the codes is possible. Nonetheless, I was willing to give it a try. After looking at the codes, I was able to discover that the functions void setup() and void loop() serve very important roles. The void setup() is usually used for a one time action, where the input and outputs are defined. The void loop() is usually used forever until the Arduino is turned off. Therefore, they are more useful when it comes to doing a function nonstop. I also discovered that the void loop() can be used instead of the void setup() when a function has to be repeated multiple times, which can reduce the possibility of errors in the code. I felt proud of myself learning these.
The next activity, which was to program "Make some noise!" which was simply playing some music through the Arduino. This code seemed very cool as compared to the codes as it was going to produce audio. We followed the simple code provided on the examples, toneMelody. This generated a code which we uploaded onto the Arduino to make some noise. I realized that I could play which notes I want by changing them in the codes, and reducing the duration of the notes. However, I realized later on that the main program to be set for this was to include the set of pitches. This is to define what each note meant, which we have to type out when we come up with audio codes. These set of pitches is where the Arduino will play the audio from. This made so much more sense, as I was wondering how does the Arduino know what each audio meant.
Next, we were at the servo, which was to program it to move. This was a unique code to function as it was with an output device. This was the part where we had to first make connections to the Arduino. This was interesting for me, as it was like connecting wires here and there. I was in the mindset that I could connect the wires anywhere and the servo would turn on. I realized that the connections had to be accurate in order for the servo to function. Thus, I followed the tutorial on the Brightspace to make the perfect connections which finally made the servo turn. That was when I noticed that the connections for the servo to the
Arduino has to be very accurate. The ground connection must be connected to the ground wire on the servo. The voltage wire should be connected to the voltage wire on the servo. With these proper connections, the set up worked. Doing these 4 activities actually helped to boost my knowledge on Arduino very greatly. I had no idea that it was this complicated and knew it was gonna get worse.
We also had to do the more advanced activities for our next Arduino lesson. We were now being exposed to use the LEDs, resistors and the breadboard. With the knowledge I learnt from the previous activities, I was ready to handle the next 4. We had to program for input devices and output devices. We were exposed to a new software, known as the Tinkercad, which was very similar to that of the Arduino. The Tinkercad was the same as that as the Arduino, except that it is online. It even produces the code for something to function. This was a great learning tool for us. I looked through the Tinkercad, and followed the youtube tutorial provided to interface a potentiometer. I tried to do it with the Arduino too after completing it on the Tinkercad, and sure enough it worked. This was really interesting to know, that I can use this website to code for the Arduino.
We then carried on to do the next Input device, which was to interface a LDR. Through this activity, I managed to understand a better idea of how the LDR worked. I found out that it actually showed changes in the display when I covered it from a light source. So, this was also how they made streetlights. This was really interesting to know. The potentiometer similarly, which made the LED blink more when the voltage was increased, due to the difference in resistance. Learning about these different equipment's in the Arduino will definitely help me out in the future.
The next activity, which was to interface 3 LEDs, to mimic that of a signal. This was very exciting to do as I had to make it similar to that in real life. Following the code provided and set up of the Arduino the set up was done. When the code was uploaded, the 3 LEDs started to slowly flash, based on the delay function I had put in. To make the LEDs light up longer, I simply had to change the delay duration. Following up on this activity, we were tasked to also make a push button for this, where the LEDs function when a button is pushed. I took this as practice and tried to program this myself. With abit of help from my friends and online, I was finally able to get the program working. After these activities, I felt competent in programming and using the Arduino.
Finally, we were able to do our practical session which involves using the Arduino. With the skills I have learnt, I was ready to perform and do well for the practical. The practical was simple. We had to make the cardboard of unicorn flap its wings using the servo. We also had to make more functions for the unicorn using our knowledge. My partner and I first brainstormed about what idea to come up with. We thought about programming music for the unicorn so that it plays a song when it flaps. We also decided on using the propeller and motor to mimic a fan when the unicorn flaps. With that, we immediately got to work. My partner was in charge of crafting the unicorn, while I was doing the programs for the code. I first came up with the code for the servo to make the flapping motion for the wings. I changed the delay, which made the servo turn much faster to help it look more like the wings are actually flapping. I then turned to help my partner to decide on how we were going to connect the servo to the inside of the unicorn. This was a confusing challenge we faced, and ended up using the metal pieces provided to slot into the servo and tape onto the wings. This made them attached to each other and easier to turn. Next, we had to decide how the servo would be connected to our unicorn as it made the center of gravity shift. We tackled this problem by simply adding supports to out unicorn. Next, we had to make an audio for our unicorn. I searched online for unique music themes and found one that was interesting. I decided to use the audio and tried to copy paste but nothing changed. This was confusing and worried me. But then, I later on realized that I had to input the audio codes into the same void setup() and void loop(). This was new to me and im really glad to find this important information out. With this known, I rewrote the code into the program properly, which made the audio to be produced properly. This was great news and now we could move on to making the fan. But this is where everything fell apart. When trying to get the code ready, I approached a classmate and followed his code. After setting up and connecting the wires to the breadboard and uploading the code, the fan spun. However, the wings did not flap. This was unusual as it was working perfectly fine and we did not change anything to the wings. The audio was also not working. Thus, we removed the program for the fan and checked to see if the other codes were working. Sure enough, they were working. This was shocking and upsetting to see. We had already used up so much time for the fan part, which is now not working. It was really frustrating but we tried to keep our cool and persevered on. We kept trying to make the fan work alongside the servo, but to no avail. Eventually, the audio part also stopped working when we did not touch the codes at all. This ruined all the work we did as we were just left with the servo mechanism. We tried to make the fan work one more time by changing some of the wire connections to it. Nothing happened except that we smelt something burning and realized the motor was not functioning properly. We had to stop using it and ended up just presenting the servo functioning with no audio or fan. This ended in a major catastrophe for us, as nothing worked the way we wanted.
We were upset with the results we got, but took it in the positive note that the learning is what really matters. Getting to try and put different programs together in the same code was new to me and glad that I was able to try it. I have now realized how difficult programming is in the context of the real world. Nonetheless, we pushed on till the end trying to make our product work but to no avail. It was a great learning opportunity and I would like to try and create something new and better the next time :)



Comments