Arduino

 Arduino Documentation Blog Entry

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

1.    Input devices:

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

b.    Interface an LDR to the maker UNO board and measure/show its signal in serial monitor Arduino IDE

2.    Output devices:

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

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

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



1. Input devices: Interface a potentiometer analog input to the 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 sensorValue = 0;

 

void setup()

 

{

 

  pinMode(A0, INPUT);

 

  pinMode(13, OUTPUT);

 

Serial.begin(9600);

 

}

 

 

 

void loop()

 

{

 

  sensorValue = analogRead(A0);

 

Serial.println(sensorValue);

 

 

  digitalWrite(13, HIGH);


 

  delay(sensorValue);

 

  digitalWrite(LED_BUILTIN, LOW);

 

  delay(sensorValue);

 

}






- AO is set as input for the potentiometer to communicate with the Arduino board.













- the analog reading will be displayed on the Arduino software

 

2.    Below is the hyperlink to the sources/references that I used to write the code/program.

 -

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

It was difficult to connect the wires as well as run the code as there were errors that constantly popped up. Afterward, I used the tinkercad website to draft out what I would do and manipulate the virtual board until there were no errors and mistakes, and was to my liking before redo-ing it on the physical board again. 

 

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


 


 

Input devices: Interface an LDR to the 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

/*
  Analog Input

  Demonstrates analog input by reading an analog sensor on analog pin 0 and
  turning on and off a light emitting diode(LED) connected to digital pin 13.
  The amount of time the LED will be on and off depends on the value obtained
  by analogRead().

  The circuit:
  - potentiometer
    center pin of the potentiometer to the analog input 0
    one side pin (either one) to ground
    the other side pin to +5V
  - LED
    anode (long leg) attached to digital output 13 through 220 ohm resistor
    cathode (short leg) attached to ground

  - Note: because most Arduinos have a built-in LED attached to pin 13 on the
    board, the LED is optional.

  created by David Cuartielles
  modified 30 Aug 2011
  By Tom Igoe

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/AnalogInput
*/

int ldr ;

void setup()

{

pinMode (A0,INPUT);

Serial.begin(9600);

}

 

void loop()

{

ldr = analogRead(A0);

Serial.println(ldr);

delay(10);

}







































- LDR (light dependant resistor) input at A0 pin.

- Initialising serial communicator 



- reading ldr input

 

 

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

https://roboindia.com/tutorials/arduino-ldr-light-dependent-resistor/

 

 

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

Similar problems with the Input: Potentiometer 

 

 

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

// the setup function runs once when you press reset or power the board

void setup() {

  // initialize digital pin LED_BUILTIN as an output.

  pinMode(7, OUTPUT);

  pinMode(8, OUTPUT);

  pinMode(9, OUTPUT);

}

 

// the loop function runs over and over again forever

void loop() {

  digitalWrite(7, HIGH);  // turn the LED on (HIGH is the voltage level)

  digitalWrite(8, LOW);                    // wait for a second

  digitalWrite(9, LOW);   // turn the LED off by making the voltage LOW

  delay(1000);      

 

  digitalWrite(7, LOW);  // turn the LED on (HIGH is the voltage level)

  digitalWrite(8, HIGH);                  

  digitalWrite(9, LOW);   // turn the LED off by making the voltage LOW

  delay(1000);    

 

  digitalWrite(7, LOW);  // turn the LED on (HIGH is the voltage level)

  digitalWrite(8, Low);                     // wait for a second

  digitalWrite(9, HIGH);   // turn the LED off by making the voltage LOW

  delay(1000);                      // wait for a second

}




  • pinMode( 7, OUTPUT) refers to setting pin 7 as the output pin. This allows the led connected to pin7 to light up when a signal is received. It is the same for pinMode( 8, OUTPUT) and pinMode( 9, OUTPUT) 



  • Void loop allows the following code to repeat continuously


  • digitalWrite(7, High) refers to the LED connected to pin7 to light up when a signal is received.

  • Whereas digitalWrite(8, Low) refers to the LED connected to pin8 not lighting up 

  • A sequence of the following code allows the led to appear like it's blinking one after another. 

  • delay(1000) simply refers to allowing the led to pause for 1000 ms

 

 

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

https://drive.google.com/file/d/1jRS9TqMTMg1_o6uaYrtcmnoyylz6FZCy/view?usp=share_link 

 

 

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

Initially, one of the LEDs would not light up. Hence, I went to check the wires and resistors, ensuring I connected each of them correctly. It turns out that what was causing the problem was me not connecting the resistor properly. Since the components are small, it is easy to misconnect the wires or to not plug them properly. They are also very fragile so it was important to be gentle and take care of it.

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

void setup() {

  //start serial connection

  Serial.begin(9600);

  //configure pin 2 as an input and enable the internal pull-up resistor

  pinMode(2, INPUT_PULLUP);

  pinMode(13, OUTPUT);

}

 

 

 

 

void loop() {

  //read the pushbutton value into a variable

  int sensorVal = digitalRead(2);

  //print out the value of the pushbutton

  Serial.println(sensorVal);

 

  // Keep in mind the pull-up means the pushbutton's logic is inverted. It goes

  // HIGH when it's open, and LOW when it's pressed. Turn on pin 13 when the

  // button's pressed, and off when it's not:

  if (sensorVal == HIGH) {

    digitalWrite(13, LOW);

  } else {

    for (int i=0; i < 5; i++)

  { digitalWrite(13, HIGH);

    delay(500);

    digitalWrite(13,LOW);

    delay(500);         }

 

  }

}


This code is to allow pin13 to blink 5 times upon clicking the button before coming to a stop. 

  • Setting pin 13 as output causes pin 13 to light up when a signal is receive 

  • int sensorVal = digitalRead(2);: reads the input of Pin2 and causes it to on and off respectively when the button is pressed.

  • digitalWrite(13, LOW): Pin13 will not be on unless signal is send when button is pressed

  • digitalWrite(13, HIGH: Pin13 will be on when signal is sent when button is pressed

  • for (int i=0; i < 5; i++): If button is pressed, action will repeat 5 times

  • delay(500);         }: pause for 0.5 secs

 

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

-

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

There were not much problems faced in this part of the programming as the codes were provided in the learning package. However, it too me some time to understand how different codes work and how they communicate to the arduino board itself. There were errors that popped up too such as "} was not declared" and I simply had to seek help with the internet to fix my problem. 

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



 

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

Overall, this lesson on Arduino run for approximately 3 weeks++. This topic was very new to me as I have never tried programming or coding. Hence, this was a very eye-opening experience. When learning about Arduino and how to code, it seemed easy. It was simply manipulating 🚩 🚩 🚩 🚩a given code to your liking. However, what these 3 weeks taught me was that Arduino was so much more than that. In my opinion, coding was basically another form of language. The code communicates to the maker UNO board and the signal is transmitted to commands, allowing us to control the components connected to it like LEDs. As I was trying to complete the 4 challenges given, I thought just copying the code was enough. However, when changing the code to do a different command, errors were prone to occur. Hence, it took a long time to understand what was the problem and how we should go about fixing it. Some common errors were “... was not declared”, and “a function-definition is not allowed here before '{' token” that was when I had to turn to the internet to fix my problem by looking at the reviews and replies made by people suffering from the same problem. Additionally, my computer was also unable to read the correct com. When I plugged in the Arduino USB, there were a few coms that appear. By looking up this problem on the internet, his problem can be solved by simply going to the computer’s device manager -> ports (COM & LPT). This experience has also shown me how important it is to understand the codes and what they are for. For example, pinMode(10, OUTPUT) means that pin10 will receive signals and act accordingly to the code, digitalWrite(13, LOW): Pin13 will not be on unless the signal is sent when the button is pressed and delay(1000) means to is to allow a pause that lasts 1-sec. Furthermore, I also experienced using the Arduino tinkercad which allowed me to play with the Arduino board virtually before doing it in real life and this saved me a lot of time because, for any mistake I faced, I could simply delete and redo it. Compared to doing it on the physical board itself, I would have to unplug all the wires and check step by step to determine if my mistake was the wires connected, the faultiness of the wires, or my code. 

This brings me to the practical we did on Arduino where we had to program the Arduino and make a pegasus flap its wing using a servo. Manipulating the delay function allows us to control how fast or slow the wings flap, allowing us to have a very majestic flapping. Altering the code by changing the degree to which the servo turns also allowed us to control how dramatic the flapping was. The practical we did made us brainstorm ways to make our pegasus the nicest out of all the pairs. During that time, my team and I had to come up with the code by using what we have learned and taken notes of in the pre-practical. Of course, there were errors in the codes and we could only simply trial and error till our code works. The next challenge we faced was making the LED light up at the same time as the servo running. It worked initially but it stopped working. We came to the conclusion that because the power given out by the computer could be too strong, it caused the bulb to fuse and the servo chip to burnt. We then made sure that when we were changing anything connected to the board, we will unplug the USB so that no power can enter. All in all, I have come to realize that programming is a very tedious task and both physically and mentally challenging. This is because the wires and other equipment like the resistors are very fragile and can easily be disconnected if it's accidentally knocked into. It is also difficult to connect it properly because it's so small. Additionally, it is mentally challenging as errors in the codes are very common. Hence, a lot of patience is needed to ensure it runs smoothly. However, it is also a very fulfilling experience when an obstacle was overcomed, and seeing a successful end product  which was possible as I sought help from my peers and the internet after trying multiple times.




No comments:

Post a Comment