Arduino Programming

Reflection:

In this tutorial, I learnt about programming using Arduino. Programming was a new concept to me so this lesson was all a new and fresh experience for me. I had no background knowledge on programming at all before starting my tutorial lesson hence I was afraid that I won't be able to follow and complete any of the tasks assigned to us. However, since programming was all anew to me, I was kind of excited to learn new things as well despite feeling nervous for the tasks as I would get to learn something new and add it into my skills in the future once I was competent on it which was tested during our practical lessons.

In this lesson, I learnt about the basic use of the electronic components in the maker uno kit, how the Arduino microcontroller system works and modify a program in Arduino IDE and upload it into the Arduino board. Additionally, I learnt the different components in an Arduino maker uno board. 

The LED emits visible light when current passes through it, this will be shown and explained in further detail below where I interfaced a Potentiometer Analog Input, a LDR and 3 LEDs to the maker UNO board as part of my task. The LED is used together with a resistor to prevent it form burning and limit the current. The longer side of the LED is the anode (+) and the shorter side is the cathode (-). The anode must be connected to the power source while the cathode will be connected to the ground. LED is considered as an OUTPUT. To do connections in the breadboard, we can make use of the jumper wires. Resistors have no polarity so we can connect them in any way. These resistors are labelled with colors to read the resistance value of that resistor. The first two colors are single digits from 0 to 9, the third color indicates the multiplier and the last color indicates the tolerance. The input devices in the maker UNO board are the button on the board and potentiometer whereas the output devices are the LEDs (red, yellow, green) and buzzer on the board, DC motor and LDR. 

Input, output and microcontrollers can be explained using our human nervous system. For instance, when there is a heat, our sensory nerves detects this (input) sends the signal to the brain (microcontroller) to process the information and sends the desired action to our motor nerves (output).

In this activity I was assigned to interface an input or an output to a microcontroller (Arduino). There are 4 interfacing activities I had to do, 2 each for input (potentiometer & LDR) and output (3 LEDs & DC motor).

1. Input Devices

a.  Interface a Potentiometer Analog Input to maker UNO board and measure its signal in serial monitor Arduino IDE 

Guided video resource: Here

This program works by reading a Potentiometer, a variable resistor, using Arduino's analog input. the potentiometer will read the voltage, in this case 5V, from the Arduino board and send its signal to light up the LED connected to pin 13. The analog input A0 will gradually sense the changing electrical signal from turning the Potentiometer. The Arduino's analog to digital converter converts an incoming analog signal between 0-5V into a range of numbers from 0-1023 which is seen from the serial monitor where 0 means the LED light is OFF and 1023 means the LED light is ON. The potentiometer contains three pins where both ends will be connected to power 5V and GND each with a jumper wire. The middle pin of the Potentiometer will be connected to analog pin A0.

CODE: 

int sensorValue = 0;

The program starts with a variable sensorValue which is set to 0.

void setup()

{

  Serial.begin(9600);

To measure the signal in serial monitor, we simply have to add in Serial.begin(9600); to establish a serial communication between the Arduino board and the computer via a USB cable to communicate at 9600 bits per second. which will be placed at the starting of void setup() { .

  pinMode(A0, INPUT);

  pinMode(13, OUTPUT);

}

This is to initialize and establish input as analog pin A0 and digital pin 13 as the output

void loop()

{

  sensorValue = analogRead(A0);

This code inside the loop reads the value from the sensor called analogRead(); to listen to the pin's state A0.

  digitalWrite(13, HIGH);

This code means that the LED will turn on when the output is HIGH.

This will pause the program for sensorValue miliseconds. This means that the value passed to delay(); function which is sensorValue will change as the knob is turned on the Potentiometer. 

  Serial.println(sensorValue);

Serial.println(); is to see the values that the Potentiometer is registering to the serial monitor. In this case, the serial monitor will show 0 if the knob is turned to the left, and if the knob is turned to the right, it will show 1023.

  digitalWrite(13, LOW);

This code means that the LED will turn off when the output is LOW.

problems faced:

The problems I initially faced was setting up the serial monitor. I fixed this problem by adding Serial.begin(9600); in the setup and Serial.println(); in the loop. I learned that by adding these code, the communication between Arduino and the serial monitored can be established the values to measure the signal can be read. I made sure in the next activity, these two codes were added as well so as to read the values in the serial monitor.

Source code: Download code here - Potentiometer






b. Interface a LDR to maker UNO board and measure its signal in serial monitor Arduino IDE 

Guided video resource: Here

This program works by turning on the LED when it's dark by placing your finger/hand above the LDR to cover the light from passing through and turning off the LED when it's bright by removing your finger/hand from the LDR.

The resistor chosen in order for this program to work is to set a 10 resistor (voltage divider circuit) with the LDR and attached to one leg of the LDR and 220Ω resistor with the LED and attached to the longer leg of the LED. Since a resistor has no polarity, it can be connected to the 5V pin and GND pin. The purpose for the 10㏀ resistor with the LDR makes a voltage divider which helps Arduino to see an accurate and linear change in voltage.

CODE:

const int ledPin = 13;   

const int ldrPin = A0;

The variable const which stands for constant to modify the behavior of the variable, making it "read-only". In the code, the ledPin is set as 13 and will not be changed. The same goes for ldrPin being set as A0.

pinMode(ledPin, OUTPUT); 

pinMode(ldrPin, INPUT);

The jumper wire below the 220 resistor leg will be placed in digital 13 to initialize the LED pin as an output to control the LED while the other wire from the LED short leg will be placed in GND of the same column as digital pin 13 to initialize the LDR pin as an input. From one of the LDR legs, the wire will be connected in +5V to give power for the LDR. From both the other leg of the LDR and 10 resistor leg connected with the LDR, the wire will be connected to analog input (A0). The wire from the other end of 10 resistor leg will then be connected to GND.

The integer int ldrStatus = analogRead(ldrPin); reads the status of the LDR value. 

analogRead() is used as the ldrPin is connected to analog input A0.

if (ldrStatus <=300) {

digitalWrite(ledPin, HIGH);              

Serial.println("LDR is DARK, LED is ON");

}

else {

digitalWrite(ledPin, LOW); 

Serial.println("---------------");

}

}

The 'if' and 'else' condition is used to specify the LED to light up if the ldr status reads a value <=300 or otherwise, the LED will not light up. In this condition, digitalWrite(); is used as the LED pin is connected to digital pin 13. In this case, anything below 300 is considered dark enough so the LED pin will light up. The LDR sends values to Arduino from 0 (a state of complete darkness) to 1023 (maximum brightness). If you want the LDR to be more sensitive, simply decrease the value of 300.

To measure the signal in serial monitor, we simply have to add in Serial.begin(9600); to establish a serial communication between the Arduino board and the computer via a USB cable to communicate at 9600 bits per second. which will be placed at the starting of void setup() { and Serial.println(); to see the values that the LDR is registering the serial monitor which will be placed in the void loop() { In this case, the serial monitor will show "LDR is DARK, LED is ON" if the ldrStatus <=300 else, it will show "--------------" if ldrStatus >= 300. 

Note - the 'l' in Serial.println(); is a small letter L.

Source code: Download code here - LDR



What I learned from interfacing an input device to Arduino board:

I learned how to connect and do serial communication between the maker UNO board and Arduino IDE in my laptop. I learned how to add an input device like sensors (Potentiometer & LDR) to a microcontroller (Arduino) and read its signal from the LED emitting light through serial monitor.

2. Output Devices

a. Interface 3 LEDs (Red, Yellow, Green) to maker UNO board and program it to perform something (fade or flash etc) 

Guided video resource: Here

This program works by adjusting the LED's brightness using Arduino's analog output. To slowly fade the LED brighter or dimmer, the LED will be connected to one of the PWM pin (~). 

In this setup, the breadboard power and ground rails connect to Arduino's 5V and GND respectively. The LED's cathode (shorter leg) is connected to one leg of the resistor while the other leg of the resistor connects to ground. The anode (longer leg) is connected to PWM pin 9 in Arduino. Do the same for the two other LEDs which will be connected to PWM pin 10 and PWM pin 11 respectively.

CODE: 

int brightness = 0;

The program starts with a variable called brightness and sets it equal to 0.

void setup()

{

  pinMode(9, OUTPUT);

  pinMode(10, OUTPUT);

  pinMode(11, OUTPUT);

}

In the setup, pin 9, 10 and 11 are initialized as an output which will use it to send signals to the LEDs rather than an input which listens to the signals to the LEDs.

The program's loop in void loop() { uses a control that counts which has 2 for() { loops. 

 for (brightness = 0; brightness <= 255; brightness += 5) {

To fade in, it will count up by increments of 5 for the variable brightness from 0 (all the way off) to 255 (all the way on). This is the condition which will be added in the 1st for loop. 

Inside this counting loop, an output is set to pin 9, 10 and 11 each to the variable brightness. This will be added into the analogWrite(); function. After, a waiting time of 30 miliseconds is added, written as delay(30); 

 for (brightness = 255; brightness >= 0; brightness -= 5) {

To fade out, another counting loop is needed but it will be counting down by increments of 5 for the variable brightness from 255 (all the way on) to 0 (all the way off) which will be added into the analogWrite(); function with a waiting time of 30 miliseconds written as delay(30); 

Source code: Download code here - Fade 3 LED


 

b. Interface the DC motor to maker UNO board and program it to on and off using push button on the board 

Guided video resource: Here

This program works by controlling the DC motor with a push button. The push button will be added in tinkercad and as for the Arduino board, the push button will be set as pinMode (2, INPUT_PULLUP) as the maker UNO board has the inbuilt button set in pin 2. When the button is pressed, the motor will start and when the button is pressed again, the motor will be stopped.

In tinkercad, the push button will be connected to a 10kΩ resistor and the transistor will be connected to a 200Ω resistor. In the maker UNO board, however, only a 200Ω resistor will be connected to the transistor. A transistor (MPS2222A) would need to be added in this activity as it acts like an electrical switch for the DC motor. The transistor uses the small current eg 5V from the Arduino digital output to control the bigger current of the motor to switch the motor on and off. A transistor consist of an Emitter which will be connected to one of the wires of the DC motor, Base to be connected to the 200Ω resistor to the output pin 13 and a Collector which is connected to the 5V pin. The other end of the DC motor will be connected to GND.



CODE:

int button = 0;

The program starts with a variable called button and sets it equal to 0.

int antState = 0;

The variable for the anterior state of the button is set to 0.

int buttonOff = 0;

This means that the variable buttonOff, 0 = DC motor will be ON and 1 = DC motor will be OFF.

void setup() {

  pinMode(2,INPUT_PULLUP);// BUTTON as INPUT

  pinMode(13,OUTPUT);// DC MOTOR as OUTPUT

}

In the setup, pin 13 is initialized as an output which will use it to send signals to the DC motor rather than an input which listens to the signals to the DC motor. The built-in button in the maker UNO board is set in pin 2 with INPUT_PULLUP as the input.

void loop() {

  int button = digitalRead(2);

  if((button == LOW) && (antState == HIGH)){

    buttonOff = 1 - buttonOff;

  }

The program's loop in void loop() { uses a control that uses 2 if(); statements. The state of the button on digital pin 2 will be read. For the first if statement, the condition is if the variable button is LOW and the antState is HIGH, the DC motor will be ON as shown in the code buttonOff = 1 - buttonOff; 

  antState = button;

This will read the actual value of the button.

  if(buttonOff == 1){

    digitalWrite(13,HIGH); 

  }

  else {

    digitalWrite(13, LOW);

  }

}

The next if statement has a condition if buttonOff == 1 , which means if the DC Motor is OFF when the button is pressed, the output signal on pin 13 will be HIGH which indicates that the motor will be ON. This is shown in the video below when the LED light on pin 13 of the maker UNO board lights up when the button is pressed, the DC motor will turn on. Otherwise indicated by else { , if the condition is false when the DC Motor is ON, the output signal on pin 13 will be LOW which indicates that the motor will be OFF. This is shown in the video when the button is pressed again, the LED light on pin 13 disappears and the DC motor stops moving.

problems faced:

First, I had a problem trying to make the tinkercad simulation to work with the DC motor as initially, I didn't include a push button for the DC motor. I had thought that the red button in tinkercad was the inbuilt button however, that was not the case. I used the guided video resource to guide me in setting up the push button into my tinkercad simulation to turn on and off the DC motor. The next problem I faced in this activity was setting up the code to let the motor to be turned ON when the button is pressed once and to let the motor to be turned OFF when the button is pressed again. This was solved by inputting the if conditions in my coding with the help from the guided video resource. Lastly, I had a hard time getting the motor to start once the button is pressed. I set up my Arduino board like the one in my tinkercad but the motor couldn't start running. I realized my problem here was due to the resistor. At first I connected the 10kΩ resistor to the base of the transistor to the output pin 13. I changed the resistor to a 200Ω resistor and the motor started to work, so this might be one of the reason why the motor didn't start switching on even though my set up and code was correct.  





What I learnt from interfacing an output device to Arduino board:

I learned how to add an output device (LEDs and DC motor) to a microcontroller (Arduino) and program it to perform a specific task.

All in all, the tasks assigned was very frustrating but gave me the sense of accomplishment once the code was able to run with my set up on the Arduino board. It was frustrating because I didn't know how to change the codes to make it meet the requirement of the question. The most challenging activity for me was interfacing the DC motor to start and stop by using the push button. With the help from google and YouTube resources, as well as consult with my groupmates, we were able to start and stop the DC motor by pressing the button in the end. These tasks definitely gave me an insight on how input and output devices work with the Arduino board and helped me to understand how serial communication works. Looking forward, I hope to apply the coding skills applied in these activities in my future projects. After completing these 4 activities, I learnt that programming and coding is not easy and requires a lot of test trials to see where you went wrong and lots of patience to get the code you want to run successfully. 


References:

BYJUS. (n.d.). Transistor - Definition, Working Principle, Types, Transistor Diagram. [online] Available at: https://byjus.com/jee/transistor/. [Accessed 27 Nov 2021].


Comments

Popular Posts