blog 3 (arduino)

There are 4 tasks that will be explained in 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 a LDR to 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.


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;

Before the setup(), we create a variable to store to store the current value read from the potentiometer. It is called “int” because it is an integer. When “int sensorValue = 0”, the variable is 0. 

void setup()

void setup() is where you set up your board’s identity, and select your input and output pins. 

{

  pinMode(A0, INPUT);

  pinMode(13, OUTPUT);

}

 

Inside the setup, pins are configured using the pinMode() function. 

Pin A0 is configured as an input and Pin 13 is configured as an output to control the LED. 

void loop()

Void loop() enables the code to run in an infinite loop. 

{

  // read the value from the sensor

  sensorValue = analogRead(A0);

In the main loop, the function “analogRead (A0);” checks the state of pin A0, and helps to store that value in the variable “sensorValue”  

  // turn the LED on

  digitalWrite(13, HIGH)

Turns the LED on. 

// pause the program for <sensorValue> milliseconds

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

“delay (sensorValue)” indicates the time delay in milliseconds in between the turning on and turning off of the LED. 

// turn the LED off

  digitalWrite(13, LOW);

Turns the LED off

// pause the program for <sensorValue> milliseconds

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

}

“delay (sensorValue)” indicates the time delay in milliseconds in between the turning on and turning off of the LED. A loop is created due to the “void loop()” code indicated above. 


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

https://www.instructables.com/Arduino-Potentiometer-Analog-Input-Tinkercad/

 

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

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

During this activity, a problem that I faced was ensuring that the set up would work. Since we did not have much experience navigating the breadboard, it was initially challenging to place the connections at the appropriate connection points. Initially, the LED did not turn on as I had not made the positive connections and negative connections. Hence I had to carry out some trial and error before the connection points matched up to the one shown in the video. 

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

const int ledPin = 5; // digital pin 5 const int 

 

const int ldrPin = A0; // analog pin 0

 

The LED is connected to digital pin 5. 

The LDR pin is connected to analog pin A0.

void setup()

void setup() is where you set up your board’s identity, and select your input and output pins. 

Serial.begin(9600);

 

Serial (9600) establishes the serial connection between the Arduino board and another device. 

pinMode(ledPin, OUTPUT);

pinMode(ldrPin, INPUT);

Indicates that LED pin is the output.

Indicates that the LDR pin is the input.

void loop()

Void loop() enables the code to run in an infinite loop. 

int ldrStatus = analogRead(ldrPin);

 

if (ldrStatus <= 200) {

 

digitalWrite(ledPin, HIGH);

The LED is turned on when the light detected by the LDR is below a certain level (when the LDR status is below 200).

Serial.print("Darkness over here,turn on the LED : ");

 

 

A indication “Darkness over here, turn on the LED” is provided to turn on the LED. 

 

Serial.printl(ldrStatus);

 

} else {

 

digitalWrite(ledPin, LOW);

 

Serial.print("There is sufficeint light , turn off the LED : ");

 

Serial.println(ldrStatus);

 

}

 

}

 

An indication “There is sufficient light, turn off the LED” is provided to turn off the light when the light detected by the LDR is of a certain level. 


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

https://create.arduino.cc/projecthub/electronicsfan123/interfacing-arduino-uno-with-ldr-8760ba


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

During this activity, I had some difficulty finding the appropriate code to use. Unlike the previous activity, there was not any resource available for reference on Brightspace. Hence this required me to do some research online to find a suitable code to use, which was slightly time consuming.

Since I learnt the importance of proper connections from the previous activity, this time I was more careful to double check on the connection points before attempting to run the code since I wanted to be more time efficient! 

 

 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

int animationSpeed = 0;

It is called “int” because it is an  integer. This indicates that animationSpeed is 0. 

void setup()

void setup() is where you set up your board’s identity, and select your input and output pins. 

{  pinMode(13, OUTPUT);

  pinMode(12, OUTPUT);

  pinMode(11, OUTPUT);

}

Pin 13 is established as output. 

Pin 12 is established as output. 

Pin 11 is established as output.

void loop()

Void loop() enables the code to run in an infinite loop.

{

  animationSpeed = 400;

  digitalWrite(13, HIGH);

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

  digitalWrite(13, LOW);

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

When animationSpeed = 400, there is a 400ms delay in between the LED lights at pin 13 turning on and off. 

digitalWrite(12, HIGH);

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

  digitalWrite(12, LOW);

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

When animationSpeed = 400, there is a 400ms delay in between the LED lights at pin 12 turning on and off.

digitalWrite(11, HIGH);

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

  digitalWrite(11, LOW);

}

When animationSpeed = 400, there is a 400ms delay in between the LED lights at pin 12 turning on and off.


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

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

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

During this activity, I had accidentally connected the anode and cathode of the LED lights incorrectly. One of the LED lights had refused to light up eventhough there was no error with the code. Initially I was confused and thought that the LED lights were faulty. Before I proceeded to change the LED light for a new one, I came to realise that the wire was not in series with the cathode of the LED hence resulting in the LED not turning on. After I made the amendment, I tried to run the code again. This time it worked!

 

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 writable format

Explanation of the code 

int buttonState = 0;

It is called “int” because it is an  integer. This indicates that buttonState is 0.

void setup()

void setup() is where you set up your board’s identity, and select your input and output pins

{

  pinMode(2, INPUT);

  pinMode(13, OUTPUT);

}

Pin 2 is established as input. 

Pin 13 is established as output

void loop()

Void loop() enables the code to run in an infinite loop.

  // read the state of the pushbutton

  buttonState = digitalRead(2);

  // check if pushbutton is pressed. if it is, the

  // button state is HIGH

 

  if (buttonState == HIGH) {

    digitalWrite(13, HIGH);

  } else {

    digitalWrite(13, LOW);

  }

If the button is pressed, it is HIGH and thus turns on the LED and vice versa.

  delay(10); // Delay a little bit to improve 

simulation performance

}

 

There is a delay in between of 10 milliseconds to help improve the stimulation performance.

 


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


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

 

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

After doing all 3 activities, during the 4th one, I already had gotten the hang on operating the breadboard and maker uno kit. Hence I made sure to look out for the anode and cathode, as well as the connection points to ensure everything was in place before I ran the code. Hence this time I did not face issues. 

 

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.


Before discovering Arduino a few weeks ago, I did not have much prior exposure to coding. In fact coding always terrified me because I have heard that it was very tedious and one small mistake in a code could lead to the entire code being invalid! So naturally I was intimidated when I got to know that our CPDD curriculum would consist of coding. Nonetheless I was determined to get through it with a positive mindset, hoping that I could learn something at the end of the day. When Arduino was first introduced by Mr Chua, I wasn’t present as I was still recovering from covid. Thankfully, I got to access the online learning package which educated me on the things I should know about the Arduino kit before I get to use it. In the Brightspace materials, there were sufficient information on the different parts of the Arduino board and the different components in the kit itself. The package also gave me a preview to how the Arduino IDE software would look like and how to navigate it.

 

With the accumulated knowledge, we were free to explore the software and the kit while using the maker uno learning package as a guide.


Through carrying out the 4 activities in the MakerUno learning package (Hello world, programmable button, make some noise and servo), I got to familiarise myself with the technical terms used in MakerUno. Moreover, I enjoyed doing the learning package as there were opportunities for us to experiment and modify original codes according to the requirements of different scenarios. 


With this accumulated knowledge, we carried out a practical session where we had to make the wings of a Pegasus flap. To execute this action, we were required to use a servo motor. Since we had already coded for a servo before, we were not completely clueless on where to start. Since we wanted our idea to be unique, we decided to add LED lights for the eyes of the Pegasus. To take it a step further, we decided to code for a melody that will play when our Pegasus flaps its wings. Our melody was "We wish you a merry Christmas" so I guess you can say Christmas came early this year 🤭 (hahah). 

Something unique that we discovered was that the servo actually moves to the rhythm of the song! We were very fascinated by this feature and spent some time experimenting on the limits to implement so that our Pegasus flaps its wings smoothly and vigorously. We also used extra materials such as the glue gun, cardboard pieces to form our final product!

Here is a picture of our end product! 



Here is a video of our Pegasus flapping its wings: 


Overall, I am very thrilled to have gotten to learn so many things from the past few CPDD lessons. I never thought I would enjoy coding, but I did have a great time experimenting with the IDE program and also working with my team members during the practical! I hope to apply the knowledge I have picked up from these few weeks and apply them in future CPDD related CAs and activities! 😇

Comments