Blog 2: Arduino Programming
- harrenchd
- Dec 8, 2024
- 7 min read
Updated: Dec 9, 2024
WE ARE SO BACK.
Exams are finally over. What a time to be alive! The holidays are here! Everyone's going overseas, hanging out with friends, and here I am writing another blog.

Hope y'all are doing well, today's topic is on coding!
What is coding?

To put it simply, coding is the act of communication between a human's input and a computer's output, somewhat like giving a computer instructions on what to do. In this blog, I'm doing two different tasks on an Arduino Maker Uno board. This finicky board can read inputs and turn it into an output, such as moving a motor, turning a light on and much more! To prove that my code works, it essentially does the thing I want it to do.
Task 1a: Interfacing a potentiometer
For this task, I had to interface a potentiometer analog input onto the Maker Uno board and show its signal on the serial monitor in the programming software, Arduino IDE.
To the people with absolutely zero knowledge on coding and this particular programming language, let me break it down real quick.
Breakdown:
Jokes aside, here are what the words in those sentence mean!
Potentiometer:
A resistor with three terminals, a potentiometer is an adjustable voltage divider which has a sliding/rotating contact to adjust the voltage! This also sounds very confusing, but I don't really know how it works either. My bad.
Analog input:
By definition, analog is defined as relating to or using signals or information represented by a continuously variable physical quantity such as spatial position, voltage.
Serial monitor:
Basically shows whether the Arduino code is sending/receiving information to/from the Arduino board, and the time it is sent.
Reading that would have had one of two outcomes: completely confuse you, or understand just a bit better. Hopefully you're part of the latter, because it only gets worse from here.
The actual coding:
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);
}Again, this might seem like a whole lot of nothing, so I'll try my best to explain.
The first part of code called 'Structure' is usually in blue (though I don't know where the color went) and includes the commands setup(), loop() (setup and loop are actually in orange whoops) and more. These are the main elements of the Arduino code. setup() is used to execute any programs below it only once, while loop() is used to, well, loop the code over and over again. Most of these are just self-explanatory from the name, thankfully.
After that, we have the 'Functions' part of code! These are the fun ones (usually in orange), which give your Arduino board instructions! These include examples like turning on a pin on your board, making a sound and more!
We also have 'Variables' which set constants for your Arduino code, such as data types and integers. Examples include int, and only that because I don't use any other variable types.
With the basic information learnt, let's dive into what the code means!
Before void setup():
These 3 lines start with int. This means that whatever written after is declared as an integer value stored in the code. In this case, sensorPin is stored as A0, ledPin as 13 and sensorValue as 0.
void setup() part:
Serial.begin(9600) establishes serial communication between the Arduino board and the device connected to the board, in this case my computer. This connection is done using (most of the time) a USB cable.
pinMode(ledPin, OUTPUT) sets the pin set as the integer pinMode, pin 13, as the output device. This means that when an input is registered such as a button press, pin 13 will light up.
void loop() part:
The most confusing part! And probably the most informative.
Again, int appears. This time, it reads the input of pin 2 and sets it as sensorVal. Hence, the integer sensorVal varies when the input of pin 2 changes.
Serial.println(sensorVal) displays the value of sensorVal if it is pressed.
sensorValue = analogRead(sensorPin) reads the output of the sensorPin and sets it as the sensorValue value.
digitalWrite(ledPin, HIGH) turns the LED or output pin on. In this case, it's both!
digitalWrite(ledPin, LOW) is just the opposite of the aforementioned line, it sets the LED and output pin off.
Thanks to the information I found on Brightspace (specifically the Extra: Maker UNO Kit Module page), my job was made much easier to set up both the code and the board with its separate components!
Mistakes:
Obviously, it wasn't my first attempt that got me to code it successfully (I, too am a first-timer

after all) and there were some failures here and there. Most of them were minor mistakes of me not including a closing brace { after an opening brace }, which I managed to eliminate after arranging my code in a way to single out all braces. More notably, my first attempt was just straight up copying of the code I found in the Brightspace resource. This meant I didn't include the Serial.begin(9600) which lead to me being unable to track the signal in the serial monitor. Thankfully, these were simple fixes, and I managed to successfully code the program after about 5-10 minutes of troubleshooting.
Here's a video of how it looked after successful programming! Pay no mind to the subpar cable management.
On to the next!
Task 2: Interfacing LEDs
For this task, I thought of making a working mini traffic light. When the button on the Arduino board is pressed, the light would turn to red, then the pedestrian lights would turn from red to green. It was pretty fun coding this one! Here it is if you wanna copy it for yourself:
const int greenLedVehicle = 5;
const int yellowLedVehicle = 6;
const int redLedVehicle = 7;
const int greenLedPedestrian = 3;
const int redLedPedestrian = 4;
const int pushButton = 2;
void setup()
{
pinMode(greenLedVehicle, OUTPUT);
pinMode(yellowLedVehicle, OUTPUT);
pinMode(redLedVehicle, OUTPUT);
pinMode(greenLedPedestrian, OUTPUT);
pinMode(redLedPedestrian, OUTPUT);
pinMode(pushButton, INPUT_PULLUP);
digitalWrite(greenLedVehicle, HIGH);
digitalWrite(redLedPedestrian, HIGH);
}
void loop()
{
if(digitalRead(pushButton) == LOW)
{
digitalWrite(greenLedVehicle, LOW);
digitalWrite(yellowLedVehicle, HIGH);
delay(2000);
digitalWrite(yellowLedVehicle, LOW);
digitalWrite(redLedVehicle, HIGH);
delay(1000);
digitalWrite(redLedPedestrian, LOW);
digitalWrite(greenLedPedestrian, HIGH);
delay(5000);
digitalWrite(greenLedPedestrian, LOW);
digitalWrite(redLedPedestrian, HIGH);
delay(1000);
digitalWrite(redLedVehicle, LOW);
digitalWrite(greenLedVehicle, HIGH);
}
}It's a lot to explain with these two, as the code is longer than the first one. However, the bulk of code can be condensed to be explained as one, so I'll try my best!
Before void setup():
Remember the first activity? This time, const int is used to set things as constant integers. This means their values CANNOT BE CHANGED! The numbers here set the pins for the constants, meaning the pins the LED lights are connected to on the Arduino board.
void setup():
pinMode(xxx, OUTPUT) sets whatever is written in the xxx portion as the output. Likewise, pinMode(pushButton, INPUT_PULLUP) sets the button on the board as the input! Hence, when the button is pushed, an input is created. Lastly, digitalWrite(redLedPedestrian, HIGH) sets the red light for the pedestrian as on by default, while digitalWrite(greenLightVehicle, HIGH) sets the green light for vehicles as on by default.
void loop():
We're almost done! The first if statement if(digitalRead(pushButton) == LOW). This means that when the button is pressed (LOW means that the button is pressed and vice versa), whatever written below the if statement will happen. For the first segment, the green light for vehicles are off and the yellow lights turn on instead. This is given a delay of 2000, which is 2 seconds. It continues by turning off the yellow light and turning on the red light for vehicles, after which the pedestrian light will turn green after a delay of 1 second. The light is on for 5 seconds, then it turns red again until the button is pressed again. The vehicle traffic light turns back to green after the pedestrian lights have been off for 1 second,
This was again made much easier using the resources found on Brightspace (Extra: Maker UNO Edu Kit Module). Thanks to that, it was only the setting up of the lights that was more time consuming. This took me about 30 minutes to complete.
Mistakes pt. 2:
No matter how easy a task, I always find ways to do things wrong. Honestly, this part wasn't really my fault though. On my first attempt, I noticed that the yellow light for vehicles was not turning on no matter how many times I pressed the button. I figured it was something wrong with my configuration, but in the end when I replaced the light, it worked out fine. It could be due to the lightbulb not working or something, but I'm not sure as well.
Anyways, here's how it looked after successful programming!
And we're done with the two!
All in all, to me, coding in a word would be 'extensive'. There are so many things to learn, and it can be especially daunting for a beginner like myself. I'm honestly in awe of programmers out there who dedicate their entire lives to just this one thing, since it's something I don't think I could ever see myself doing. Respect to them, man.
In my opinion, it was an eye-opener to things I haven't experienced before. What more is there out there? What's to come? It changed my perspective of what I don't actually know. There's so much to learn out there that we cannot possibly learn it all in our entire life. I am a speck of dust in this universe. Woah.
Philosophical stuff aside, I think the largest learning point of this activity wasn't even the memorization of the programming language, but the patience and determination to complete the code. It's really hard to start out as a beginner coder, and at first, I was completely lost when we were tasked with this. I had zero experience before, and the simplest mistakes made always led to errors, which didn't exactly help, either. It was only thanks to my friends and resources online that I was able to even start at all.
That's about it for this blog! Again, thanks for the friends who helped me finish the activities, the creators online who made the resources, and you, for reading this to the end. Hopefully we'll meet again.



Comments