Skip to main content

Arduino Programing

 Arduino Documentation Blog Entry


There are 4 tasks that will be explained in this page:

  1. Input devices:

  1. Interface a potentiometer analog input to maker UNO board and measure/show its signal in serial monitor Arduino IDE.

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

  1. Output devices:

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

  2. Include the pushbutton on the MakerUno board to start/stop part 2.a. above

For each of the tasks, I will describe:

  1. The program/code that I have used and explanation of the code. The code is in writable format (not an image).

  2. The sources/references that I used to write the code/program.

  3. The problems I encountered and how I fixed them.

  4. The evidence that the code/program worked in the form of video of the executed program/code.

Finally, I will describe:

  1. My Learning reflection on the overall Arduino programming activities.



Input devices: Interface a potentiometer analog input to maker UNO board and measure/show its signal in serial monitor Arduino IDE.

  1. Below are the code/program I have used and the explanation of the code.

Code/program in writeable format

Explanation of the code

int sensorPin = A0;
int ledPin = 13;
int sensorValue = 0;
void setup()
{
pinMode(ledPin,OUTPUT);
}
void loop()
{
sensorValue = analogRead(sensorPin);
digitalWrite(ledPin, HIGH);
delay(sensorValue);
digitalWrite(ledPin, LOW);
delay(sensorValue);
}




  • Inside void setup, the pinmode function is used to establish A0 as an input and digital pin 13 as an output. Pin 13 is reflected as LED_BUILTIN on the code.

  • The code inside void loop uses a new function called analogRead to listen to the pin’s state. 

  • Comment lines (which helps to clarify what the programme is doing) are marked with double slashes, those are not really included in the final programme.

  • digitalwrite will help to turn the LED on or off depending on whether it is set on high or low.

  • delay function instead of pausing for a fixed amount of time, the value passed to the delay() function will change as the knob of the potentiometer is turned because each time through the loop it’s going to read the position again.




  1. Below are the hyperlink to the sources/references that I used to write the code/program.

This video shows how to write the code on Thinkercad, and transfer it to the Arduino Software. Then, upload it to the Arduino Uno board. 

Link: https://www.youtube.com/watch?v=yyG0koj9nNY



  1. Below are the problems I have encountered and how I fixed them.

The light wasn't light up at first. After we checked is the resistor is use wrongly, we switch to a value which higher than previous one. Then it is light up.



  1. Below is the short video as the evidence that the code/program work. https://drive.google.com/file/d/18_nnr4hhq4wYxNfhg7VoLJRrr279ZXoA/view?usp=share_link




Input devices: Interface a LDR to maker UNO board and measure/show its signal in serial monitor Arduino IDE:

  1. Below are the code/program I have used and the explanation of the code.

Code/program in writeable format

Explanation of the code

int LDR = A0;
int ledPin = 13;
int LDRvalue = 0;
void setup()
{
pinMode(ledPin,OUTPUT);
}
void loop()
{
LDRvalue = analogRead(LDR);
if(LDRvalue > 600)
digitalWrite(ledPin, HIGH);
else
digitalWrite(ledPin, LOW);
}

  • Pin 13 and A0 are established as the number for the LED pin and LDR pin respectively. It will stay constant and subsequently on when LED and LDR pin is written it will mean pin 3 and A0.

  • The Serial.begin(); open a serial communication between the Maker UNO and the computer. 9600 is the baud rate of the comunication. The serial monitor must use the same baud rate to view the information

  • When LDR status is less than or equal 300 the LED is high which means we will see the LED turn on. Same with the opposite.




  1. Below are the hyperlink to the sources/references that I used to write the code/program.

This video shows how to blink an LED on Thinkercad


Link: https://www.youtube.com/watch?v=yyG0koj9nNY


This video shows how to fade an LED on Thinkercad


Link:https://www.youtube.com/watch?v=X8dHbdhnGKY


  1. Below are the problems I have encountered and how I fixed them.

The resistor was not plugged into the correct hole. So, the light did not turn off at dark

 I reconstructed it again and ensure that all the items are attached tightly to the hole.


  1. Below is the short video as the evidence that the code/program work.



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

  1. Below are the code/program I have used and the explanation of the code.

Code/program in writeable format

Explanation of the code

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);
}
}

  • Programme creates 3 variables: LED 1, 2 and 3. This allow us to change the output pins without changing the entire programme.

  • Code in setup part tells the Arduino that pins 13, 12 and 11 will be outputs.

  • Loop portion is where the actual instructions are.

  • First 3 digitalwrite functions turn on 1 LED at a time with a 200ms delay between each light turning on. 

  • Next 3 digitalwrite function turn off the LEDs with a 300ms delay between each.




  1. Below are the hyperlink to the sources/references that I used to write the code/program.


  1. Below are the problems I have encountered and how I fixed them.

The LED wasn’t lighting up. As I had plugged out the wrong side of the LED which the short one (anode) should be connected to the resistor. Thus, I switch it and it works. 


  1. Below is the short video as the evidence that the code/program work. https://drive.google.com/file/d/1D6gPHwiOIUBguH494S3VkFed-NtRHs9X/view?usp=share_link



Output devices: Include pushbutton to start/stop the previous task 

  1. Below are the code/program I have used and the explanation of the code.

Code/program in writeable format

Explanation of the code

#include "pitches.h"
int melody[49] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
NOTE_C2, NOTE_D2, NOTE_E2, NOTE_F2, NOTE_G2, NOTE_A2, NOTE_B2,
NOTE_C3, NOTE_D3, NOTE_E3, NOTE_F3, NOTE_G3, NOTE_A3, NOTE_B3,
NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4,
NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_G5, NOTE_A5, NOTE_B5,
NOTE_C6, NOTE_D6, NOTE_E6, NOTE_F6, NOTE_G6, NOTE_A6, NOTE_B6
};
int sensorValue = 0;
int sensorLow = 1023;
int sensorHigh = 0;
const int ledPin = 13;
void setup()
{
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
// Calibrate for the first five seconds after program runs
while(millis() < 5000)
{
sensorValue = analogRead(A0);
if(sensorValue > sensorHigh)
sensorHigh = sensorValue;
if(sensorValue < sensorLow)
sensorLow = sensorValue;
}
digitalWrite(ledPin, LOW);
}
void loop()
{
sensorValue = analogRead(A0);
int pitch = map(sensorValue, sensorLow, sensorHigh, 48, 0);
tone(8, melody[pitch], 50);
delay(50);
noTone(8);
delay(150);
}


  • The Programmable Button is connected to pin 2 and GND. To use it, we need to configure it as INPUT_PUL-LUP.

  • Therefore for const int, the push button is = to pin 2.

  • Under void setup also configure it to input_pullup.

  • By setting the pushbutton to low, it set it so that when press the button the the board, the 3 LEDs will light up like 2a. 




  1. Below are the hyperlink to the sources/references that I used to write the code/program.

This video shows how to blink an LED on Thinkercad


Link: https://www.youtube.com/watch?v=yyG0koj9nNY


This video shows how to fade an LED on Thinkercad


Link:https://www.youtube.com/watch?v=X8dHbdhnGKY



  1. Below are the problems I have encountered and how I fixed them.



  1. Below is the short video as the evidence that the code/program work. https://drive.google.com/file/d/1B9caLN_-9ys3fE45uKRRjufIIMAGq0qG/view?usp=share_link


Below is my Learning Reflection on the overall Arduino Programming activities.

Learning Reflection in this Practical:

This was the great experience that I ever had before. It made me feel like I can be a programmer👩‍💻 in the future hahaha.😅 I learned the basics of coding which gave me a meaningful experience as we can create the things that we desired. However, there is still a bit of frustration as the breadboard holes are very tiny which is we will keep plugging in the wrong place. But when we see the code is working, we will be very satisfied and elated as our efforts are not wasted!!! Focus and care are required in the practical as the things are very small and soft (resistor and LED bulb). So, please stay calm and patient along the way.

Comments