Arduino Programming

In this blog, I will be documenting my experience with Maker UNO Kit & coding. 💻

Initially, coding was hard for me because I had no idea what language was being used. After watching many youtube videos about coding... It has become easier in the sense that I can understand around half of the language used. 👽

On this page, I will describe:

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
2. 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 listed above, I will describe:
    • The program/code that I have used and an explanation of the code. The code is in writable format (not an image).
    • The sources/references that I used to write the code/program.
    • The problems I encountered and how I fixed them.
    • The evidence that the code/program worked in the form of a video of the executed program/code.    
    3. My Learning Reflection on the overall Arduino programming activities. 

    1. Input:

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

    breakdown of code

    Explanation of each code

    int sensorValue = 0;

     

     

     

    void setup()

     

    {

     

      pinMode(A0, INPUT);

     

      pinMode(13, OUTPUT);

     

      Serial.begin(9600);

     

    }

     

     

     

    void loop()

     

    {

     

      // read the value from the sensor

     

      sensorValue = analogRead(A0);

     

    Serial.println(sensorValue);

     

      // turn the LED on

     

      digitalWrite(13, HIGH);

     

      // pause the program for <sensorValue> milliseconds

     

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

     

      // turn the LED off

     

      digitalWrite(LED_BUILTIN, LOW);

     

      // pause the program for <sensorValue> milliseconds

     

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

     

    }


    int sensorValue = 0;

    to store the sensor value read from the potentiometer using int because it’s an integer. 


    void setup()

     

    {

     

      pinMode(A0, INPUT);

     

      pinMode(13, OUTPUT);

     

      Serial.begin(9600);

     

    }

     

    pins are configured using the pinmode() function. 


    Pin A0 is configured as an input, so we can “listen” to the electrical state of the potentiometer which we will then monitor using the serial monitor. 


    Pin 13 is the output to control the LED. 


    To be able to send messages, the Arduino opens a new serial communication channel with serial.begin() which takes a baud rate argument (what speed to communicate), in this case 9600 bits per second. 


    anything after a set of slashes // is a comment which helps us to understand the code using simple English.

    void loop()

     

    {

     

      // read the value from the sensor

     

      sensorValue = analogRead(A0);

     

    Serial.println(sensorValue);

     

      // turn the LED on

     

     

    analogRead() checks the state of pin A0 (which will be a whole number from 0-1023), and stores that value in the variable sensorValue.


    serial.println() is used to allow us to observe a steady stream of numbers ranging from 0-1023 as we turn the potentiometer. These values will change instantly and appear on the serial monitor window. 

    digitalWrite(13, HIGH);

     

      // pause the program for <sensorValue> milliseconds

     

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

     

      // turn the LED off

     

      digitalWrite(LED_BUILTIN, LOW);

     

      // pause the program for <sensorValue> milliseconds

     

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

     

    }


     

    digitalWrite is an action to send digital signal to PIN13 to either release high voltage (HIGH) or low voltage (LOW) to flow past the LED light.


    In between this action “digitalWrite ()”, there is delay or pause to wait for sensorValue milliseconds. 

    Explanation of the overall code

    The potentiometer is connected to A0 (input) to control the amount of voltage flowing through the LED which is connected to Pin 13 (output). 


    The analog-to-digital converter (ADC) converts the analog voltage into a digital value that will be shown on the serial monitor window by adding the serial.Println() function into the code.




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

    https://www.youtube.com/watch?v=-EDYMQ9lczA



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

    I did encounter a problem when connecting the LED to the breadboard and spoilt one LED. I plug the shorter lead (cathode) into the breadboard and connected it below the middle channel of the breadboard instead of connecting it to the resistor. As a result, the LED did not light up when I turn the potentiometer. Hence, when I swap the position to the correct position, the LED did not light. It only lights up when I swap the LED with a new one. 😑


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




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

    breakdown of code

    Explanation of the code

    int ldr ; 

    void setup() 

    {

    pinMode (A0,INPUT);

    Serial.begin(9600);

    }

     

    void loop() 

    {

    ldr = analogRead(A0);

    Serial.println(ldr);

    delay(10);

    }

     


    int ldr ;

    to store the ldr value read from the LDR using int because it’s an integer. 

    void setup() 

    {

    pinMode (A0,INPUT);

    Serial.begin(9600);

    }

     

    Pin A0 is configured as an input, so we can “listen” to the brightness state of the LDR which we will then monitor using the serial monitor. 


    To be able to send messages, the Arduino opens a new serial communication channel with serial.begin() which takes a baud rate argument (what speed to communicate), in this case 9600 bits per second. 


    void loop() 

    {

    ldr = analogRead(A0);

    Serial.println(ldr);

    delay(10);

    }

    analogRead() checks the state of pin A0 (which will be a whole number from 0-1023), and stores that value in the variable ldr.


    serial.println() is used to allow us to observe a steady stream of numbers ranging from 0-1023 as we cover the LDR. These values will change instantly and appear on the serial monitor window.


    A delay or pause to wait for 10 milliseconds. 

    Explanation of the overall code

    The LDR is connected to A0 (input) to control the amount of voltage flowing through the LDR. 


    The analog-to-digital converter (ADC) converts the analog voltage passing the LDR into a digital value that will be shown on the serial monitor window by adding the serial.Println() function into the code.


    In this code, there is no output as another leg of the resistor is connected to the GND of the Arduino. 

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

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


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

    I did not encounter any problems because the code was easy to replicate and assembling the components was very smooth because it is very easy to set up on the breadboard. 👍😎


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







    2. Output

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

    breakdown of code

    Explanation of each code

    int LED1 = 11;

    int LED2 = 12;

    int LED3 = 13;

     

    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

    }


    int LED1 = 11;

    int LED2 = 12;

    int LED3 = 13;

     

    to store the LED value read from the respective pins using int because it’s an integer.

    void setup() {

      pinMode(LED1, OUTPUT);

      pinMode(LED2, OUTPUT);

      pinMode(LED3, OUTPUT);

    }

     

    Pin 11 is the output to control LED1. 

    Pin 12 is the output to control LED2. 

    Pin 13 is the output to control LED3. 

    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

    }

     

    digitalWrite is an action to send a digital signal to Pin 11, 12 & 13 to either on (HIGH) or off (LOW) the LED.


    In between this action “digitalWrite ()”, there is a delay or pause to wait for 200 milliseconds when the previous LED was on. Wait for 300ms when the previous LED was off. 

    Explanation of the overall code

    The LEDs are connected to Pin 11, 12 & 13 (output). There is no input as it is connected to the 5V of the Arduino. 


    When the voltage flows through the circuit it supplies energy to the LED to light up the LEDs. The LED will fade out after 200ms and 300ms. 



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

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


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

    The problem I faced was searching for a simplified code to program the LED to fade using simple functions and repeating it. I only used digitalWrite and delay to get the LED fade. I saw some examples online and from my friends using this code “repeatedly: for (brightness = 0; brightness <= 255; brightness ++);” which I could not comprehend at all. Hence, I kept on searching on Google to see if I could use digitalWrite and delay to create the same fading effect. 


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




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

    //PIN 3: Input for reading the button

    //PIN 11, 12 & 13: Output for controlling the LED

    int ButtonValue = 0;

    int Button = 3;

    int LED1 = 11;

    int LED2 = 12;

    int LED3 = 13;

     

    void setup() {

      pinMode(3, INPUT_PULLUP);

      pinMode(LED1, OUTPUT);

      pinMode(LED2, OUTPUT);

      pinMode(LED3, OUTPUT);

    }

     

    void loop() {

      int sensorVal = digitalRead(2);

      Serial.println(sensorVal);

     

      if (sensorVal != LOW)   { 

         //if button is pressed then turn the LED on!

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

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

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

       } 

       else {

         //else the button is not pressed, turn it off

        digitalWrite(LED1, HIGH);

        digitalWrite(LED2, LOW);

        digitalWrite(LED3, LOW);

        delay(500);

        digitalWrite(LED1, LOW);

        digitalWrite(LED2, HIGH);

        digitalWrite(LED3, LOW);

        delay(500);

        digitalWrite(LED1, LOW);

        digitalWrite(LED2, LOW);

        digitalWrite(LED3, HIGH);

        delay(500);

      }

      }


    ButtonValue is a variable with a starting value of 0.

    To store the LED value read from the respective pins using int because it’s an integer.

    Pin 11 is the output to control LED1. 

    Pin 12 is the output to control LED2. 

    Pin 13 is the output to control LED3. 


    if () {} else {} IF a condition is met, do something...ELSE perform another action... In simple English, if the button is not pressed, the light will be turned OFF, and if the button is pressed, the light will be ON.



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

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


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

    This code was hard to edit because I wasn't able to directly use the code given in the video hence, I had to keep on rewatching the video to understand what the new function means in order to edit the code to let the code work. E.g., for this function sensorVal != LOW, it gave me a tough time understanding what it meant so that i change edit the digitalWrite setting whether set it HIGH or LOW.

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



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

    Learning Arduino programming was a great experience😊 for me because I have never thought about learning programming in my free time. The requirements for all activities are very straightforward since the only thing we need to do is copy and paste.


    Learning programming is extremely time-consuming⌚. The more I learn about it, the easier it gets but I had to pay the price of spending half a day to understand the code. This is similar to me learning how to drive for the first time, trying to drift on a busy road. I may succeed, but I will be bound to crash. Hence, I planned my work to allow for adequate time to answer questions on this blog. Since all these codes are a little complicated, I uploaded all the videos and code that I made for this blog entry on Week 5 so that I wouldn’t have to do the last-minute coding for this blog entry. The code took me a day to figure out, thus if I had started this late I wouldn’t have learned anything as I would be busy copying and pasting codes and taking videos of the code running on the Arduino. Also, I always got bored when I just copy and paste the code because I do not know what each line of code does so a good practice is to spend some time searching or understanding what each code means.


    Overall, through these activities, I learned that time management🕗 is important as poor time management and work planning will reduce my productivity. The ability to look for creditable resources is important as well as some websites seem to publish creditable information however when I tried to run their code… There were some errors. Lastly, I learned that I dislike programming. I used to find programming cool but after exploring it... It is not something I would enjoy doing for a long time.💦