top of page
Search

Blog entry 3: Arduino programming

  • Writer: Ngo Van Anh
    Ngo Van Anh
  • Dec 3, 2022
  • 9 min read

There are 4 tasks that will be explained in this page:

1. Input devices: a. Interface a potentiometer analog input to maker UNO board and measure/show its signal in serial monitor Arduino IDE. b. Interface a LDR to maker UNO board and measure/show its signal in serial monitor Arduino IDE

2. Output devices: a. Interface 3 LEDs (Red, Yellow, Green) to maker UNO board and program it to perform something (fade or flash etc) b. Include the pushbutton on the MakerUno board to start/stop part 2.a. above

For each of the tasks, I will describe: 1. The program/code that I have used and explanation of the code. The code is in writable format (not an image). 2. The sources/references that I used to write the code/program. 3. The problems I encountered and how I fixed them. 4. 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 writable format

​Explanation of the code

int sensorValue = 0; void setup()


{


pinMode(A0, INPUT);


pinMode(13, OUTPUT);


Serial.begin(9600);


} void loop(){


{ sensorValue = analogRead(A0);


Serial.println(sensorValue);


digitalWrite(13, HIGH);


delay(sensorValue);


digitalWrite(13, LOW);


delay(sensorValue);


}

​Establish sensorValue as a variable.

In void setup, establish A0 as an input by using pinMode(A0, INPUT) and pin 13 as an output by using pinMode(13, OUTPUT); Serial.begin(9600); then starts the interface communication between the Arduino board and the potentiometer installed on the breadboard.



In the void loop, sensorValue = analogRead(A0) reads the input signal of A0 and converts the input value into a variable value, being sensorValue.

Serial.println(sensorValue); then reads and records the voltage value from A0, the number is then stored in the SensorValue variable previously established.

The voltage from A0 recorded is used to determine whether ot not to turn on the LED connected to Pin 13 as seen in digitalWrite(13, HIGH); delay(sensorValue);

and digitalWrite(13, LOW); delay(sensorValue);


2. Below are the hyperlink to the sources/references that I used to write the code/program.


3. Below are the problems I have encountered and how I fixed them.


Initially, when me and my partner were doing this exercise, we had trouble getting the LED to blink according to the position of the potentiometer. At first, the LED would just blink the same speed no matter how we rotate the potentiometer.

Then after we examine and try to redo certain connections, the LED would sometime respond to the potentiometer being turned, but sometime not.

In the end we found the issue to be one lose connection between the LED and pin 13 on the Arduino, we found this out because we suddenly realized that when I hold the breadboard in a specific position, the potentiometer will work, and it turn out because one connection is quite lose, it only have enough contact for the signal transmission when I hold it a certain way. So it was very easy to fix and we got it working just by pushing the wire into pin 13 more securely.


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 writable format

​Explanation of the code

​int light;

void setup() {


Serial.begin(9600);


} void loop() {

light= analogRead(A0);


Serial.println(light);


delay(100)

}

​Define light as a variable. In void setup, Serial.begin(9600); then starts the interface communication between the Arduino board and the LDR. In void loop, define the variable light as the reading of the LDR, light= analogRead(A0); The value of the light variable is then printed out using Serial.println(light); In addition, delay(100) sets a delay of 100 milliseconds before the value is displayed on the graph 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 I have encountered and how I fixed them.


This is in my opinion the easiest exercise out of the four, as we were only required to wire one LDR pin onto the breadboard and nothing else, and also we do not need to interface the LDR signal to any other devices beside the Arduino board, in order to display the reading on a monitor. Me and my partner did not encounter much problems in this exercise beside a minor hiccup at the start when we don't know how to find the Serial Monitor. 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 (fade or flash etc) 1. Below are the code/program I have used and the explanation of the code.

​Code/ program in writable format

​Explanation of the code

​void setup()

{


pinMode(9, OUTPUT);


pinMode(11, OUTPUT);


pinMode(13, OUTPUT);

} void loop()

{


digitalWrite(9, HIGH);


delay(1000);


digitalWrite(9, LOW);


delay (1000);


digitalWrite(11, HIGH);


delay(1000);


digitalWrite(11, LOW);

delay(1000);


digitalWrite(13, HIGH);

delay(1000);


digitalWrite(13, LOW);

delay(1000);

}


​In void setup, establish pins number 9, 10, and 13 as outputs

pinMode(9, OUTPUT);

pinMode(11, OUTPUT);

pinMode(13, OUTPUT); respectively. Then, in void loop, using digitalWrite, LEDs 9, 11 and 13 can be turn on and off with a delay of 1000 milliseconds. This is done using the commands digitalWrite(x, HIGH) to turn on the LED and digitalWrite(x, LOW) to turn off the LED, where x is the LED pin number. The delay duration is set using delay(1000), which is 1000 milliseconds or 1 second.

2. Below are the hyperlink to the sources/references that I used to write the code/program.

3. Below are the problems I have encountered and how I fixed them.

Wiring the LEDs for this exercise was quite difficult. Not only the wiring was confusing, but I also made a mistake and confused the anode and cathode on the LEDs, which resulted in 2 of the LEDs did not light up despite the code being correct But at the time we did not know and tried to debug the code instead. After I figured out my mistake, turn out all I need to do is rotate the LEDs and it worked.


However, the light is very dim and could not be seen on the video, we could barely see the light with naked eyes. So I thought to remove the resistor and replace them with wires. While this does achieve what I wanted which is to make the LEDs light up brighter, I later learned that you must always use a resistor to prevent the LEDs from being burned out. What I should have done is switched to resistors with a lower resistance first, and see if the LEDs is bright enough, instead going straight to just wires.

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 writable format

​Explanation of the code

​void setup() {

Serial.begin(9600);

pinMode(2, INPUT_PULLUP);


pinMode(9, OUTPUT);

pinMode(11, OUTPUT);

pinMode(13, OUTPUT);


} void loop() {


int sensorVal = digitalRead(2);

Serial.println(sensorVal);


if (sensorVal == HIGH) {


digitalWrite(9, LOW);


digitalWrite(11, LOW);


digitalWrite(13, LOW); } else {

digitalWrite(9, HIGH);

delay(1000);

digitalWrite(9, LOW);


delay (1000);

digitalWrite(11, HIGH);

delay(1000);


digitalWrite(11, LOW);


delay(1000);


digitalWrite(13, HIGH);


delay(1000);


digitalWrite(13, LOW);

delay(1000);


}

}


​In void setup, establish an interface between the Arduino board and the built-in button automatically connected to pin 2. Then, establish pins 9, 10, and 13 as outputs

pinMode(9, OUTPUT);

pinMode(11, OUTPUT);

pinMode(13, OUTPUT); respectively. In void loop, int sensorVal = digitalRead(2) establishes the sensor value to be the reading from pin 2, aka the state of the button. Serial.println(sensorVal); prints out the variable value when the button is pressed. Then, using the if and else commands, if (sensorVal == HIGH), meaning when the button is not pressed (in the "HIGH" position), the 3 LEDs would remain off, as digitalWrite(x, LOW); with x being the LED pin number. If the button is pressed, meaning the sensor value is not HIGH, the else { part of the code is activated hence the program of the previous exercise will be executed with LED 9, 11 and 13 turning on and off with a delay of 1 second.

2. Below are the hyperlink to the sources/references that I used to write the code/program.


3. Below are the problems I have encountered and how I fixed them.

With the wiring already done in the previous exercise, the main problem we encountered in this exercise is the code. We tried to follow the example given as best as we can to no avail. So we turned to the internet, however, all the intructions available are for a seperate button pin we can install onto the breadboard. But in our case, we have to use the build in button on the Arduino. We forgotten how to program the built-in button until we realized that it is automatically interfaced with pin 2, so we do not need to establish any further interface.


However the challenges does not stop there, as we struggled with the code even after we know the button is connected to pin 2. We tried debugging the code several times but it still would not work. Our other teammate Yan Zhen had to help us look though the code and realized we put pinMode(2, INPUT); instead of pinMode(2, INPUT_PULLUP); At first we did not understand what the PULLUP is for, turn out it is to set the default state of the button as being up, meaning not pressed. After we fix this mistake, the code worked well and with that, we were done with the 4 challenges.

4. Below is the short video as the evidence that the code/program work.

5. Below is my Learning Reflection on the overall Arduino Programming activities.

The Arduino programming part of this module is by far the most challenging skill to learn in my opinion, compared to laser cutting, gears, or even 3D printing in the previous module ICPD. Partially because I have very limited exposure to coding in the past, partially because the exposure I did have in the past is very rushed and fast-paced and I was unable to keep up.


So when I found out that not only will we be doing Arduino coding, but we will do it in 3 parts with a practical lesson at the end where we need to make our own product, I was already despairing, like many other of my classmates, as we do not expect we need to learn coding when we are not even in an EEE course.


The first lesson was quite easy, as it is an introductory lesson to the Arduino kit, all the components and what they do, as well as some simple coding exercises like making the lights on the Arduino blink, or make it play some melody. Even though I can understand all the physical components and how they work, as they are all quite basic stuff in an electrical circuit, I can only somewhat grasp what the code does, with a lot of help of explanation from my partner Jeremy. We managed to get through the first exercises in the first lesson, however I am still very unsure of my ability to use the Arduino in later parts of this whole Arduino program.


The second lesson ramped up the difficulty by several notches as we need to do the 4 challenges above in this blog, which are the Potentiometer, the LDR, the traffic lights LEDs, and the traffic lights LEDs with a button. In the first challenge, me and my partner took awhile to get it down, but after which we realized that despite me not fully understanding the coding of the Arduino, I can wire the hardware to the breadboard to the Arduino easily as wiring the system seems very intuitive to me. So that is what we did for the remaining 3 challenges, me wiring everything together while Jeremy wrote the codes. Luckily, he has prior experiences in coding and is much more proficient than I am, so not only can he do all the coding by himself he can also explain them to me as we go. With this division of work, we were able to effectively go through the challenges with only some troubles along the way.


The third and final lesson is the practical, where we were tasked with making a pegasus toy that is able to flap its wings and have other features. :Okay, that is a big stretch, going from blinking LEDs to making a full-fledge toy with various functions, from scratch." you might think to yourself.


And you would be absolutely correct.


It IS a big stretch to go from basic, isolated tasks to making a full-fledge functional toy with many features.


But nevertheless we got down and get started anyway, because what other choice do we have.


We were given a cardboard cutout which we have to assemble into a Pegasus, which is very easy if you have ever assemble a Gundam before, or just any other toy model/ figurines in general.


ree

Meet Pegasus in his natural, untouched, unharmed stage, before we butchered him with all kinds of artificial implants and mechanical parts.


The main feature that our toy MUST have is the flapping wings, and the flapping must be consistent and smooth. After fiddling with Pegasus for a while, we realized that the way to make the wing flap in the move the inner parts of his wings forwards and backwards.

ree

Normally this is done with a rubber band, pulled by hand, but we had to make it automatic with what we have on hand, which is the Arduino and all its add-ons. Our attention immediately shifted to the Servo, as flapping the wings require movement and the Servo is the only component in the kit that creates movement of some kind.


ree

The next problem we need to tackle is that direction of movement, as stated earlier, the wings need a back and forth motion the flap, but the Servo only rotates, so its motion is a circular motion. I was thinking of how to change the circular motion into the back and forth motion when I remember a mechanism that can do this. The slider-crank, which is basically a rotating wheel with sliders attached.


ree

We can use the rotating servo as the wheel providing the rotary motion, and strips of cardboard as sliders, as the cardboard can be quite firm when you cut parallel to the grain of the wavy middle layer instead of cutting 90 degrees to it.


ree

So at first I tested out with one slider attached to the wings first and it seem to work fairly well.


But with only 1 slider, it is still a but unstable as the servo rotates, its motion is slightly pushing the whole body of Pegasus up and down, so we cannot make Pegasus stand still on a base. To remedy this, I add another horizontal slider in between the servo and the wings.



First implant completed



With this mechanism finished, me and Jeremy gave ourselves pats on the back as we have gotten the main requirement down with a lot of extra time left to work on the additional features of our choice.


For the additional features, we decided on a blinking LEDS, and a melody that plays whenever we pressed the button on the Arduino, since that is about all the things we learned that is applicable here, because not like we can somehow integrate a LDR or a potentiometer into this toy, and even if there is a way, we couldn't think of it.


The LED is fairly easy, the only difficult part if wire it and install it onto Pegasus, which I managed to do after remarkable struggle, partially because I'm not used to wiring with gloves on, and also because I need to wire AROUND the moving sliders mechanism underneath. We initially wanted to do 2 LEDs, which will become eyes for Pegasus, but because of how difficult it is to wire and install the LED, we decided to only do one and give him a red flashing nose instead. So he is Pegasus Rudolph now.



ree

ree


ree

Second implant completed


The third part of the plan is the music, which Jeremy took full responsibility for since there is no physical parts to be installed, or any wiring to be done. Luckily he is also familiar with reading music, so we can play whatever melody we wanted and not just the default.


We also tried to make it so that the wings flapping and the LED flashing at the same time as the music play, but for some reason, the codes would not execute at the same time and instead, when the button is pressed for music to play, it also stops the wings flapping and the LED flashing. We tried to find out the reason but were unable to before time run out, so we decided to just went with what we have so far, which turned out to be not bad at all. With the remaining 5 minutes I decorated Rudolph the Pegasus before we presented him to Mr Chua, and below is our final product, in action.



After this practical, my irrational fear of programming has been proven to be untrue, and coding is not as daunting as I make it out to be. I had a lot of fun in this practical and learned many useful things that will come in handy when I do my FYP in the future.


 
 
 

Comments


Post: Blog2_Post

Contact

86884786

Follow

  • Facebook
  • Twitter
  • LinkedIn

©2022 by CP5070-2022-2B02-Group4-VanAnh. Proudly created with Wix.com

bottom of page