Arduino Programming
For this post, it will be about Arduino Programming. To learn and practice using the Arduino Uno, we went through a tutorial lesson, competency test and practical. I was actually quite excited to learn yet another new program, however trying to learn and use the Arduino Uno was much harder than I thought.
Explanation on the code used:
Explanation on the code used:
The contents of this post:
1. Thoughts on the Tutorial Lesson
- The 4 Challenges
2. What was done during the Practical
- Competency Test
- Group Challenge
3. Arduino Individual work on Input and Output devices
- How I programmed the Arduino board and explanation of the codes used for the given tasks
- Source code files
- Videos of the executed program work
- What I learnt from interfacing an input device to Arduino board
- What I learnt from interfacing an output device to Arduino board
- Problems I faced and how I fixed it
- Reflection
1. Thoughts on the Tutorial Lesson
During the tutorial lesson, we were asked to go though a rise articulate package that teaches us everything we need to know about Arduino and there were also some challenges for us to do with the Arduino Uno kits that was provided.
The different challenges are:
1- Hello World
2- Programmable Button
3- Make Some Noise
4- Servo
After some difficulty with completing the challenges and help from friends, I managed to achieve what was asked for in the challenges. From this lesson, I kind of got the gist of how things should be done using the Arduino but it was still not easy.
2. What was done during the Practical
For this practical, we had yet another competency test to sit for. This time I was even more nervous as I was still unsure and not confident in using the Arduino and coding. However, during the competency test I surprisingly knew how to do what was asked for. I did require some guidance here and there due to my poor understanding of the question but I still managed to do it in the end. Overall, I really enjoyed the test as there was some thrill in trying to figure out the code and testing whether the code actually works like I wanted it to.
The competency test was not the main part of the practical, but the group challenge was. For the group challenge we were tasked to make a Pegasus that can flap its wings through the use of a servo motor. Initially, I thought that it was going to take my group and I forever but we actually managed to finish everything within the practical session. The picture below is our very own Pegasus that can "fly".
This whole challenge was such a fun session especially since there were a few added challenges to the initial one. We had to design it in such a way that allows the whole Pegasus to be portable (can be disconnected from the computer) and that the wires and servo motor used in the setup cannot be seen.
I really enjoyed trying to find a way to hide all the exposed wires and servo motor while making it look nice. My group and I added clouds to do just that! We also hoped that the clouds gave an illusion that the Pegasus is really "flying" though the sky. I would say the most difficult part of this whole challenge would be making the wings flap with the metal wire connected to the servo motor and trying to make the code work. In conclusion, this challenge is something I would not mind having to do again as I really like that it involves hands on activities like making the base for the Pegasus and decorating said base.
3. Arduino Individual work on Input and Output devices
These are the tasks we were given:
Input devices:
a. Interface a Potentiometer Analog Input to make UNO board and measure its signal in serial monitor Arduino IDE
b. Interface a LDR to make UNO board and measure its signal in serial monitor Arduino IDE
Output devices:
a. Interface 3 LEDs (Red, Yellow, Green) to make UNO board and program it to perform something (fade or flash etc.)
b. Interface the DC motor to make UNO board and program it to on and off using push button on the board
How I programmed the Arduino board and explanation of the codes used for the given tasks:
Potentiometer:
A potentiometer is a type of variable resistor that is able to adjust the resistance through turning the knob on the potentiometer. The potentiometer has 3 legs, one will be connected to 5V for power, another will be connected to PIN A0 to measure and sense the change in resistance in the potentiometer and lastly, the last leg will be connected to GND.
What the Arduino board and code looks like:
The code used can be found by clicking onto the "CODE" tab above.
int sensorValue=0; - set value of sensorValue to 0
Serial.begin(9600); - this establishes serial communication between the Arduino board and another device. The most common serial communication is between the Arduino and computer via a USB cable.
pinMode(A0, INPUT); - establishing PIN A0 as the input
pinMode(13, OUTPUT); - establishing PIN 13 as the output
sensorValue = analogRead(A0); - read the value from the potentiometer that is connected to PIN A0
Serial.println(sensorValue); - print out the value of the potentiometer (results can be seen on serial monitor)
digitalWrite(13, HIGH); - turns LED on
delay(sensorValue); - pause the program for <sensorValue> milliseconds/wait for sensorValue millisecond(s)
digitalWrite(13, LOW); - turns LED off
Source code:Potentiometer Code
Video Guide:Here
In summary, when the potentiometer (input) is being turned, the frequency of the LED blinking changes as the resistance is also changing. When the potentiometer is turned clockwise (to the right), the frequency of the blinking decreases due to an increase in resistance. However, when the potentiometer is turned anti-clockwise (to the left), the resistance decreases and the frequency of the blinking will increase. Essentially this means when the sensorValue is higher, the delay will be longer, resulting in a decrease in frequency. All this changes will be recorded in the serial monitor as Serial.begin(9600) and Serial.println(sensorValue) was added into the code. The video below shows what was described above.
LDR:
The light dependent resistor (LDR) changes resistance with changing light intensity. When there is light, the resistance decrease and when there is no light, the resistance increases. The LDR will be connected to the red LED so that when there is a change with the LDR the LED will react along with it.
The code used can be found by clicking onto the "CODE" tab above.
const int ledPin = 13; - making PIN 13 the "ledPin" and will not be changed as it has been set as a constant ("const")
const int ldrPin = A0; - making PIN A0 the "ldrPin" and will not be changed as it has been set as a constant ("const")
Serial.begin(9600); - this establishes serial communication between the Arduino board and another device. The most common serial communication is between the Arduino and computer via a USB cable.
pinMode(ledPin, OUTPUT); - establishing PIN 13 as the output
pinMode(ldrPin, INPUT); - establishing PIN A0 as the input
int ldrStatus = analogRead(ldrPin); - read the status of LDR value
if (ldrStatus <=300) {
digitalWrite(ledPin, HIGH);
Serial.println("LDR is DARK, LED is ON");
}
else {
digitalWrite(ledPin, LOW);
Serial.println("-----------");
}
What the above highlighted code means is that if the LDR value read is less than or equals to 300, the LED will light up, else, the LED will remain off.
Source Code: LDR Code
Video Guide: Here
In summary what this code means is that, if the LDR is in a dark environment (ldrStatus <=300), the LED will react and light up. But if the LDR is in a well lit environment (ldrStatus is more than 300), the LED will remain off. Since there is a serial connection (Serial.begin(9600) and Serial,println( ) ), the value is continuously printed out on the serial monitor. When the LED lights up " LDR is DARK, LED is ON" will show up on the serial monitor and on the other hand, if the LED is off, "----------" will show up on the serial monitor instead. The video below shows the serial monitor reacting to when the LDR is in a dark environment and then switched to a well lit environment.
3 LEDs:
For this, I set the LEDs in a way that it will all fade together, this is done by adjusting the brigthness of the LEDs.
What the Arduino board looks like:
How the code works:
int brightness = 0; - set value of brightness to 0
pinMode(9, OUTPUT); - establish PIN 9 as the output
pinMode(10, OUTPUT); - establish PIN 10 as the output
pinMode(11, OUTPUT); - establish PIN 11 as the output
for (brightness = 0; brightness <= 255; brightness += 5) {
analogWrite(9, brightness);
delay(30); - wait for 30 milliseconds
analogWrite(10, brightness);
delay(30); - wait for 30 milliseconds
analogWrite(11, brightness);
delay(30); - wait for 30 milliseconds
}
For the above code, it means that if the value of brightness is smaller than 255, the program will continue adding 5 to the value until it reaches 255. brightness = 0 states that the brightness is at 0.
for (brightness = 255; brightness >= 0; brightness -= 5) {
analogWrite(9, brightness);
delay(30); -wait for 30 millisecond
analogWrite(10, brightness);
delay(30); -wait for 30 millisecond
analogWrite(11, brightness);
delay(30); -wait for 30 millisecond
}
For the above code, it means that if the value of brightness is more than 0, the program will continue subtracting 5 to the value until it reaches 0. brightness = 255 states that the brightness is at 255.
Source Code: 3LED Code
Video Guide: Here
DC motor:
For this program, a dc motor is used. The dc motor will also be controlled by a push button, when the button is pressed, the motor will run and when it is pressed a second time, the motor will stop. Additionally, for this setup a transistor is used. The transistor has 3 legs just like the potentiometer from before, the 3 legs are: Emitter, Base and Collector.
Code for this setup can be found in the "CODE" tab.
How the code works:
int button = 0; - variable for the button and is set to 0
int antState = 0; - variable for the anterior state of the button and is set to 0
int buttonOff = 0; - 0 = dc motor ON, 1 = dc motor OFF
pinMode(2,INPUT_PULLUP); - establish the built-in button (PIN 2) on the Arduino board as input
pinMode(13,OUTPUT); - establish PIN 13 as the output
void loop() {
int button = digitalRead(2); - read the state of the button
if((button == LOW) && (antState == HIGH)){
buttonOff = 1 - buttonOff;
}
antState = button; - reads the actual value of button
if(buttonOff == 1){
digitalWrite(13,HIGH);
}
else {
digitalWrite(13, LOW);
}
}
What the above code mean is that essentially there are 2 scenarios, first is when the button is pressed and it is sensed that the dc motor is off, the program will start the dc motor. Secondly is when the button is pressed and it is sensed that the dc motor is on, the program will then turn off the dc motor.
Putting the above 2 scenarios into context, when the button is first pressed, the dc motor will start turning that is because the program read that the dc motor isn't on previously. On the second press, the program read that the dc motor is on, it will proceed to then switch off the motor. This is how the code and the setup works in terms of controlling the dc motor.
Source Code: DCmotor Code
Video Guide: Here
What I learnt from interfacing an input device to Arduino board:
For interfacing an input device, I learnt how to use the serial monitor. In both codes of the potentiometer and LDR, there was Serial.begin(9600) and Serial.println(). These 2 statements were what made it possible to measure the signals from the setup into the serial monitor. When the Arduino board and computer was connected, it was like connecting the input device to the serial monitor. Whenever there were changes in the input, it will always show on the serial monitor. For example, for the potentiometer, when I changed the resistance by turning the knob, signals will be sent to the serial monitor and the value will be displayed.
What I learnt from interfacing an output device to Arduino board:
For interfacing an output device, I learnt to add in an output device and control it. The output device will be connected to the Arduino board and when there is an input, the output device will act accordingly to what was written in the code. There are different ways of input, for the 3 LEDs, I just uploaded the code and the program began running, but for the dc motor, I used a push button as the input to control whether the motor runs or not. The main part to make this work is to connect the output device to a PIN and establish the PIN as an output in the code. For example, for the dc motor, the motor was connected to output PIN 13. When the push button is pressed (input), signal will be sent to the output PIN (HIGH or LOW) and then to the motor (ON or OFF).
Problems I faced and how I fixed it:
Throughout this whole activity, I have certainly ran into some problems.
Firstly, when trying to make the potentiometer work, I realised I would need to measure the signal in the serial monitor. I had some difficulties setting up the serial monitor so that whenever there were changes to the input device, it will show on the serial monitor. So I went to research and read up on how to use the serial monitor. From there, I realised that I had to add these 2 important statements :Serial.begin(9600) and Serial.println(). After adding the 2 newly found codes, I started to see values appearing on the serial monitor. This could work as Serial.begin(9600) establishes a connection between the Arduino board and computer. While Serial.println() is used to add in what I wanted to appear on the serial monitor. For example, Serial.println(HELLO); , "HELLO" will keep on appearing on the serial monitor.
Secondly, when setting up the Arduino board and codes for the dc motor, I realised that TinkerCad was a tad bit different from the actual Arduino boards we were given. Throughout this activity, I was using both the actual Arduino board and TinkerCad to test my codes and setup, from there I found that the Arduino board in TinkerCad does not have an in-built button as compared to the actual Arduino boards we had. This created another problem for me as I had zero idea on how to include a pushbutton into the setup and code, so a long research session started again. From online, I learnt a plethora of ways to add it in and succeeded.
Lastly and most probably the biggest headache out of all my problems that I faced, trying the start/stop the dc motor with a pushbutton. From the start, I already knew I would have a few problems with this portion of the activity, hence, I went online to learn what I can about controlling a dc motor using a push button. There were a lot of resources about it, but most or almost all of them needed a diode which I did not have in the Arduino kits we received. After more research, I found a video that did not need a diode. I tried it out but it did not work, so I tried it again and again until there was a burnt smell coming from the transistor. It probably overheated. After that my dc motor stopped running for some reason, one problem after another so I had to ask a friend for help. After that, I got the setup I have on TinkerCad but when doing it on the actual Arduino, it could not work. It was most probably due to the resistor, as previously a 10k ohm resistor was used but when changed to a 200 ohm resistor, it started working. Thinking back, it was probably the reason why my previous tries did not succeed as I was constantly using a 10k ohm resistor.
Reflection:
After going through the tutorial, competency test, practical and this individual tasks, I realised that Arduino programming is no easy feat. A lot of trial and error is needed as well as patience. However, I feel quite happy and accomplished having done all these activities. The 4 activities also allowed me to learn a lot more about Arduino programming as I got to interface output devices and input devices although it was a little challenging. A large tool that have helped me greatly for all these activities were the resources that can be found online. There is a multitude of different reosurces online that actually help to explain and teach the viewers/readers about Arduino programming which actually helped me a lot in understanding the different codes better. As one of the biggest challenges I had was getting the codes to work the way I wanted it to, since my knowledge was limited, so were my abilities. Therefore, I had to rely heavily on online resources. Additionally, another great help were my friends. While doing the activities, I consulted some friends from my group and from other schools. This helped me to deepen my understanding in Arduino Programming.
Overall, I would not say that I really like Arduino programming but there is certainly a thrill and feeling of pride whenever you get the code to work correctly. However, this has been a really fruitful experience of learning how to use Arduino. I hope that in future projects, I can bring forward what I have learnt. Eventhough I am not really confident in my skills but I am sure with perseverance and help from friends, I can achieve what I want.


Comments
Post a Comment