top of page

Arduino Uno Programming

  • shaira214
  • Oct 29, 2022
  • 3 min read

Updated: Nov 26, 2022

Arduino Documentation Blog Entry


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

  1. Input devices:

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

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

  4. Output devices:

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

  6. 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.

  • Code/program I have used and the explanation of the code.

​Code/program in writeable format

Explanation of the code

// C++ code // int sensorValue = 0; void setup() { pinMode(A0, INPUT); pinMode(LED_BUILTIN, OUTPUT); } void loop() { // read the value from the sensor sensorValue = analogRead(A0); // helpful single-line comment here digitalWrite(LED_BUILTIN, HIGH); // pause the program for <sensorValue> milleseconds delay(sensorValue); // Wait for sensorValue millisecond(s) // turn the LED off digitalWrite(LED_BUILTIN, LOW); // pause the program for <sensorValue> milleseconds delay(sensorValue); // Wait for sensorValue millisecond(s) }

  • Void setup(), the pinMode function (A0, INPUT) establishes A0 as input. (LED_BUILTIN, OUTPUT) tells us that Digital pin 13 is output.

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

  • Comment lines helps 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.

  • hyperlink to the sources/references that I used to write the code/program.


  • problems I have encountered and how I fixed them

Initially, the LED light bulb was not turning on, even though I rechecked the wires and connections multiple times. I then recalled that LED has a positive and negative side, and it could be a reversed-biased circuit. Hence, I swapped the direction of the LED and the LED lit up!


  • 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:

  • Code/program I have used and the explanation of the code.

Code/program in writeable format

Explanation of the code

//set pin numbers //const won't change const int ledPin = 13; //the number of the LED pin const int ldrPin = A0; //the number of the LDR pin void setup() { Serial.begin(9600); pinMode(ledPin, OUTPUT); //initialize the LED pin as an output pinMode(ldrPin, INPUT); //initialize the LDR pin as an input } void loop() { int ldrStatus = analogRead(ldrPin); //read the status of the LDR value //check if the LDR status is <= 300 //if it is, the LED is HIGH if (ldrStatus <=300) { digitalWrite(ledPin, HIGH); //turn LED on Serial.println("LDR is DARK, LED is ON"); } else { digitalWrite(ledPin, LOW); //turn LED off Serial.println("---------------"); } }

  • ​Pin 13 and A0 is established as the number for the LED pin. Pin AO is established as the LDR pin. 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 communication. 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, and vice versa.

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

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

After running the code, I realised that the LED light was not lighting up. I then saw that at the bottom corner of the Arduino UNO page, "no board selected" was shown.

I explored Arduino more, and I found out that I needed to select a board for our code to run. I clicked on "select board", clicked on "Arduino UNO" as that was the board I was using. Lastly, I clicked on the port I was using. After running the code again, the LED lit up!

  • 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.)

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

Code/program in writeable format

Explanation of the code

// C++ code

//

int animationSpeed = 0;

void setup()

{

pinMode(LED_BUILTIN, OUTPUT);

pinMode(12, OUTPUT);

pinMode(11, OUTPUT);

}

void loop()

{

animationSpeed = 400;

digitalWrite(LED_BUILTIN, HIGH);

delay(animationSpeed); // Wait for animationSpeed millisecond(s)

digitalWrite(LED_BUILTIN, LOW);

delay(animationSpeed); // Wait for animationSpeed millisecond(s)

digitalWrite(12, HIGH);

delay(animationSpeed); // Wait for animationSpeed millisecond(s)

digitalWrite(12, LOW);

delay(animationSpeed); // Wait for animationSpeed millisecond(s)

digitalWrite(11, HIGH);

delay(animationSpeed); // Wait for animationSpeed millisecond(s)

digitalWrite(11, LOW);

}


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

  • void setup() shows that pins 13, 12 and 11 are outputs.

  • void loop() 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.



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


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

After I uploaded the code, I realized that the red and yellow LEDS were blinking, except the green. I checked through the code and replaced the green LED several times, but the outcome was still the same.

I realized that the green LED was significantly dimmer than the rest, and it may be due to the resistance being higher for the green LED, causing brightness to be lower.


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



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

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

Code/program in writeable format

Explanation of the code

// C++ code

//

int animationSpeed = 0;

int buttoninput = 0;

void setup()

{

pinMode(2, INPUT);

pinMode(13, OUTPUT);

pinMode(LED_BUILTIN, OUTPUT);

pinMode(12, OUTPUT);

pinMode(11, OUTPUT);

}

void loop()

{

buttoninput = digitalRead(2);

if (buttoninput == HIGH) {

digitalWrite(13, HIGH);

} else {

digitalWrite(13, LOW);

}

animationSpeed = 400;

digitalWrite(LED_BUILTIN, HIGH);

delay(animationSpeed); // Wait for animationSpeed millisecond(s)

digitalWrite(LED_BUILTIN, LOW);

delay(animationSpeed); // Wait for animationSpeed millisecond(s)

digitalWrite(12, HIGH);

delay(animationSpeed); // Wait for animationSpeed millisecond(s)

digitalWrite(12, LOW);

delay(animationSpeed); // Wait for animationSpeed millisecond(s)

digitalWrite(11, HIGH);

delay(animationSpeed); // Wait for animationSpeed millisecond(s)

digitalWrite(11, LOW);

}


  • ​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.


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

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


  • 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 didn't expect myself to be learning more about Arduino, especially in Chemical Engineering. But, it is an important skill to have.


My group had 4 members, but we were only given 2 uno kits, causing us to split into 2 groups and work on the activities. The YouTube videos and tutorials provided allowed me to properly understand how to operate Arduino.


I felt that is was relatively easy plugging in the wires, resistors and LED into the breadboard. Tinker cad was also pretty straightforward as I was able to understand how to decipher the code so that it can run, and that our set-up would work.


Although there were some complications, I was able to run the codes myself successfully.

 
 
 

Comments


CP5070-2022-2B01-Group2-Shaira

©2022 by CP5070-2022-2B01-Group2-Shaira. Proudly created with Wix.com

bottom of page