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

// C++ code

//

int sensorValue = 0;


void setup()

{

  pinMode(A0, INPUT);

  pinMode(LED_BUILTIN, OUTPUT);

}


void loop()

{

  // read value from the sensor

  sensorValue = analogRead(A0);

  // turn the LED on

  digitalWrite(LED_BUILTIN, HIGH);

  // pause the program for <sensorValue>

  // millieseconds

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

  // turn the LED off

  digitalWrite(LED_BUILTIN, LOW);

  // pause the program for <sensorValue>

  // millieseconds

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

}

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

To help do part 1, I searched online and came across a video by Autodesk Tinkercad. Video Link : https://www.youtube.com/watch?v=-EDYMQ9lczA 


This video taught me how to write the code in Thinkercad, before I transfer it to the Arduino Software. and uploaded it to the Arduino Uno board.



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

The LED light was not lighting up even after we were sure that the code on Arduino was correct. It took a while but we realised that the pair next to us had a similar problem and they solved it by switching the sides of the LED diodes. It turns out we had the same problem as them and so we solved it by switching the cathode and anode of our LED on the breadboard. 



  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

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

We found a video by MERT Arduino & Tech. 

Video link: https://www.youtube.com/watch?v=4fN1aJMH9mM 

The video uses a breadboard that is longer than ours but it’s ok because we manage to make some slight adjustments.

The same source also provided us with the Arduino code.

Link here: https://create.arduino.cc/editor/mertarduinotech/40f2d22f-7853-4882-b2a9-1b6d43ac81b0/preview 


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

For the first try, my pair forgot to include the LED light. Without it, it wasn’t really possible for us to see physically whether the LDR was working or not. Well on the Ardino software we could see that it was working because the serial monitor was picking up a reading but physically there was no change. 


It wasn’t until our teacher pointed it out to us that we realised we had forgotten to add the LED light. We add it in, in the end.


  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

/* A simple program to sequentially turn on and turn off 3 LEDs */

 

int LED1 = 13;

int LED2 = 12;

int LED3 = 11;

 

void setup() {

   pinMode(LED1, OUTPUT);

   pinMode(LED2, OUTPUT);

   pinMode(LED3, OUTPUT);

}

 

 

void loop() {

  digitalWrite(LED1, HIGH);    // turn on LED1

  delay(200);                  // wait for 200ms

  digitalWrite(LED2, HIGH);    // turn on LED2

  delay(200);                  // wait for 200ms      

  digitalWrite(LED3, HIGH);    // turn on LED3

  delay(200);                  // wait for 200ms

  digitalWrite(LED1, LOW);     // turn off LED1

  delay(300);                  // wait for 300ms

  digitalWrite(LED2, LOW);     // turn off LED2

  delay(300);                  // wait for 300ms

  digitalWrite(LED3, LOW);     // turn off LED3

  delay(300);                  // wait for 300ms before running program all over again

}

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

We use the video made by Drone How to help us for 2a. 

Video link: https://www.youtube.com/watch?v=e1FVSpkw6q4 

The programming code by the same person can be found here: https://www.dropbox.com/s/bivwdnehp7ln1iq/Sequential_blinking.ino?dl=0 


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

When the code was first uploaded, we noticed that the 3 LED started blinking just like how we programmed it to be. Just 1 problem, we realised that compared to the red and yellow LED, the green LED was considerably dimmer compared to the both of them. 


After checking it again, I realised that the resistor used for the green LED was different from the other 2 light bulbs and that it has a higher resistance. I think it was because of the higher resistance, which means lesser current which cause the green LED to be dimmer.


We managed to solve this problem by simply switching out that resistor to be the same as the other 2 LEDs.


  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

/* A simple program to sequentially turn on and turn off 3 LEDs */ 


int LED1 = 13;

int LED2 = 12;

int LED3 = 11;

const int pushButton = 2;


void setup() {

   pinMode(LED1, OUTPUT);

   pinMode(LED2, OUTPUT);

   pinMode(LED3, OUTPUT);

   pinMode(pushButton, INPUT_PULLUP) ;

}



void loop() 

{

  if(digitalRead(pushButton) == LOW)

  {


  digitalWrite(LED1, HIGH);    // turn on LED1 

  delay(200);                  // wait for 200ms

  digitalWrite(LED2, HIGH);    // turn on LED2

  delay(200);                  // wait for 200ms       

  digitalWrite(LED3, HIGH);    // turn on LED3 

  delay(200);                  // wait for 200ms

  digitalWrite(LED1, LOW);     // turn off LED1

  delay(300);                  // wait for 300ms

  digitalWrite(LED2, LOW);     // turn off LED2

  delay(300);                  // wait for 300ms

  digitalWrite(LED3, LOW);     // turn off LED3

  delay(300);                  // wait for 300ms before running program all over again

  }

  }

  • 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 the button on the the board is pressed 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.

I actually got help for the coding from a pdf file posted in my school e-learning site. But the good news is that I found that pdf online also. The link to download is here:  https://download.kamami.pl/p576422-Maker_Uno_Edu_Kit_Module.pdf 


Refer to pages 51 to 52. 

We did not use the exact coding in those pages as our part 2a was a bit different in that ours only uses 3 LEDs while theirs uses 5. Since 2b is a continuation of 2a we didn't want to redo our board from 2a to match the one in the pdf. Hence, we simply choose to pick out the parts with the push button and try to see if we can slot it in our code from 2a. Thus resulting in the code u see on top.🥳


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

As we were manually typing the code into the Arduino programme, there were times where we made the mistake in spelling out and the format of the words. This resulted in Arduino not understanding the commands that we were making.


Below is some of the mistakes:





The way of solving it is quite easy. We simply just went back and double check the code to correct the spelling mistake and format mistake we made.



  1. Below are the short videos as evidence that the code/program work.




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


To be honest, never would i have thought that i would come into contact with arduino programming, circuits board etc …🤣 in chemical engineering. But here i am🤩. I suppose it is a good skill to have.


Each group was given 2 maker uno kit to work with. Inside consist of many things: breadboard, circuit board, resistors, LED light bulb… to name a few. There were only 2 kit so in a group of 4 we split up into pairs to use 1. From there, we first look at the e learning package provided to us. There are many activities there that help to introduce us to the function of the kit. Such as making music sound and making LED bulbs light up.


Then we began with the 4 activities that are in the blog. Beside the problem mentioned on top, overall it was still manageable. There was a small problem for me because the breadboard has so many holes to plug the terminal in and the holes are quite dense. There were times where I kept plugging the end of the components in the wrong place, same with the circuit board. But it wasn't a big problem and so the whole process ran quite smoothly. 


Although there were some frustrations at first (mainly due to the code we use being wrong or the board was constructed wrongly), buttttt i felt quite happy after we completed those activities. There was a sense of achievement and while i guess this is just the basic of basic for coding  i am still quite proud of myself 🤩🥳



Comments

Popular posts from this blog

PROJECT Development

HYPOTHESIS Testing