Arduino Programming
In this entry, I attempted a couple of individual Arduino tasks, using Tinkercad simulation software to program the tasks without the use of a physical maker Uno kit.
It is an input device as a potentiometer is a component that controls voltage, which in this case is used to control the voltage to power the LED. I will be following the steps of a video tutorial to learn how to interface the input device to the board.
b. Interface a LDR to maker UNO board and measure its
signal in serial monitor Arduino IDE
I used a video tutorial to help guide me on how to setup the components. I learnt that similar to 1a, the LDR is also an input device as it controls the amount of resistance in the maker UNO. Changing the LDR affects the brightness of the LED light;
- After assigning the push button to pin 2 and adding a resistor to it, I added the DC Motor and attached it to pin 13, along with another resistor.
1. Input devices:
a. Interface a Potentiometer Analog Input to maker UNO
board and measure its signal in serial monitor Arduino IDE
- LED flashes at a rate of between 0 to 1023 milliseconds
- input : analog pin A0
- output : digital pin 13
// C++ code
//
int SensorValue = 0;
void setup()
{
pinMode(A0, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
// read the value of the sensor
SensorValue = analogRead(A0);
// turn LED on
digitalWrite(LED_BUILTIN, HIGH);
// pause program for sensor value ms
delay(SensorValue); // Wait for SensorValue millisecond(s)
// turn led off
digitalWrite(LED_BUILTIN, LOW);
delay(SensorValue); // Wait for SensorValue millisecond(s)
}
Video of potentiometer interface
Link to the simulation
Next, I will be using a Light Dependent Resistor(LDR) and interfacing it to the maker UNO board.
when LDR is dim = LED will not light up
when LDR is bright = LED will light up
Code
// C++ code
//
int sensorvalue = 0;
void setup()
{
pinMode(A0,INPUT);
Serial.begin(9600);
pinMode(9, OUTPUT);
}
void loop()
{
sensorvalue = analogRead(A0);
Serial.println(sensorvalue);
analogWrite(9, map(sensorvalue, 0, 1023, 0, 255));
delay(150);
}
Video of LDR interface
Link to the LDR simulation
2. Output devices:
a. Interface 3 LEDs (Red, Yellow, Green) to maker UNO
board and program it to perform something (fade or flash
etc)
In this step I'll be switching from an input device to an output device, specifically interfacing 3 LEDS to the maker UNO board.
- Following the steps of the video, I managed to interface one LED and programmed it to fade.
// C++ code
//
int brightness = 0;
void setup()
{
pinMode(9, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
}
void loop()
{
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)
}
- What caused the LED to fade was the command to increase brightness from 0 to 255, in small increments of (+5 & -5), in delays of 30ms
- However, in order to complete the task I was required to add in 2 more LED lights. Hence i duplicated the original code and chose pin 6 and 5 respectively for my additional LEDs.
Code
// C++ code
//
int brightness = 0;
void setup()
{
pinMode(9, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
}
void loop()
{
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)
}
for (brightness = 0; brightness <= 255; brightness += 5) {
analogWrite(6, brightness);
delay(30); // Wait for 30 millisecond(s)
}
for (brightness = 255; brightness >= 0; brightness -= 5) {
analogWrite(6, brightness);
delay(30); // Wait for 30 millisecond(s)
}
for (brightness = 0; brightness <= 255; brightness += 5) {
analogWrite(5, brightness);
delay(30); // Wait for 30 millisecond(s)
}
for (brightness = 255; brightness >= 0; brightness -= 5) {
analogWrite(5, brightness);
delay(30); // Wait for 30 millisecond(s)
}
}
Video of LED interface
b. Interface the DC motor to maker UNO board and
program it to on and off using push button on the board
This task was harder than expected, as there were not many straightforward tutorials on how to integrate the DC motor together with the push button, however I managed to assemble it successfully with the help of a few videos and a bit of experimenting. Below are the videos I used to assist me with the process:
DC Motor push button setup
Code
// C++ code
//
int buttonstate = 0;
void setup()
{
pinMode(2, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
// read state of button
buttonstate = digitalRead(2);
if (buttonstate == HIGH) {
digitalWrite(LED_BUILTIN,HIGH);
}else{
digitalWrite(LED_BUILTIN,LOW);
}
delay(10); // Delay a little bit to improve simulation performance
}
- After assigning the push button to pin 2 and adding a resistor to it, I added the DC Motor and attached it to pin 13, along with another resistor.
- Programming the state of the button to high, the push button (input at pin 2), when pushed, causes the output DC motor at pin 13 to rotate.
- Additionally, setting the code of button state = digital read, means that if the button state is low(not pushed), the DC motor will not rotate.
- Adding a 10ms delay causes the DC motor to wait for 10ms before starting to rotate.
Video of push button and DC motor
Link to the simulation
Problems faced
Generally, I feel that I did not encounter any significant problems with Tinkercad. However, I would say that the switch from the physical UNO maker board to an online software was a bit rocky at the start, as there were many more components that are provided and it took me a while to get used to the placements of wires, LEDs etc. Another issue I faced was that a few times I wanted to switch from " Block " code to " Text " code but I accidently erased all my codes while doing so. A few times I missed out on a few semi colons or i failed to add spaces in between commas and that led to a few failed simulation attempts. However all of these problems were quickly solved by just repeating through all my steps again and looking out for any errors.
Reflection
Overall, although I encountered a few obstacles, albeit minor, I felt that my process of using Tinkercad was smooth and allowed me to carry out commands with ease. Initially, I expected Tinkercad to be challenging as I was still new to Arduino programming and an online version of the Arduino board felt like it would be more confusing, but it turned out much better than expected and I felt that I managed to learn and visualize better with the Arduino boards and components displayed in a simple but clear way. I managed to learn how to use input devices like a potentiometer or output devices like DC motors and code simple functions like LED fade. The different ways of showing code, like block and text, were helpful in my understanding of how the code worked to perform actions, particularly the block code, which enabled me to look at the different lines of code more clearly than regular text code. Personally, the 3 LED task was my favorite, as I learnt how to perform code for a singular component, then duplicate it to also apply to other components, and when put together could form something more complex like a traffic light. I feel like these tasks really helped to develop and further my understanding of Arduino and I also had fun while doing so.
Comments
Post a Comment