Arduino Programming entry
During this weeks’ Arduino Programming entry, I have used tinkercad to simulate the programming code for the 4 tasks that have been given to me.
To interface an input device, I must first ensure that I
have connected Dupont wiring to the breadboard from the Arduino Maker Uno board
as well as the other components that are input functions. It is also important
to identify the components that are supposed to be used for input as well.
To interface an output device, I ensure that I have
connected the Dupont wiring properly. It is important to identify if the ground
cable is always connected, as well as whether the output device requires a
resistor. This is important especially when using LEDs, to ensure that they do
not burn out.
For the first output task, I was required to Interface 3
LEDs (Red, Yellow, Green) to the Maker UNO board and program it to blink in a
traffic light pattern. To do this, I have input the code below:
|
int Animation
= 0; void setup() { pinMode(LED_BUILTIN, OUTPUT); pinMode(12, OUTPUT); pinMode(11, OUTPUT); } void loop() { Animation = 200; digitalWrite(LED_BUILTIN, HIGH); delay(Animation); // Wait for variable Animation millisecond(s) digitalWrite(LED_BUILTIN, LOW); delay(Animation); // Wait for variable Animation millisecond(s) digitalWrite(12, HIGH); delay(Animation); // Wait for variable Animation millisecond(s) digitalWrite(12, LOW); delay(Animation); // Wait for variable Animation millisecond(s) digitalWrite(11, HIGH); delay(Animation); // Wait for variable Animation millisecond(s) digitalWrite(11, LOW); } |
The link for the LED program code can be found here: https://docs.google.com/document/d/1mB70yYUzYxwDGxnYYQ02_P2yink87aRe6tJhNuVn2EY/edit?usp=sharing
The comments for the code are in red, in order to further
understand the code. The void
setup() of pins 11, 12, as well as the built in pin 13, are identified
as outputs in this case.
Here is an image of the simulation on tinkercad for this
task:
In this task, I have experienced some issues connecting the
LEDs to the breadboard. As it is my first time using the breadboard, I was
unsure of whether I should place the LEDs vertically or horizontally. To solve
this, I tried different variations before understanding the breadboard
connections at the negative and positive terminals.
Here is a video of the simulation:
For the second output task, I was required to interface a DC
motor to the Maker UNO board and program it to turn on and off using a push
button on the board. I was unable to utilise the built-in button despite
multiple tries, hence I used an external button to make the program work. To do
this, I have input the code below:
|
int md1Pin =
6; // assign temporary integer for md1Pin int md2Pin =
7; // assign temporary integer for md2Pin int Button =
8; // assign temporary integer for external button int
ButtonState=0; void setup() { pinMode(md1Pin, OUTPUT); pinMode(md2Pin, OUTPUT); pinMode(Button, INPUT_PULLUP); } void loop() { ButtonState = digitalRead(Button); // to read the logic state of button at ButtonState if (ButtonState == LOW) { digitalWrite(md1Pin, HIGH); // if button is pushed, DC motor will start rotating anticlockwise digitalWrite(md2Pin, LOW); } else { digitalWrite(md1Pin, LOW); // else button is not pushed, DC motor will not rotate digitalWrite(md2Pin, LOW); } } |
The link for the DC motor program code can be found here: https://docs.google.com/document/d/1I2nx3D0cJsIZgZ4VT3rJosElz9-RteTcIw7yLR1OzGo/edit?usp=sharing
The comments for the code are in red, in order to further
understand the code. The void
setup() of pins 6, 7, 8, allow for the identification of pins 6 and 7 as
outputs for the DC motor, and the external button (pin 8) as the input in this
case.
Here is an image of the simulation on tinkercad for this
task:
In this task, I have experienced some issues connecting the H-bridge
motor driver to the breadboard. I was unsure of which inputs and outputs I
should connect to the DC motor. I was also unable to properly ground the
connection. To solve this, I made sure to connect Dupont wires to the same
input corresponding to the same output. Watching tutorials on YouTube also
helped me in connection of the H-bridge motor driver to the breadboard as well
as the DC motor. The code was also difficult to piece together, and it took me
a while before I was able to properly use the if/else code without any errors.
Here is a video of the simulation:
For the first input task, I was required to interface a LDR
to the Maker UNO board and measure its signal in serial monitor Arduino IDE. To
do this, I have input the code below:
|
int
photoresistor; // assign temporary integer for photoresistor int ldr = A0;
// assign temporary integer for ldr void setup(){ Serial.begin(9600);
// for serial data
transmission, and communicating with Serial Monitor 9600 pinMode(ldr,INPUT);
} void loop(){ photoresistor = analogRead(ldr); // reads the value from the
specified analog pin. Serial.println(photoresistor); // prints data to the serial port } |
The link for the LDR program code can be found here: https://docs.google.com/document/d/1GfXrqJf5_rxuUdHcQlom4BZ9wM3-AX5EIpKpu1dv8c8/edit?usp=sharing
The comments for the code are in red, in order to further
understand the code. The void
setup() of Serial.begin(9600) allows for serial data transmission, and communicating with Serial Monitor
9600 as well as inputs for the LDR.
Here is an image of the simulation on tinkercad for this
task:
In this task, I have experienced some issues connecting the
LDR to the breadboard. I was unsure of which inputs and outputs I should
connect to the LDR, as well as where to connect the LDR to the analog pin, A0.
I was unable to use the code at the start because I did not specify an integer
for the photoresistor. To solve this, watching tutorials on YouTube helped me
in the connection of the resistor to the breadboard as well as the LDR.
Here is a video of the simulation:
For the second input task, I was required to interface a
Potentiometer Analog Input to the Maker UNO board and measure its signal in the
serial monitor Arduino IDE. To do this, I have input the code below:
|
int
sensorValue = 0; // assign temporary integer for sensorValue void setup() { Serial.begin(9600); // for serial data
transmission, and communicating with Serial Monitor 9600 pinMode(A0, INPUT); pinMode(LED_BUILTIN, OUTPUT); } void loop() { Serial.println(sensorValue); // prints data to the serial port sensorValue = analogRead(A0); // reads the value from the
specified analog pin. digitalWrite(LED_BUILTIN, HIGH); // turn the LED on delay(sensorValue); // wait for variable sensorValue millisecond(s) digitalWrite(LED_BUILTIN, LOW); // turn the LED off delay(sensorValue); // wait for variable sensorValue millisecond(s) } |
The link for the Potentiometer program code can be found here: https://docs.google.com/document/d/1B-QZFIVGCl-Vwc7gj6eF2OSpI3Zu2DrgbniAtN06P3g/edit?usp=sharing
The comments for the code are in red, in order to further
understand the code. The void
setup() of Serial.begin(9600) allows for serial data transmission, and communicating with Serial Monitor
9600 as well as inputs for the potentiometer connected to analog pin A0,
and output of the built in LED in this case.
Here is an image of the simulation on tinkercad for this
task:
In this task, I have further connected a multimeter to test
if the potentiometer is showing variable input when it is adjusted manually. I
have experienced some issues connecting the potentiometer to the breadboard as
I was unsure of which terminal the input, output and analog pins should be
connected to. I was unable to use the code at the for the LED at the start
because I did not specify the delay to be the sensorValue. To solve this, I watched
tutorials on YouTube to assist me in the coding and connections for the
potentiometer, and I also made sure the multimeter was connected to the analog
pin A0 as well, to show that the potentiometer was indeed showing variable
input when adjusted.
Here is a video of the simulation:
Reflection:
Using the Arduino Maker Uno board simulations in tinkercad has allowed me to explore a lot of different components that I can possibly attach to the board itself. I have further understood the connections to be made to each component, and how I am required to connect the Dupont wires to the breadboard to be able to interface the components properly. I have also learnt a lot more about the different variations of code from doing my own research on how to properly connect and code the programs for them to perform the tasks. For example, I have learnt how to use the delay(sensorValue) function on the LED of the potentiometer program to show that with lower resistance, the LED blinks at a much faster rate. This allows me to make my code a lot more interactive, and it adds on a layer of complexity to the task that I am supposed to do.
I have also understood better that coding is not just about copy and pasting from the internet; but rather a lot of troubleshooting and understanding error messages. I realised that in order to do so, you have to first understand the code that you are writing - your code has to first be legible to you. I have hence made a habit of putting in comments using the '//' symbol to leave annotations on what each code refers to, and the deciphering of each part as I go along coding the program. This benefits me in ensuring that I do not confuse myself using duplicate codes, as well as preventing me from getting lost in the process of coding.
Comments
Post a Comment