Arduino Programming

 Hi everyone!!! In this blog entry, I will explain 4 tasks:

Input devices:

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

Output devices:

  • Interface 3 LEDs to maker UNO board and program it to perform something (fade or flash etc)​
  • Include the pushbutton on the MakerUno board to start/stop part 2.a. above


For each of the tasks, I will describe:

  • The program/code that I have used and explanation of the code. The code is in writable format 
  • 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 video of the executed program/code.


Finally, I will describe 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.


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

Code/program in writable 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(13, LOW);

  delay(sensorValue);

}




First I establish sensorValue as a variable that can change.


Next, in the void setup, I establish A0 as an input for the program, by using pinMode(A0, INPUT)

and pin 13 as an output by using pinMode(13, OUTPUT);

Serial.begin(9600); then starts the communication channel between the Arduino board and an external device, in this case the potentiometer.



In the void loop, sensorValue = analogRead(A0) reads the input of A0 and changes it into a variable, being sensorValue.

Serial.println(sensorValue); then reads the voltage value from A0, which is an integer from 0 to 1023, which is then stored in the SensorValue variable established earlier.

The value of voltage recorded is then used to determine whether the LED at pin 13 is on or off, as seen in  digitalWrite(13, HIGH); delay(sensorValue);

and  digitalWrite(13, LOW); delay(sensorValue);




































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

Potentiometer Analog Input With Arduino in Tinkercad


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


When I was first doing this activity, I had trouble getting the LED to change based on the position of the potentiometer. No matter how much or how little I turned the potentiometer, the LED did not seem to light up at all. However, after a bit of troubleshooting, I realised that one of the wires connecting the main Arduino board to the breadboard was not properly connected, so that was an easy fix. Other than that, both the programming and the wiring was rather simple to understand, and proceeded without setbacks.



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






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

Code/program in writable format

Explanation of the code

int light;





void setup() {

Serial.begin(9600);

}




void loop() {

  light= analogRead(A0);

  Serial.println(light);

  delay(100)
}

First, I define light as a variable that can be changed by being detected by the LDR.


In the void setup, Serial.begin(9600); then starts the communication channel between the Arduino board and an external device, in this case the LDR.


In the void loop, light= analogRead(A0); reads the input of the LDR and converts it into the light variable. The value of said variable is then printed out through the Serial.println(light); command, and then a delay of 100 milliseconds, or 0.1 seconds, is set before the value of light is displayed on the Serial Monitor.


























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

LDR with Arduino - Measure Light Intensity using Photoresistor


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


This activity, in my opinion, was even simpler than the last, and as such, I did not encounter any major problems during the process of performing this activity. The wiring of the LDR to the breadboard was simple enough to understand with the video as reference, and the code was extremely simple and easy to decipher and understand. However, I did not know how to access the Serial Monitor at first, but after figuring that out, everything else went well.


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)


Code/program in writable format

Explanation of the code


void setup()
{

  pinMode(9, OUTPUT);

  pinMode(11, OUTPUT);

  pinMode(13, OUTPUT);

}

void loop()
{

  digitalWrite(9, HIGH);

  delay(1000); 

  digitalWrite(9, LOW);

  delay (1000);

  digitalWrite(11, HIGH);

  delay(1000); 

  digitalWrite(11, LOW);

  delay(1000); 

  digitalWrite(13, HIGH);

  delay(1000); 

  digitalWrite(13, LOW);

  delay(1000); 

}


In the void setup, I establish pins 9, 10 and 13 as output through
pinMode(9, OUTPUT);
pinMode(11, OUTPUT); and
pinMode(13, OUTPUT); respectively.




Then, in the void loop, using digitalWrite, I turn LEDs 9, 11 and 13 on and off with a delay of 1 second. This is done by using digitalWrite(x, HIGH) or digitalWrite(x, LOW) to turn the LED on or off, where x is the LED number. The delay is then done by using delay(1000), which is 1000 milliseconds or 1 second. I then repeat this for the other 2 LEDs, where it then goes on a loop.
































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


LEDs & Breadboards With Arduino in Tinkercad




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

This activity was an absolute nightmare. At first, no matter what I did for the wiring, I was only able to make 1 LED light up, being the first one. After losing my mind while doing this, I took a step back and tried to understand why my program was not working, so I first started with the actual programming of the activity. 

When troubleshooting the program, I double and even triple checked the code to make sure everything was correct, but even with the correct code, I still could not get a proper result, so using my deduction, I realised that the only possible reason why my LEDs were not lighting up was due to improper wiring, similar to the first activity where I could not get the LED to light up. After fiddling with the connections for a bit, I realised that the two LEDs that were not lighting up were placed in the opposite direction, with the anodes and cathodes reversed, which was quite embarrassing. 


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






Include pushbutton to start/stop the previous task

Code/program in writable format

Explanation of the code

void setup() {

  Serial.begin(9600);

  pinMode(2, INPUT_PULLUP);

  pinMode(9, OUTPUT);

  pinMode(11, OUTPUT);

  pinMode(13, OUTPUT);

}

void loop() {

  int sensorVal = digitalRead(2);

  Serial.println(sensorVal);

  if (sensorVal == HIGH) {

    digitalWrite(9, LOW);

    digitalWrite(11, LOW);

    digitalWrite(13, LOW);









  } else {

  digitalWrite(9, HIGH);

  delay(1000); 

  digitalWrite(9, LOW);

  delay (1000); 

  digitalWrite(11, HIGH);

  delay(1000); 

  digitalWrite(11, LOW);

  delay(1000); 

  digitalWrite(13, HIGH);

  delay(1000); 

  digitalWrite(13, LOW);

  delay(1000); 

  } 
}

In the void setup, I establish a connection between the Arduino board and another device, in this case being the button. Then, I establish pins 9, 10 and 13 as outputs through
pinMode(9, OUTPUT);
pinMode(11, OUTPUT); and
pinMode(13, OUTPUT); respectively.


Then, in the void loop, int sensorVal = digitalRead(2) reads the input of pin 2, which is the button, and converts it into a variable.  Serial.println(sensorVal); then prints out said variable when the button is pressed. Then, using the if and else commands, I am able to perform different tasks based on what position the button is in. As seen from  if (sensorVal == HIGH), if the button is not pushed (in the "HIGH" position), the 3 LEDs would remain off.

If the button is pressed down, the program seen in the previous activity would be performed, with LED 9, 11 and 13 lighting up with a delay of 1 second before turning off.











































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


Pushbutton Digital Input With Arduino in Tinkercad

LEDs & Breadboards With Arduino in Tinkercad


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


At the start, I could not get the button to activate the rest of my code for some reason, and as such, I spent quite a long time trying to figure out why that was the case. I had actually forgotten how to program the button, as I was wondering which pin number it could possibly be connected to, but after looking through the resources provided to me both on BrightSpace and on Youtube, I realised it was connected to pin 2. After that, it was all down to applying what I had learnt in the previous Arduino practicals to program the button correctly.


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






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


Initially, similar to everything else that we have done for this module this semester, when I first heard that we would be doing programming, I genuinely thought that I had walked into the wrong classroom. Even though I was quite unprepared for the activity, I was quite excited to start learning how to program the Arduino board, as I have some background in programming, and I'm quite interested in programming as a subject in general. 


To start, we had a brief introduction to the Arduino board, as well as the starting activities. From the briefing, as well as the short videos and the ArticulateRise package that showed us how to get started with the program, I could tell that my past experience with programming would help immensely, and the confused looks on most of my classmate's faces further confirmed this for me. I picked up the basics of Arduino programming rather easily, and in no time, I even started modifying the code on my own. 


Despite my fast learning of the programming, however, I found myself struggling with the wiring of the Arduino board when it came down to installing more complex functions, such as the potentiometer and the LEDs, and so I had to seek the help of my partner, who was much more capable when it came down to the wiring procedure than I could ever hope to be. This really humbled me, as I was really confident going into the first actual "difficult" activity, as I found everything else to be a breeze. I realised that even though I was rather proficient in the programming aspect of the Arduino programming process, I had much to learn when it came to wiring.


After the first few activities, my partner and I found ourselves handling the later activities really well, as we were able to split our work efficiently, with my partner handling the wiring while I handled the programming. Even though we both had our strengths, I did take time in between writing lines of code for our activity to watch my partner carry out the wiring of the breadboard and the Arduino board, as I wanted to learn how to carry it out in the future as well. Similarly, she also watched me handle the code while I tried my best to explain my rationale with the program. I really learned a lot from my partner by doing this, so I do appreciate her help in this activity, and I could not have finished it without her.


Overall, the Arduino Programming activities were really fun and quite informative. Even with my past experience with programming, I learnt quite a bit that I did not know before, and I enjoyed myself throughout the entire process as well. Knowing that we are going to be making use of this Arduino programming for our CA1 was comforting and exciting to think about, as I can't wait to continue making use of this in the future modules, as well as applying what I've learnt in my future career! 😁


Comments

Popular posts from this blog

Project Development Blog Entry

Design for Experiment (DOE) Blog Entry

Gears Documentation Blog Entry