top of page

Blog 2: Arduino Programming 💻

Dec 7, 2024
10 min read

Updated: Feb 2, 2025



Hello again, readers!! Welcome back to yet another CPDD blog. In today's blog, I will be sharing with you guys what I have done with Arduino programming and its interesting functions. Without further ado, let me share with you the 2 interesting tasks I did with Arduino.


Figure 1: A picture of how a Arduino board look like


For starters, Arduino programming is a way to write instructions on an integrated development environment (IDE) on a computer to tell the Arduino board what to do.

Furthermore, the Arduino board can be connected to sensors, motors and LED lights to make them work by typing a program in the IDE. Hence, Arduino can be potentially be used in many engineering prototypes as it is simple to use, cost-effective, and versatile. Having that in mind, I shall share with you the two interesting tasks I have done with Arduino!


Task 1: Using a potentiometer as analog Input and showing its signal in serial monitor


I decided to use a potentiometer as my analog input as it was fun and interesting way to adjust the resistance of the LED light manually and the code below shows how I did it. 🙂


The code that I used

int sensorPin = A0;

int ledPin = 13;

int sensorValue = 0;



void setup()

{

  Serial.begin(9600);

  pinMode(ledPin, OUTPUT);

}


void loop()

{

  int sensorVal = digitalRead(2);

  Serial.println(sensorVal);


  sensorValue = analogRead(sensorPin);

  digitalWrite(ledPin, HIGH);

  delay(sensorValue);

  digitalWrite(ledPin, LOW);

  delay(sensorValue);

}


Explanation of my code

I know it may sound dry and boring but I try my best to make it short and sweet!


int sensorPin = A0;

int ledPin = 13;

int sensorValue = 0;


In a nutshell, the first three lines of code establish variables called sensorPin, ledPin, and sensorValue with integer values of A0, 13, and 0, respectively, where A0 is referring to the analog input while 13 refers to the digital input.


Serial.begin(9600);

pinMode(ledPin, OUTPUT);


Moving on, under void setup() I want tasks to be initialized and run the program once. Hence, we have Serial.begin(9600) to initialize the serial monitor for debugging, allowing the Arduino board to send data to the computer. Whereas for pinMode(ledPin, OUTPUT), we are setting the LED pin 13 to output so it can control the LED.


int sensorVal = digitalRead(2);

Serial.println(sensorVal);

sensorValue = analogRead(sensorPin);


On the other hand, under void loop(), we want to create an integer variable called sensorVal to read the state of the digital pin 2. This means that it will check if the pin is set to HIGH (LED light is turned on) or LOW (LED light is turned off). Furthermore, Serial.println(sensorVal) will print out the value of sensorVal to the serial monitor, and it will display either 1 or 0, which means it is either HIGH or LOW, respectively. Now, I will be explaining the analog read of the code. sensorValue = analogRead(sensorPin); means that it will read the value from the analog sensor connected to pin A0 on the Arduino board. The potentiometer will also be connected to pin A0 to control the value to pin A0.


digitalWrite(ledPin, HIGH);

delay(sensorValue);

digitalWrite(ledPin, LOW);

delay(sensorValue);


Last but not least, the rest of the code is basically me coding to control the LED. digitalWrite(ledPin, HIGH); means that I want pin 13 to turn on as I wrote HIGH inside, and to turn off, u guessed it, it is digitalWrite(ledPin, LOW); which will turn it off pin 13 as I wrote LOW inside. Additionally, I also added a delay (sensorValue), which will introduce a delay sent to the LED light. However, in my case, I set the default to be 0 milliseconds, which will not introduce any delay sent to the pin. But if I turn the potentiometer knob, it will manually control the timing of the delay which will change it from 0 milliseconds to another value which will cause the light to blink faster or slower. Finally, that's the end of my code. I hope my explanation was comprehensive. 😀



Resources where I got it from

If you are wondering where I have gotten this source from, I gotten it from this YouTube link: https://www.youtube.com/watch?v=-EDYMQ9lczA and also resources from the PDF file from BrightSpace: Maker UNO Edu Kit Module.


Problems that I have encountered and how I troubleshoot it

Definitely there was problems that I have encountered when I was trying to make it work. First and foremost, as I was unsure of how the breadboard worked at first, I did not know how the tie points of the breadboard works. Hence, it causes the light bulb that I have randomly wire to, to not light up. To tackle this, I went on to YouTube to learn the basics of how the breadboard works.

Figure 2: Showing how the breadboard connects and gives electricity


The above shows two highlighted green row and column. This means that if a wire is connected to one of the holes shown above, that whole row and column will have electricity. This applies to the other rows and column on the breadboard too. By following this principle, I decided to connect the wires in the correct manner. Eventually, I managed to make it work yayy! 😄😄


Video 1: My failed attempt


As you can see from my failed attempt, I plug the 5V wire and the ground wire into the other side of the breadboard causing it not to light up. Hence, I decided to put it into the other side where the electricity will flow through the whole row causing the LED light to light up. I will show the workable LED light later.


Evidence that my code works



Video 2: My successful attempt


For my successful attempt, the LED light will blink faster or slower based on the resistance is given by the potentiometer. For instance, if I turn the knob anticlockwise, the delay will be lesser and the LED light blinks faster. Similarly if i turn the knob clockwise, the delay will be bigger and the LED light blinks slower. Last but not least, the serial monitor will also show the amount of times it is turned off as it it represented by the 0 in it.


Task 2: Using 3 LEDs to blink simultaneously with a pushbutton


Whereas for task 2, I decided to make the LED to blink as I thought that having LED lights to blink look like a traffic light and I was intrigued on how it can work. 🤔


The code that I used

void setup() {

  Serial.begin(9600);

  pinMode(2, INPUT_PULLUP);

  pinMode(7, OUTPUT);

  pinMode(8, OUTPUT);

  pinMode(9, OUTPUT);


}


void loop() {

  int sensorVal = digitalRead(2);

  Serial.println(sensorVal);

 

  for (int i=7; i<=9; i++) {

  if (sensorVal == HIGH) {

    digitalWrite(i, LOW);

  } else{

    digitalWrite(i, HIGH);

    delay(100);

    digitalWrite(i, LOW);

    delay(100);}

  }

}


Explanation of code

Serial.begin(9600);

pinMode(2, INPUT_PULLUP);

pinMode(7, OUTPUT);

pinMode(8, OUTPUT);

pinMode(9, OUTPUT);


Under void setup(), similar to task 1, I first initialize a serial communication between the computer and the Arduino board. Afterwards, I set pin 2 as the input pin with an internal pull-up resistor. So, by default, the PIN 2 will be in HIGH status when the button is not pressed but when the button is pressed, it will change the to LOW status. Imagine it as a ON/OFF switch.

Moving on, I configure pins 7,8 and 9 as output as I want to 3 LED lights to light up.


int sensorVal = digitalRead(2);

Serial.println(sensorVal);


Under void loop(), it will firstly read the digital state of pin 2 which will be stores as a integer value in sensorVal as it has integer value of 0 or 1 which means LOW (the button is pressed) or HIGH (the button is not pressed because of the pull-up resistor) respectively. The next line Serial.println(sensorVal); will display the value of sensorVal on the serial monitor.


for (int i=7; i<=9; i++) {}


Next, I use a for loop, which will iterate pins 7, 8 and 9 that will be stored as a integer value in i and it will continuously loop around those pins.


if (sensorVal == HIGH) {

    digitalWrite(i, LOW);

  } else{

    digitalWrite(i, HIGH);

    delay(100);

    digitalWrite(i, LOW);

    delay(100);}

  }


Afterwards, I use a if else statement which will do different actions IF condition are met or ELSE perform another action. So, for the above code, IF sensorVal status is HIGH, pins 7,8 and 9 will be LOW as the button is not pressed and it gives a status of HIGH. ELSE, pins 7,8 and 9 will be turned on after 100 milliseconds first as it is in HIGH and turn off for 100 milliseconds as it is LOW. The 3 pins will turn on and off one after another creating a blinking effect among them.


Anddd that's it of me explaining the code to you. I hope you found this explanation useful! 😄



Resources where I get the code from


Also not to mention, I got this code from the resources from the PDF file from BrightSpace: Fun with Arduino and also YouTube source : https://www.youtube.com/watch?v=X8dHbdhnGKY&t=31s. These sources does not directly show how I got the code as I only got inspiration from it and improvise my own. Furthermore, about how I manage to wire them correctly, I learnt the basics from this YouTube link: https://www.youtube.com/watch?v=MojSo7OtF9w&list=PLV6cmKvnKRs5geApVORPW79U6s3wpa0Ht. I find these resources helpful for me to kick start my own Arduino journey. 🙂


Problems that I have encountered and how I troubleshoot it



Words cannot describe how frustrated I was when I did this task as I have to think logically how the code works. The GIF above shows how I felt 😤. Without further ado, allow me to describe to you what problems I faced.


if(sensorVal==HIGH){

    digitalWrite(i, LOW);

  } else{

    for(int i=7 ; i<=9; i++) {

      digitalWrite(i, HIGH);

      delay(100);

      digitalWrite(i, LOW);

      delay(100);}}

  }

}


The code above shows the mistake that I have made. So whats wrong with this? Well, I made a mistake of putting the for loop under the else statement instead of putting it before the if else statement. This is wrong as the computer reads the code line by line instead of reading the whole statement like a human. Hence, if I type this code it will read the if else statement first which read digitalWrite(i, LOW). However, i is not defined first and I must define before I can run it.


Furthermore, i is only defined in the else statement under the for loop where int i=7. This i is a local variable inside the for loop and it would not read other i outside of the for loop. Hence, to correct my code, I must place the for loop outside of the if else statement to clearly define i.


Video 1: My failed attempt


From my video, you can see that there is an compilation error which means that the variable i is not defined in the if statement which causes an error to occur. To tackle this, I must define i throughout the if else statement by placing the for loop before the if else statement. If you want to see the corrected code, you can refer to the code above.


Evidence that my code works

Video 2: My successful attempt


From my successful attempt, I have now corrected the error and the code works. yayy. Not to mention, whenever I pressed the button, the LED light will turn on and off starting from pin 7 to 9. Therefore, making a blinking effect for 3 LED lights simultaneously.


My learning reflection


From all of the Arduino programming activities that I have done so far, I felt that why is it a must to learn Arduino program and how does it help me in my project for CPDD? Initially, I believed it was merely about coding to control hardware. However, as I delved deeper, I realized that Arduino programming involves understanding the interplay between software logic and hardware behavior, which is crucial for creating seamless automated systems.




Furthermore , I felt a mix of feelings ranging from confusion, frustration and surprises if I manage to troubleshoot it. This is due to the fact that I have many misconceptions about Arduino programming. Hence, there are some learning points I have learnt from Arduino programming.



First and foremost, Arduino thinks differently from humans. I encountered this problem when I am coding on the IDE, there were some logic errors that I have faced. Initially, I was asking myself, why this code not work and why is it wrong. However, I realized that it was because I thought that the computer reads code differently from humans. For instance, as mentioned earlier, the variable i must be defined in the whole loop and not only the else statement as the computer would not recognize any variables outside of the else statement. Hence, the process of dealing with logic errors made me realized I have to think from a perspective of the computer. Additionally, dealing with logic errors reinforced the importance of a growth mindset. While it can be frustrating to encounter unexpected results, each error presents an opportunity to learn and refine my problem-solving skills. Overtime, I will have more experience in coding Arduino and debugging my mistakes would be easier.


Secondly, it also teaches me that everyone's code is different but they result in the same outcome. Initially, I believe that there was a 'right' way to write a code for every tasks. However, when I asked my peers how they code their tasks, I realise that their codes look sightly different from mine. For instance, in task two, my peers went to define variable i two times as shown below. Compared to mine which I only define i once throughout the whole if else statement.

My code

Others

for (int i=7; i<=9; i++) {

  if (sensorVal == HIGH) {

    digitalWrite(i, LOW);

  } else{

    digitalWrite(i, HIGH);

    delay(100);

    digitalWrite(i, LOW);

    delay(100);}

  }

  if (sensorVal == HIGH) {

    for (int i = 7; i <= 9; i++) {

      digitalWrite(i, LOW);

    }

  } else {

    for (int i = 7; i <= 9; i++) {

      digitalWrite(i, HIGH);

      delay(100);

      digitalWrite(i, LOW);

      delay(100);

    }

  }

Hence, both code works but one of them is more efficient than the other. Also, I learned that Arduino program is not about getting the job done but rather the experiences you have with coding and this will lead one to be more efficient.


In conclusion, learning Arduino programming can directly impact my CPDD module project on the automated half-boil egg maker. I plan to use Arduino program to help me control the heating elements and sensors. Specifically, I will use what I code from the task and pre-practical to help me develop a temperature senor with a LCD display to tell me when the egg is ready.




That's it for my blog, Thank you for taking the time to read it it really means a lot! see you in the next blog!! 😄😄


 
 
 

Recent Posts

See All

Comments


Contact

500 Terry Francine Street

San Francisco, CA 94158

info@mysite.com

123-456-7890

Be in the Know

Subscribe to me for the latest update

Menu

Home

About

My Blogs

Follow us on

© 2035 by Printa. Powered and secured by Wix

bottom of page