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:
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 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 } |
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
1b. Input Devices - Interface a LDR to maker UNO board and measure its signal in serial monitor Arduino IDE
Simulate using Tinkercad
To obtain the code, simply open Arduino, click on File -> Examples-> Basics-> AnalogReadSerial
Copy and paste in the Code under Text
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 } |
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
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) } } |
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
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.
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
Post a Comment