Arduino Programming
- Dec 6, 2024
- 10 min read
Updated: Feb 1, 2025
Hey guys! Welcome back to my blog! Hope you guys have been doing good :)
In this blog, I'll be documenting my journey exploring Arduino Programming over the past few weeks. Specifically, I'll be going over 2 tasks, and how I went about completing them. These 2 tasks are:
Interfacing an LDR to the Maker UNO Board and measuring/showing its signal in the serial monitor Arduino IDE.
Interfacing 3 LEDs to the Maker UNO Board and programming them to blink thorugh using the push button on the Maker UNO Board to start/stop the action.
Before we dive in, I just wanna share how I felt before starting these 2 tasks.

To be honest, I’ve never been a fan of coding. My parents even wanted me to pursue Computer Science, but I told them it wasn’t for me. That’s how I ended up in Chemical Engineering. So, when I found out I had to code for this module, I wasn’t exactly thrilled. Let’s just say I didn’t have high expectations for enjoying this experience.
But enough of that! Let’s set aside this negative mindset and jump right into the documentation :)
Without further ado, Task 1 was to:
INTERFACE AN LDR TO THE MAKER UNO BOARD AND MEASURING/SHOWING ITS SIGNAL IN THE SERIAL MONITOR ARDUINO IDE
Let's dive into Task 1!
To start off, the foundation for these tasks is to create a code, so here's code I used:
#define LDRpin A0
int LDRValue = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
LDRValue = analogRead(LDRpin);
Serial.println(LDRValue);
delay(2);
}That's a lot of words and numbers! Let's break it down and explain the key parts of the code and how each part contributes to ensuring the LDR functions properly!
#define LDRpin A0
int LDRValue = 0; Firstly, I defined the LDRpin using the define function and designated its analog pin. In this case, I've designated the A0 analog pin to it. I defined an integer variable (0) to store the readings.
void setup()
{
Serial.begin(9600);
}I then moved on the setting up the void setup. I started off by using the Serial.begin function under void setup to allow for data to be sent from the Maker UNO Board to my laptop. I initialised the serial communication at a baud rate of 9600 bits per second, as this is the rate I've used for all the Arduino activites I've done in class.
void loop()
{
LDRValue = analogRead(LDRpin);
Serial.println(LDRValue);
delay(2);
}I finished the code by setting up the void loop to allow my code to work properly. I first used the analogRead function to read the voltage from the LDRpin and return a value to my laptop. I then used the Serialprint.ln function so that the values read by the LDR would be sent to the serial monitor. The println function prints the new values on a new line, making it easy to view a new reading on each line. I finished setting up the void loop using the delay function to pause the program before the LDR takes the next reading. I kept the delay short, at 2ms, so that I could obtain the values fast. This ensures the readings are updated frequently but are not too fast to process.
And that's it for my code for Task 1!
'Wow Ivan's so smart! I thought he said he didn't like coding? How'd he create such a complex one?"
I'm gonna come clean, I had no idea how to approach this at first. We'd never been taught how to interface an LDR in class. But hey, everything can't be spoonfed to us, right? This task was probably given to us to encourage independent learning and help us be more self-reliant. So, I did what anyone would do - I turned to the internet for help… a lot of it, actually hehe.
That's when I came across this website! https://www.instructables.com/Arduino-and-a-LDR-Light-Dependent-Resistor/

This website basically gave me the answer. Like straight up. It had insturctions on how to set up the Maker UNO Board and the breadboard, as well as the code, which made things a lot of easier for me! Of course, I didn't blindly copy and paste the code from the website. I mean I did, but I took the time to study it and understand why it is was written in that way, and that's the most important part! The learning process is all about picking up new things along the way!
So it was easy, right? Well apparently, I can't folllow simple insturctions. I still had a hard time setting the Maker UNO Board up. There were so many wires, and I kept plugging them into the wrong holes. I also didn't know that I could bend the resistor to make the ends to go in the right hole, so that had me stumped for a while. But in the end, I just looked at the photo properly, rearranged the wires, and bent the resistor into the right place, and my LDR could function properly!
In all honesty, I can't really go so in-depth with my struggles throughout this task cause I really just followed the website. I don't think I did anything wrong since one of the blog requirements was to cite my sources/references I anyway :D. But I still had fun, and that's what matters the most! Anyways, here are some photos and videos of how the LDR works!

As you can see, in this video, when I use my finger to block out all the light, the reading decreases from about 20 to 2, showing that my setup is working!
In this second video, I used the phone's flashlight function to shine light on the LDR. This made the value to increase to about 105! wow so cool
Alright! That's it for Task 1! Let's move on to Task 2!
INTERFACING AN LDR TO THE MAKER UNO BOARD AND MEASURING/SHOWING ITS SIGNAL IN THE SERIAL MONITOR ARDUINO IDE
For this task, here was the code I used:
const int buttonPin = 2;
const int redPin = 9;
const int yellowPin = 10;
const int greenPin = 11;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
}
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
flashLEDs();
} else {
turnOffLEDs();
}
}
void flashLEDs() {
digitalWrite(redPin, HIGH);
delay(200);
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, HIGH);
delay(200);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, HIGH);
delay(200);
digitalWrite(greenPin, LOW);
}
void turnOffLEDs() {
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
}Just like Task 1, I'm gonna break everything down and explain it :)
const int buttonPin = 2;
const int redPin = 9;
const int yellowPin = 10;
const int greenPin = 11; I first used the const int function to define the button and each LED to a certain pin number. In this case, I defined the button, red LED, yellow LED and green LED to pins 2, 9, 10 and 11 respectively.
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
}I then set up the void setup. I used the pinMode functions to set the button as the input, and the LED pins as the output. This allows me to control the on/off states of the LEDs through using the button.
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
flashLEDs();
} else {
turnOffLEDs();
}
}
As always, the void setup needs to be followed by the void loop. Under the void loop, I used the digitalRead command to read the current state of the button. I made the button work through using the if and else commands. The if condition is when the buttonState is low, which is when the button is pressed, and the if result is for the LEDs to flash. The else condition and result is hence the opposite, where the LEDs don't flash when the button is not pressed.
void flashLEDs() {
digitalWrite(redPin, HIGH);
delay(200);
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, HIGH);
delay(200);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, HIGH);
delay(200);
digitalWrite(greenPin, LOW);
}
I configured the way the LEDs flash using the void flashLEDs function. The delay function determines how long the LEDs stay on for. Since I wanted them to flash, I kept the delay short at 200ms. I also separated the code for the three LEDs so that they flash one at a time.
void turnOffLEDs() {
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
}I completed the code using the void turnOffLEDs function. This ensures that the LEDs are off at the end of the function by setting each pin to low.
And that's the code for Task 2 done and dusted!
'So you're incompetent at coding right? Since you probably just copy and pasted from a website again.'

NOPE. I was able to do this without using any sources (except maybe one, I'll dive into it later)! We'd already covered using LEDs in class and prgramming them to turn on with the push of a button, so I used my knowledge from then to help me start off, and finish this task. As for the layout of the board, I took the concepts from Task 1, and tried to copy the same idea, but with LEDs instead. It kinda worked, almost. - I'll dive into it in the next part.
Okay, this time, I can be more specific with the problems I encountered throughout this task since I relied more on my own thinking rather than referring to a website.
The first main problem was struggling program the LEDs to flash one by one.
You see, I wanted the LEDs to flash one by one, instead of making them flash all at once, to make it cooler (kinda). However, I didn't know how to do that. I only knew how to program something like this:
void flashLEDs() {
digitalWrite(redPin, HIGH);
digitalWrite(yellowPin, HIGH);
digitalWrite(greenPin, HIGH);
delay(200); // Wait for 200ms
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
delay(200); // Wait for 200ms
}
This code works, but I knew it would cause all the LEDs to light up at the same time. So, I did some research (and used ChatGPT), to find out how to make them flash one by one. I then found out that I had the split the digitalWrite and delay functions into three different parts for each LED, which allowed them to light up one by one. In case you need a refresher, this is the what I'm talking about:
void flashLEDs() {
digitalWrite(redPin, HIGH);
delay(200);
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, HIGH);
delay(200);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, HIGH);
delay(200);
digitalWrite(greenPin, LOW);
}
void turnOffLEDs() {
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
}After I solved the code, I was confident that the LEDs would flash once I pressed the button. But guess what? I messed up (you may have noticed that this is a common occurence from me if a hands-on acitvity is involved). Only two of my LEDs lit up, while the red one didnt when I pressed the button. My excitement after figuring the code out was short-lived :/.
I was certain the issue lay in the way I set up the board, not my code. So I examined everyhting, step by step.
Were the wires arranged correctly? Yes.
Was the resistor in the right place? - Yes.
Were the LEDs in the correct position? - Yes.
Was my board even plugged in? - Yes.

Where did I go wrong? At this point, I was stumped and started to disassemble everything :(
BUT WAIT - as I started to remove the LEDs, I realised that the red LED was inserted wrongly. The shorter and longer leg of the LED needed to be swapped for it to light up! I should've figured this out earlier since the red LED was the odd one out, and I could've compared its placement to the green and yellow LEDs when first troubleshooting the issue, but oh well, lesson learned.
Anyway, after reorienting the red LED so the short and long legs were in the right spots, the LEDs started flashing properly as per my code!
Don’t believe me? Here’s some proof with more photos and videos!

As you can see, the three lights flash one at a time, showing that my code works!
Alright! That's it for my 2 tasks. For my previous blog, I realised that I forgot to follow the guidelines for reflection writing, so let's try to summarise things better this time :D
Looking back, I found the LED activity easier compared to the LDR one. This was probably because we were guided on programming LEDs in one of our CPDD lessons, which allowed me to find a way to start developing my code. However, I still struggled. Even though I relied on my previous knowledge, troubleshooting issues like the incorrect orientation of the red LED taught me the importance of paying attention to small details during hands-on activities.

One thing I noticed was that many of my friends struggled with the code too, so asking for help didn’t always solve the problem. Many of them were even posting stories on Instagram, complaining about how hard it was. Of course, we couldn't give up. So, we instead ended up sharing research resources and working through our issues together. Working with my friends helped me understand different approaches to solving problems, even if they didn’t always work out.
It’s also humbling to realise how much more I need to improve in coding. Both my parents studied computer science, and they were making fun of me for struggling with something as simple as programming LEDs HAHAHAHAHA. But you know, ITS ALL ABOUT THE LEARNING PROCESS, and that's why we're here!

Despite the challenges, I’ve picked up some valuable new skills. I learned how to modify existing codes to achieve the desired outcome. I also discovered that if you want ChatGPT to give you an accurate code, you have to be SUPER DUPER SPECIFIC. I learned this the hard way, through multiple failed attempts in both this task, and the shark practical I had a few weeks ago (I'd rather not talk about the shark practical lol). What's most important is that I try my best to apply these skills in the future :D
Speaking of the future, I'm already trying to envision how I'm gonna use these acquired skills! Me and my groupmates need to develop an automated tea brewer next year. I can program LEDs to act as indicators to signal when the brewing process is complete. I can't confirm that this will happen, but maybe it will. Who knows? All in all, this experience has given me a glimpse of how coding can be used in practical applications, and I’m excited to explore more!
OKAY, that's it for this blog! It's been a tough, yet fun one to do! I hope it's been a good read for y'all :) See you guys in the next one! BYEBYE :DDDD



Comments