Arduino Programming

 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()

{

   

Serial.begin(9600);



 pinMode(ledPin,OUTPUT);

}


void loop()

{

  

sensorValue = analogRead(A0);

 

  Serial.println(sensorValue);



 sensorValue = analogRead(sensorPin);

 


digitalWrite(ledPin, HIGH);

 delay(sensorValue);

 


digitalWrite(ledPin, LOW);

 delay(sensorValue);

}

int sensorPin = A0; means the potentiometer is connected to analog pin 0

 

int ledPin = 13; indicates that the LED is connected to digital pin 13

 

int sensorValue = 0; declares  another integer variable to store the value of the potentiometer

 

 

Serial.begin(9600); means to start serial communication between Arduino and micro controller through usb cable with a maximum communicate rate of 9600 bits per seconds

 

pinMode(ledPin,OUTPUT); set pin 13 as the output

 

 

 

 

sensorValue = analogRead(A0); is to read the voltage levels

 

Serial.println(sensorVal); prints data to the serial port 

 

sensorValue = analogRead(sensorPin);Reads the value from analog pin, A0

 

Digitalwrite() is used to set the output of pin 13 to HIGH, turning ON the LED

Delay() is used to create a delay of 0 milliseconds

 

Digitalwrite() is used to set the output of pin 13 to LOW, turning OFF the LED

Delay() is used to create a delay of 0 milliseconds

 


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

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


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

Initially, we are not sure how to show any data on the serial monitor, then we decided to put in serialbegin(9600) in void setup and serial.println(sensorVal) and it works!

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





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()

{

 

 Serial.begin(9600);

 

 

 

 

pinMode(ledPin,OUTPUT);

}

 

void loop()

{

 

   

 

  Serial.println(LDRvalue);

 

 

LDRvalue = analogRead(LDR);

 

 

if(LDRvalue > 600)

 digitalWrite(ledPin, HIGH);

 else

 digitalWrite(ledPin, LOW);

}

int LDR = A0; is to define LDR pin as analog pin, A0

 

int ledPin = 13; is to define pin 13

 

 

 

 

Serial.begin(9600); means to start serial communication between Arduino and micro controller through usb cable with a maximum communicate rate of 9600 bits per seconds

 

pinMode(ledPin,OUTPUT); is to set pin 13 as the output

 

 

 

 

 

 



Serial.println(sensorVal); prints data to the serial port

 

LDRvalue = analogRead(LDR); reads analog values from the LDR

 


If the LDRvalue > 600, digitalWrite() is then used to set pin13 to HIGH, turning LED on

 

If the LDR < 600, digitalWrite() is then used to set pin13 to LOW, turning LED OFF

 

 

 

 

 

 

 

 


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

https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/172726?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequences.api.brightspace.com%2F172726%2Factivity%2F5400368%3FfilterOnDatesAndDepth%3D1

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

the difficult part about this was to cover my finger on the LDR to block the light as it was still on even after trying to block it. I finally managed to block the light with my finger after a few tries. 

  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

void setup()

{

  pinMode(11,OUTPUT);

  pinMode(12,OUTPUT);

  pinMode(13,OUTPUT);

}

void loop()

{

  digitalWrite(11,HIGH);

  delay(1000);

  digitalWrite(11,LOW);

  delay(1000);

 

 

digitalWrite(12,HIGH);

  delay(1000);

  digitalWrite(12,LOW);

  delay(1000);

 

 

 

 

digitalWrite(13,HIGH);

  delay(1000);

  digitalWrite(13,LOW);

  delay(1000);

}

 

 

pinMode(11,OUTPUT); sets pin 11 as an output

pinMode(12,OUTPUT); sets pin 11 as an output

pinMode(13,OUTPUT); sets pin 11 as an output

 

digitalWrite() is used to set pin11 to HIGH, turning LED on

delay(1000) creates a 1 second delay

digitalWrite() is used to set pin11 to LOW, turning LED off

delay(1000) creates a 1 second delay

 

digitalWrite() is used to set pin12 to HIGH, turning LED on

delay(1000) creates a 1 second delay

digitalWrite() is used to set pin12 to LOW, turning LED off

delay(1000) creates a 1 second delay

 

 

digitalWrite() is used to set pin13 to HIGH, turning LED on

delay(1000) creates a 1 second delay

digitalWrite() is used to set pin13 to LOW, turning LED off

delay(1000) creates a 1 second delay

 

 



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

https://www.youtube.com/watch?v=MojSo7OtF9w

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

when we upload the code, the LED did not light up as bright, so we change the resistors and it is brighter!

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






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

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

}

}

Pin 5 is constant

Pin 6 is constant

Pin7 is constant

Pin 3 is constant

Pin 4 is constant

Pin 2 is constant

 

 

pinMode(greenLedVehicle, OUTPUT); sets pin 5 as an output

 

pinMode(yellowLedVehicle, OUTPUT); sets pin 6 as an output

 

pinMode(redLedVehicle, OUTPUT); sets pin 7 as an output

 

pinMode(greenLedPedestrian, OUTPUT); sets pin 3 as an output

 

pinMode(redLedPedestrian, OUTPUT); sets pin 4 as an output

 

pinMode(2, INPUT_PULLUP) assigned pin 2 as an input port, this enable pin 2 to read a digital input base on the status of the switch connected to pin 2 with a pullup resistor.

 

digitalWrite() is used to set pin 5 to HIGH to turn on green LED

digitalWrite() is used to set pin 4 to HIGH to turn on red LED

 

if push button is pressed, input value is low, hence sensorvalue is low. digitalWrite() is used to turn

 

green LED from pin5 on, (5V)

yellow LED frompin6 off, (0V)

Create a 2 seconds delay

Yellow LED from pin6 off, (0V)

Red LED from pin 7 on, (5V)

Create a 1 second delay

Red LED from pin4 off, (0V)

Green LED from pin3 to on, (5V)

Create a 5 seconds delay

Green LED from pin3 to off, (0V)

Red LED from pin 4 to on, (5V)

Create a delay of 1 second

Red LED from pin 7 to off, (0V)

Green LED from pin 5 to on, (5V)

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

https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/172726?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequences.api.brightspace.com%2F172726%2Factivity%2F5400368%3FfilterOnDatesAndDepth%3D1
  1. Below are the problems I have encountered and how I fixed them.

when we upload the code, the LED did not light up as bright, so we change the resistors and it is brighter!
  1. Below is the short video as the evidence that the code/program work.



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

I feel that it is pretty engaging and fun to learn new stuff. Like in the pre practical, i search up information from the maker_uno_edu_kit_Modue from brightspace and google to see the explanantion for the codes and how they work. But i learnt an important lesson that, in coding, you can learn about the theory and concept behind the code but that will not make you understand. Initially, i do not know much so i had to ask my team mates and friends for help. 

But the e-learning and resources provided from brightspace is helpful in my understanding of Arduino programming and executing codes.

Overall, these sessions were fun and i hope to arduino programming again to execute more functions.

         

Comments