Week 5: Arduino Programming

Welcome back to my blog!

This week we went through Arduino programming. We had previously went through a practical that required us to make a self-moving unicorn. The team was able to do that and even added a few extra features too! 

For this blog, I was tasked to complete 4 different tasks, two for input devices and two for output devices. The first one is...

1) Interfacing a Potentiometer to the Arduino board & log its signal using the serial monitor

Let's first breakdown the task to its components:

A potentiometer is able to change its resistance through an adjustable knob. It also has an interesting feature in that it has three pins. In particular, the middle pin is called a wiper, which basically outputs the resistance indicated on the potentiometer. 

A serial monitor allows for a reading to be output. When specified in a pin, so long as that pin is connected to an input device like the potentiometer, a reading can be seen.

Using this knowledge, I started on the code:

Initially I wanted to blink an LED bulb using the output resistance from the potentiometer. The bulb allows me to verify if the signal is going out correctly through the frequency of blinking.

But first, I had to work without the LED bulb so that I can make sure that the potentiometer's connections are correct. I made the code such that when the potentiometer is adjusted, the serial monitor can detect that change. The set-up and code are as follows:


If you would like to make this physically, you'll need the .ino file! Click here for the testing code. The output should be seen just like in this video:

Putting the LED together to the set-up, I created the tasked work. The LED is connected to a ground (with a resistor in series to prevent major signal fluctuations that can cause it to spoil) and a power supply. How the code works is that the analog pin A0 collects the signal output from the potentiometer, puts that value into the serial monitor, and then executes to blink the LED (connected to pin 13, listed as LED_BUILTIN in the code) based on how much signal output is there. You can click here to see the code itself! Here is the simulation:

 
2) Interfacing an LDR to the Arduino board & log its signal using the serial monitor

Breaking down this challenge, it is similar to the previous one except this time, we are using an LDR. An LDR decreases its resistance as the surrounding area becomes brighter and vice versa. 

However, an LDR only has two pins, both of which are for the ground and power supply. I got stuck and thought, "How do I get the signal from the LDR?"

Well, if the LDR is connected to a ground and power supply, current would flow through it. We only want the current that is supplied after it goes through the LDR, and current only goes one way. Hence, I thought that a signal wire can be connected at the ground terminal of the LDR so that current can be transported to the analog pin! I put that in practice using the same code for testing the potentiometer but with a different set-up, as shown here:

 
With this simulation, I found that my hypothesis worked! I then used the same code for the potentiometer and LED. The code would work the same as last time, where the signal is obtained from pin A0, printed onto the serial monitor and then the LED blinks according to the signal number. This is what came out of it:


Through this practice of input devices, I learnt that they are pretty similar in compatibility. I was able to use the same code for both, despite them having different set resistances. I also learnt that some adjustments need to be made to allow input, such as when the LDR only has two pins. I hope to use that knowledge a lot more in the future, where I can troubleshoot Arduino problems through guess-and-check simulations.

Next, we move on to our output device tasks:

3) Interfacing 3 LEDs (Red, Yellow, Green) to the Arduino board & make it fade subsequently

Well, not much to break down but may as well:

LEDs are basically your light bulbs! These are just about the same as the ones we used from the previous tasks, however this time we need to put 3 of them on and of different colours. 

An LED is always connected to a positive and negative terminal. The positive connects to the power supply, while the negative goes to the ground. 

In an Arduino board, LEDs can be made to blink (which is just to turn it on and off after a short delay), or to fade. As the two other tasks were about blinking LEDs, I decided to try and challenge myself to make fading LEDs.

I followed a tutorial about fading LEDs and used the code obtained from there. It used a digital pin to set a variable for the bulb brightness (which defaults at 0, where it is off), and then counts up the variable by 5 in the range 0 to 255, then counts back down by 5 once it reaches at 255. There are small delays in between so that there is that fading effect.

Using this principle, I copied the code but on two other pins to facilitate as the other two LEDs. Click here if you want to download the code! Here is the set-up and simulation:


I tried making it physically and this is how it looks!



4)  Interfacing a DC Motor and making it turn ON and OFF using the Arduino board's built-in button 

As usual, let's break that down:

A DC Motor directly converts electrical energy to mechanical energy, and is often used for rotational devices such as fans. They need a ground and a power supply to work.

To turn it on and off, I had to program an ON-OFF switch. In the Arduino board, the input button is connected to pin 2 and labelled as INPUT_PULLUP. However for the purpose of the simulation, I only kept it in pin 2 and created a separate button.

I first started with just the button to test how it works. Instead of the DC motor, I replaced it with an LED bulb. 

How the code would work is that when the button is pressed once, increase an integer (intButton) that would light up the LED when intButton = 1, and turns it off when intButton = 0 or > 1. When the intButton is >1, the integer is set to 0 again. There is a slight delay so that the serial monitor can print out the intButton's number as well as to allow it to turn on and off. Here's the set-up:

I then did this physically. Click here if you would like the code! Here's how it looks:


I then used the same code but replacing the LED with the DC motor. Here's the set-up:

 
And with that I've completed the output device tasks! I learnt about the set-up required for these types of devices, which is to always have a resistor connected at the ground terminal. This helps the signal to fluctuate a lot less when the signal changes. I

All in all, I loved doing Arduino programming and most of the troubleshooting that I had to do. It taught me how beginner-friendly Arduino programming is. I felt like it was a nice balance between complicated code and easy-to-read code, and especially when there is many resources to refer to when doing any tasks. Much of the coding relies on logic-driven sentences, which I find useful in relation to what I learnt last semester, which was about control valves. I really hope to re-use this skill very soon and improve my logical thinking.

Thank you for reading!

Comments