Arduino Programming

Arduino Programming 

Arduino Assignment:                                                                                                  18 November 2021

1. Input Devices

a. Interface a Potentiometer Analog Input to maker UNO board and measure its signal in serial monitor Arduino IDE

b. Interface a LDR to maker UNO board and measure 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. Interface the DC motor to maker UNO board and program it to on and off using push button on the board


1a: Input Devices -Interface a Potentiometer Analog Input to maker UNO board and measure its signal in serial monitor Arduino IDE


Simulate using Tinkercad:



Link to Tinkercad Simulation: 1a Tinkercad


To obtain the code, simply open Arduino, click on File -> Examples-> Basics-> AnalogReadSerial

Copy and paste in the Code under Text


Maker Uno: 

Connect the potentiometer to the breadboard. Connect three wires from the potentiometer to the breadboard. The first goes from Left pin of the potentiometer to 5 volts. The second goes from the Right pin of the potentiometer to ground. The last goes from the middle pin of the potentiometer to analog pin A0


Upload the code to the Maker-Uno and open the serial monitor. (Click on Tools-> Serial Monitor)


The code:

// the setup routine runs once when you press reset:

void setup() {

  // initialize serial communication at 9600 bits per second:

  Serial.begin(9600);

}

 

// the loop routine runs over and over again forever:

void loop() {

  // read the input on analog pin 0:

  int sensorValue = analogRead(A0);

  // print out the value you read:

  Serial.println(sensorValue);

  delay(1);        // delay in between reads for stability

}

 

Link to code: Code 1a


How the program works:

Serial.begin(9600); - Serial.begin() establishes serial communication between the Arduino board and another device. The most common use of serial communication between the Arduino and the computer is via a USB cable. 

int sensorValue = analogRead(A0); - It reads the input of Analog 0 and writes into a integer variable, sensorValue

Serial.println(sensorValue); - It displays the value of sensorValue on the serial monitor. When serial monitor is opened, a steady stream of numbers ranging from 0-1023 will be shown correlating to the position of the pot. As the potentiometer is turned, the number will respond and change.


This is how it looks like in action


When the knob is turned, the value on the serial monitor changes accordingly from 0 to 1023.


1b. Input Devices - Interface a LDR to maker UNO board and measure its signal in serial monitor Arduino IDE


Simulate using Tinkercad




Link to Tinkercad Simulation: 1b Tinkercad Simulation

To obtain the code, simply open Arduino, click on File -> Examples-> Basics-> AnalogReadSerial

Copy and paste in the Code under Text




Maker Uno:

As follow the connection shown in the Tinkercad, connect the LDR to the breadboard and connect a 220 ohm resistor from one end of the resistor to another column on the breadboard. Connect one wire from 5 volts to the end on the LDR without the resistor. Another wire from Analog 0 to the other end of the LDR with the resistor. Connect one more wire from GND to the row where the end of the resistor is.



Upload the code to the Maker-Uno and open the serial monitor. (Click on Tools-> Serial Monitor)


The code:

// the setup routine runs once when you press reset:

void setup() {

  // initialize serial communication at 9600 bits per second:

  Serial.begin(9600);

}

 

// the loop routine runs over and over again forever:

void loop() {

  // read the input on analog pin 0:

  int sensorValue = analogRead(A0);

  // print out the value you read:

  Serial.println(sensorValue);

  delay(1);        // delay in between reads for stability

}

 

Code: Code 1b


How the program works:

Serial.begin(9600); - Serial.begin() establishes serial communication between the Arduino board and another device. The most common use of serial communication between the Arduino and the computer is via a USB cable. 

int sensorValue = analogRead(A0); - It reads the input of Analog 0 and writes into a integer variable, sensorValue

Serial.println(sensorValue); - It displays the value of sensorValue on the serial monitor. When serial monitor is opened, a steady stream of numbers ranging from 0-1023 will be shown correlating to the position of the pot. As the potentiometer is turned, the number will respond and change.


This is how it looks like in action


The value of the signal on the serial monitor decreases as the light decreases.


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



Simulate using Tinkercad:




Link to Tinkercad Simulation: 2a Tinkercad Simulation


To create the code, first create a variable called brightness. Bring out a control block  'count up by 1 for i from 0 to 0 do' and change count up to 5, i to brightness, 0 to 0 to 0 to 255 -> 'set pin 11 to brightness' -> 'wait 30 milliseconds' -> duplicate and change up to down and 0 to 255 to 255 to 0. -> duplicate everything 2 times and change the pin to 10 and 9 respectively.





Maker-Uno:

Following the Tinkercad simulation, connect two wires, one from the 5 volts to the postive row on the breadboard and another from GND to the negative row of the breadboard. connect 3 LEDs (1 red, 1 yellow, 1 green) to the breadboard. Connect three 220 ohm resistor from the negative row of the breadboard to the cathode of each LED. Connect three wires from the anode of each LED to PIN 11,10, and 19. (Red to PIN 11, Yellow to PIN 10, Green to PIN 9)




Upload the code to Maker Uno

The code:

int brightness = 0;

 

void setup()

{

  pinMode(11, OUTPUT);

  pinMode(10, OUTPUT);

  pinMode(9, OUTPUT);

}

 

void loop()

{

  for (brightness = 0; brightness <= 255; brightness += 5) {

    analogWrite(11, brightness);

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

  }

  for (brightness = 255; brightness >= 0; brightness -= 5) {

    analogWrite(11, brightness);

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

  }

  for (brightness = 0; brightness <= 255; brightness += 5) {

    analogWrite(10, brightness);

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

  }

  for (brightness = 255; brightness >= 0; brightness -= 5) {

    analogWrite(10, brightness);

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

  }

  for (brightness = 0; brightness <= 255; brightness += 5) {

    analogWrite(9, brightness);

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

  }

  for (brightness = 255; brightness >= 0; brightness -= 5) {

    analogWrite(9, brightness);

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

  }

}

 

Code: Code 2a


How the code works:

int brightness = 0; - creating an integer variable called brightness, and of starting value,

pinMode(11, OUTPUT);

pinMode(10, OUTPUT);

pinMode(9, OUTPUT);

The code above sets PIN 11,10,9 as output

For the void loop, we have 2 different for loops, one to control the LED from 0 to 255 and the other from 255 to 0.

for (brightness = 0; brightness <= 255; brightness += 5) { - this code sets brightness = 0, and if brightness is less or equals to 255, it will increment 5 by itself.

for (brightness = 255; brightness >= 0; brightness -= 5) {– this codes sets brightness = 180, and if brightness is greater or equals to 0, it will decreases 5 by itself


This is how it looks like in action


One LED fades off and on while the remaining two LED remain lit, then the next LED fades off and on and the cycle repeats.


2b. Output Devices -  Interface the DC motor to maker UNO board and program it to on and off using push button on the board


Simulate using Tinkercad






Link to Tinkercad Simulation: 2b Tinkercad Simulation


To obtain the code, simply open Arduino, click on File -> Examples -> Digital -> DigitalInputPullup

Copy and paste in the Code under Text




Maker Uno

Following the Tinkercad simulation, connect the transistor to the breadboard. Connect one wire from 5 volts to the left of the transistor, connect another wire from PIN 13 to a 220 ohm resistor collinear to the middle of the transistor. Connect the red wire of the DC-motor to the right side of the transistor and the black wire of the DC-motor to ground.



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 {

    digitalWrite(13, HIGH);

  }

}

 

Code: Code 2b


What the code means

Serial.begin(9600); - Serial. begin() establishes serial communication between your Arduino board and another device. The most common use of serial communication you will establish is between your Arduino and your computer via a USB cable.

pinMode(2, INPUT_PULLUP); - This is where we declare that PIN 2 is an INPUT and enable the internal pull up resistor. So, by default, PIN2 status will always be HIGH

pinMode(13, OUTPUT); - Setting PIN13 as output

int sensorVal = digitalRead(2);  - It reads the input of PIN2 and writes into a INTEGER variable, sensorVal.

Serial.println(sensorVal);  - It displays the value of sensorVal if it is pressed.

IF..ELSE command – IF a condition is met, do something…ELSE do another action

IF sensorVal status is HIGH, PIN13 will be LOW, ELSE, it will be HIGH.

In this case, if the button is not pressed, the DC-motor will be turned off, and if the button is pressed, the DC-motor will turn on. 


This is how it looks like in action





What I have learnt from interfacing an input device to arduino board:

- Input devices are often called Sensors. In the maker-uno board and kit, the input devices are button on the board, potentiometer and LDR

- Input devices are used for measurement, for example measuring the amount of light using an LDR. The value is then reflected on the serial monitor

- In order to interface an input device, one end of the input device must be connected to an analog pin for example, A0.


What I have learnt from interfacing an output device to arduino board:

- Output devices are often called Actuators. In the maker-uno board and kit, the output devices are LED on the board, buzzer on the board, LED (Red, Yellow, Green),DC motor.

- Output devices are used to perform an action, for example to make the DC motor turn when the button is pressed.

- To interface an output device a resistor is usually used to connect the output device and the output PIN. A resistor has no polarity and can hence be connected in any direction. 

- To interface an LED, a resistor is necessary to prevent the LED from blowing. To interface a DC-motor, a transistor is required in order to programme the DC-motor.



Problems Encountered:

One of the problems I encountered was when i was interfacing 3 LEDs to perform fade. The problem was that when I was simulating in Tinkercad, it works but when I did it on the arduino maker uno, the LED would not light up. Only the LED on the maker uno would fade like the code. So i thought that there was something wrong with my board. Then i tried it on another board but the same thing happened. But I decided to give it another go on my board and it worked, then i realised that I had connected the wires wrongly. So by following closely the connection in my Tinkercad simulation, my program would work.


The other problem I encountered was when i was interfacing a DC-motor to turn on and off using a DC-motor. I tried many ways to interface but none of them worked. Then after looking up some examples in Youtube tutorials, I realised that I needed to add a transistor in order for it to work. So after adding a transistor to my breadboard, the program worked.


Reflection:

After learning some basic arduino programming through the pre practical activity (Hello World!, Programmable Button...), going through an individual competency test and a practical session to program a flapping unicorn, we were tasked to interface an input device (Potentiometer, LDR) and an output device (LED, DC-motor). At first, going into this activity, I was not confident, as I am not very good at programming and when I was going through the pre-practical activity, I did not really understand what some of the part mean for example the decipher code part where they explain what the code does. So when I was doing the activity, I was just blindly following the video tutorial on how to do the activity. However, as I went through part one on interfacing an input device, i started to understand more on how arduino progamming work and what the code means. Then I decided to try simulating myself using Tinkercad as the tutorials had many extra and unnecessary parts. I managed to interface a potentiometer using only 3 wires and I was really proud of myself. This gave me more confidence as I went into the remaining activities, I decided to program the arduino myself. I found that it was quite manageable after I understood how arduino programming works as I could just use the example codes in arduino the program the input and output devices. 


This tasks is important as it teaches us how to do arduino programming which would be very useful in the future, for the project in CP5070 and in our FYP project when we have to make a chemical product and then we can use arduino programming to program it. It teaches us how to interface an input and output device which would be needed in our chemical product. It also teaches us the use of simulating using Tinkercad. Instead of have to trial and error on the maker uno, one can just simulate and play around on the Tinkercad simulation making it much easier to program. This task is interesting because a very small board can be used to program many different things and has many fun activties that we can try out on our own. It also teaches us some basic programming knowledge that we can use in the future when we want to program or bring out our hidden interest for programming. In my case, I used to think that programming would be very difficult and not for me as I thought that I did not have the talent for it unlike some of my classmates. However, after going through the activities, now i think that arduino programming is actually quite fun to play with and that I am confident to be able to program using arduino maker uno as well. So next I will try to program more codes and activity by myself during the school break to further enhance and build on my programming skills which would be very useful in the future.




Comments

Popular posts from this blog

Home