Blog 2: Arduino Programming.
Updated: Dec 7, 2024
void setup() {
Serial.begin(BLOG 2);
while (!Serial);
Serial.println("Welcome Back!");
}
Hello and welcome back to blog 2. I hope by the end of this blog you will also be able to appreciate the section title for this opening.

In this blog, I'll be taking you guys along the journey I recently took to build another 21st-century competency: PROGRAMMING/CODING. To describe it briefly, it was mentally fatiguing and frustrating but also very rewarding.
I don't think anything quite compares to the joy you feel after you troubleshoot for hours on end only to realize your error is something as simple as missing colon or bad cable connections. I honestly didn't know whether to laugh or bang my head against a wall. To summarise my journey as a whole it went from learning how to interface devices with the Arduino. For an input, we used a potentiometer and for an output, we used LEDs. Honestly after having said and done all the tasks, I would say it is not too difficult to become at least semi-competent at using microcontrollers such as Arduino, especially in this day and age with endless resources such as ChatGPT and Arduino's website to guide you through or simply provide you the code with a simple description of what you want to accomplish. And I would recommend that everyone at least try it once.
WHY DO YOU KEEP PRESSING ME????!!
Because I value your input
Arduino inputs come in all sorts of shapes and sizes. Buttons, sensors, joysticks, switches, and even touchscreens! There are two different kinds of input, Digital and analog.

Digital inputs are binary in the sense that they only have two states. Binary means on or off, 1 or 0, there or not there, noodles don't noodles. You get the idea, in the context of a microcontroller, these inputs come in the form of buttons, switches, or sensors. As long as they only have two distinct states, they may be considered digital inputs. Analog inputs are a bit more tricky as they can vary continuously across a range of values depending on the nature of the input device itself, some of the simplest analog inputs come in the form of sensors that measure things like light intensity, sound, or temperature that have a variety of values. These values are read and output themselves as varying voltage values depending on what the sensor is measuring, this voltage is then measured and turned into a digital value that represents the intensity of the analog signal using something called an analog-to-digital converter.
This video by AddOhms, really helped me better understand the difference and I think he uses some more relevant analogies to aid this understanding so do check it out if my explanation was inadequate.
Why so mafan need to differentiate sooo specifically I thought plug-in can work is acceptable already. Why you want to work so hard

Understanding this distinction is important in the context of a microcontroller as they have designated pins to connect analog inputs to and designated pins to connect digital inputs. So understanding the difference is crucial to the practical aspect of even knowing what to connect where. You can see where they are on the maker uno using the image on the right.
That's enough theory for now, let us move on to applying what we've learned. I'll be showing off how an analog input looks in action in this case it is a potentiometer, also known as a variable resistor.
The code behind this was pretty simple. So simple that I just used ChatGPT and threw it into the maker uno. Jokes aside, I did take the time to dig through and understand the code itself and now can confidently say that I will be relying on ChatGPT to do the actual coding/programming...
int potPin = A0; // Pin where the potentiometer is connectedint potValue = 0; // Variable to store the potentiometer valuevoid setup() { Serial.begin(9600); // Start serial communication at 9600 baud rate}void loop() { potValue = analogRead(potPin); // Read the potentiometer value (0 to 1023) Serial.println(potValue); // Output the value to the Serial Monitor delay(500); // Wait for 500 milliseconds before reading again}Here is the not-so hard-earned code and let me talk you guys through it in a super simplified way.
Before I dive into the specifics let me tell you the basics, the Arduino will read the code from up to down in that order. So if function1 is above another function2 in the typed-out code it will do function1 before function2. " void setup {} " contains the function "setup" as such it contains things that only need to be run once. anything you put in the {container} is run once only before moving on
In this case, we use it to tell the Arduino to start sending data to the serial monitor at a rate of 9600 bits per second. That's what a baud rate is (1 bit per second), if you increase the value in the bracket it increases the number of bits sent to the serial monitor per second. But for our case, 9600 bits is more than sufficient.
"void loop {}" contains the function "loop" and usually contains the main function of the code as whatever is stored in its {container} is run repeatedly from up to down before going back up and repeating the code in the container over and over until it turns off or is otherwise specified to do something else.
The first line, "int potPin = A0" just defines a variable called potPin as pin A0 on the Arduino, we'll use this to tell the Arduino where we've connected our analog input (the potentiometer). The function int is short for Integer as this function can only store whole values. To describe it in a math way you can think of it as when you declare x to represent something. In this case, you could rephrase that line of code as "Let potPin = analog pin A0"
Similarly, the second line does the same but creates an empty variable for us to use to store our output value for the potentiometer later. "Let potValue = 0"
Now we can jump into the meat that is in our void loop.
The first thing we do is equate our empty variable potValue to the value that we read from the analog pin called potPin. Remember that potPin refers to pin A0
We then tell the Arduino to print this value(potValue) to the serial monitor on our computer using the Serial.println function. Before telling the Arduino to wait 500 milliseconds using the delay function before repeating the same process in our loop.
wow, that was so simple, programming so hard meh? In this case, our requirements are pretty rudimentary so we didn't face much trouble programming-wise.

However, I had some problems with the physical aspect of connecting it all. I had it all connected and was wondering why it was not working. But one YouTube video later I learned that each potentiometer pin has its designated purpose and has to be wired according to that. From this, I learned that you must also consider how to wire up your inputs to the Arduino and know where and what the pins mean and where they should go.
What goes up must come down. And what goes in must come out.
Now that we know the fundamentals of both programming and inputs we can move on to how to use these to output signals.
To demonstrate this I will be using the Arduino to turn on 3 different colored LED's in a particular order. While also using the onboard button on the Arduino as an on-and-off switch.
Wow, that was suuuuper easy, right?
No, not at all. But I'll get into that later, let's first try to understand the programming of the Arduino that was also obtained by prompting ChatGPT. (oh where would I be without AI)
// Define pin numbers for LEDs and buttonconst int ledPin1 = 8;const int ledPin2 = 9;const int ledPin3 = 10;const int buttonPin = 2; // Button connected to pin 2 (using internal pull-up)bool ledState = false; // State of the LEDs (on/off)bool lastButtonState = HIGH; // Previous button state (pull-up means HIGH when not pressed)bool buttonPressed = false; // Flag to indicate if the button has been pressedvoid setup() { // Set LED pins as output pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT); // Set button pin as input with internal pull-up resistor pinMode(buttonPin, INPUT_PULLUP);}void loop() { // Read the button state bool currentButtonState = digitalRead(buttonPin); // Check if the button has been pressed (transition from HIGH to LOW) if (lastButtonState == HIGH && currentButtonState == LOW) { buttonPressed = true; // Button has been pressed } // If the button has been pressed, toggle the LED state if (buttonPressed) { ledState = !ledState; // Toggle LED state (on/off) buttonPressed = false; // Reset button press flag } // If LED state is on, make LEDs blink one after another if (ledState) { digitalWrite(ledPin1, HIGH); // Turn on LED 1 delay(500); // Wait for 500ms digitalWrite(ledPin1, LOW); // Turn off LED 1 delay(100); // Wait for 100ms digitalWrite(ledPin2, HIGH); // Turn on LED 2 delay(500); // Wait for 500ms digitalWrite(ledPin2, LOW); // Turn off LED 2 delay(100); // Wait for 100ms digitalWrite(ledPin3, HIGH); // Turn on LED 3 delay(500); // Wait for 500ms digitalWrite(ledPin3, LOW); // Turn off LED 3 delay(100); // Wait for 100ms } // If LED state is off, keep LEDs turned off else { digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, LOW); digitalWrite(ledPin3, LOW); } // Save the current button state for next loop lastButtonState = currentButtonState;}
As you guys are now hopefully more familiar with how Arduino programming is formatted I'll break it into the setup phase and loop phase for easier understanding.
Setup Phase
The first portion of this code is similar to the input code. We are simply telling the Arduino where we are going to plug in the LEDs and where it should be sending signals. For instance,
const int ledPin1 = 8;we are simply saying that our first ledPin is at pin8 on the Arduino. Pins 8,9 and 10 have been named ledPin1, ledPin2, and ledPin3 respectively for easier reference during the rest of our code.
const int ledPin2 = 9;const int ledPin3 = 10;We also named the built-in button which for the Maker Uno is on pin2 as buttonPin similarly.
const int buttonPin = 2; // Button connected to pin 2 (using internal pull-up)You'll also notice that there's a new function called "bool" "bool" is short for boolean and for the uninitiated, it simply means that the data type that we declare using this function only has two states, TRUE or FALSE. Sounds familiar huh? In this case, we are using the bool function to create a variable where we will store the states of our button as well as the LEDs. For instance,
bool ledState = false; // State of the LEDs (on/off)is simply declaring that at the initialisation of this code, our ledState is false (or off) in this case.
Similarly, we create two data variables to later check if the button has been pressed, and call them buttonPressed and lastButtonState
bool lastButtonState = HIGH; // Previous button state (pull-up means HIGH when not pressed)bool buttonPressed = false; // Flag to indicate if the button has been pressedMoving into our void setup{ container }, we are telling the Arduino that the pins that we have just named ledPins are outputs and that signals should be sent through them.|
For instance,
pinMode(ledPin1, OUTPUT);is just saying that the pin we have named ledPin1 is an output. We do the same for the other two pins we have declared earlier.
pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT);Similarly, we declare our buttonPin (pin2) as an input variable to monitor and use later in our loop phase.
pinMode(buttonPin, INPUT_PULLUP);Loop Phase
Moving onto the void loop{ container }, the first thing you will notice is that we are constantly checking the state of the button at the start of the loop using a bool function and storing it as a variable named "currentButtonState"
We then use an if loop to determine whether the button has been pressed.
if (lastButtonState == HIGH && currentButtonState == LOW) { buttonPressed = true; // Button has been pressed }
This is checking two conditions before it can execute the code below it, It is checking if the button's previous state is HIGH (meaning the signal it was outputting before is high meaning the button hasn't been pressed) as well if its current state is LOW (low signal means that the button has been pressed).
And if these two conditions are met it changes the value of the variable "buttonPressed" to true.
When the value of "buttonPressed" is changed it runs the function in another if loop.
if (buttonPressed) { ledState = !ledState; // Toggle LED state (on/off) buttonPressed = false; // Reset button press flag }In this loop, it changes the variable of ledState from FALSE to TRUE. Or TRUE to FALSE, depending on the previous state. As well as resetting the state of the buttonPressed back to false so that the check at the start of the loop is still able to meet the conditions of the first if loop. When the state of ledState is true, the function runs which sends a high signal to each LED for 500ms. For instance,
digitalWrite(ledPin1, HIGH); // Turn on LED 1 delay(500); // Wait for 500ms digitalWrite(ledPin1, LOW); // Turn off LED 1 delay(100); // Wait for 100msSo this is essentially telling the Arduino to send a high signal to ledPin1 for 500ms and then a low signal for 500ms before moving on to the next LED. We must also tell the Arduino what to do when the ledState is false so we add an else condition.
else { digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, LOW); digitalWrite(ledPin3, LOW); }This section essentially tells the Arduino to just send a low signal (off) to all of the ledPins if the condition of ledState is not true.
That's about it for programming. But we've only won the battle, the war is still ongoing.
ALL THIS WIRING HAS GOT ME WIRED.
Yeah, the coding really was the easy part of this demonstration. As I still had to connect all the LEDs onto the breadboard and ensure that there was enough resistance to each of them so they didn't blow up in my face.
BUT THANKFULLY, and I really mean thankfully, there are online tools that can help to simulate how I should wire my breadboard to my Arduino before I do it for real on the actual board. TinkerCAD has an excellent online tool that lets you design your Arduino circuits and simulate them before you do it in real life. IT IS REALLY A LIFESAVER and saved me a whole bunch of real-life troubleshooting.
Despite the simulation doing most of the heavy lifting I still ran into a few issues that required me to deploy my troubleshooting skills.
It also tells you if your LED is going to explode in your face, so that is very helpful too. I'll spare you the details and just run you through the issues and fixes I did. ISSUE # 1
The LED would not light up at all, this was caused by the LED's anode and cathode not being correctly connected to the positive and negative flow of the current through the board. SOLUTION: Switch the anode and cathode of the LED by switching the terminals. ISSUE # 2
The green and yellow LEDs were way too dim to even notice if they were being turned on, I initially thought the LEDs were broken but then I realised they were just too dim. This was caused by the resistors being too strong and reducing the voltage provided to the LEDs making them way too dim. SOLUTION: Switch out the resistors on the dimmer LEDs for ones with lower resistance. All in all, the troubleshooting was not too bad because the simulation helped me ensure that my wiring was correct. Through this, I think I've learned the importance of planning out how to connect what, where, and how before actually doing it so that you can minimise the errors later.
Why did the mirror start a podcast?
(Because It had a lot to reflect on)
I think these activities were very eye-opening and helped me to further progress the goal I had mentioned in my first blog about wanting to develop even more competencies as well as wanting to see things through a more "engineering" perspective.
While these activities served as a good way to dip my toes into the field of programming/coding. I feel I still have a long way to go before I can confidently call myself competent at it. Without the help of the external resources I have mentioned such as ChatGPT, TinkerCAD, Arduino's Website as well as YouTube tutorials the progress I've made in this short amount of time would have certainly been much slower.
I think that this small dip has been enough to give me enough courage to dive in fully.
Nevertheless, I am grateful that I have been exposed to this as it has broadened my sense of appreciation for things in an "engineering" sort of way.
After having struggled through trying to make my wiring and programming as clean and simple as possible. I can better recognise the insight, experience, and effort it takes to create and design things that are as simple as possible. As such I have grown to appreciate good design and look for it in things that I use in my everyday life and have been taking for granted in the sense that I haven't fully appreciated them for what they are. An example of this occurred to me as I was writing this very blog. I went to heat up a snack in the microwave and it just occurred to me that I did not know how a microwave worked at all. To me, it is nothing more than a magical box that somehow magically heats up my food from cold to piping hot in the span of a few seconds all while taking up a small space in my kitchen. After this realisation. I went and spent the next few hours watching videos and reading up on how microwaves worked and was super intrigued and interested in what I learned. Instead of finishing up my blog... I WON'T BORE YOU WITH ANY MORE DETAILS BUT HERE'S A VIDEO ON HOW A MICROWAVE WORKS.
WITH THAT IT IS ONCE AGAIN TIME TO SAY GOODBYE. Once again I hope everything is going well in my reader's lives, if you are going through hard times I hope they end soon the way you feel now is not final. I hope my blog has served as at least some sort of entertainment if you didn't get any educational value out of it. But goodbyes are too sad. So instead let's say can't wait to see you again.



Comments