WHAT IS ARDUINO?
Arduino is an open-source electronics platform based on easy-to-use hardware and software. Arduino boards are able to read inputs - light on a sensor or even pressing a button!
We used C++(a type of coding language) to code our Arduino!
Tinkercad Challenge Time!
Tinkercad is a online version of an arduino where we can use block (commands) to help code our programme!
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
Input devices:
a. Interface a Potentiometer Analog Input to maker UNO
board and measure its signal in serial monitor Arduino IDE
1. Create a circuit connecting the LED,
resistor and the Arduino board.
2. Input the Potentiometer above the green
wire
3. Click on “code”, create new variable
“sensorValue”, then drag out the set block as shown in the picture below.
5. Serial monitor code.
6. Serial monitor reading.
7. The final code using C++
// C++ code
//
int sensorValue = 0;
void setup()
{
pinMode(A0, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
}
void loop()
{
//
read the value from the sensor
sensorValue = analogRead(A0);
//
turn the LED on
digitalWrite(LED_BUILTIN, HIGH);
//
pause the program for <sensorValue>
delay(sensorValue); // Wait for sensorValue millisecond(s)
//
turn the LED off
digitalWrite(LED_BUILTIN, LOW);
//
pause the program for <sensorValue>
delay(sensorValue); // Wait for sensorValue millisecond(s)
//
read the input on analog pin 0:
sensorValue = sensorValue;
//
print out the value you read
Serial.print("sensor");
Serial.println(sensorValue);
}
Here's is how it looks like:
b. Interface a LDR to maker UNO board and measure its
signal in serial monitor Arduino IDE
1. Change the resistor to Photoresistor.
2. Add in the serial monitor.
3. The final code:
// C++ code
//
int sensorValue = 0;
void setup()
{
pinMode(A0, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
//
read the value from the sensor
sensorValue = analogRead(A0);
//
turn the LED on
digitalWrite(LED_BUILTIN, HIGH);
//
pause the program for <sensorValue>
delay(sensorValue); // Wait for sensorValue millisecond(s)
//
turn the LED off
digitalWrite(LED_BUILTIN, LOW);
//
pause the program for <sensorValue>
delay(sensorValue); // Wait for sensorValue millisecond(s)
}
Here's how it looks like
Output devices:
Interface 3 LEDs (Red, Yellow, Green) to maker UNO
board and program it to perform something (fade or flash
etc)
1. Start by making this set up
2. Click on code: select block under control.
3. Put in output: set 9 to brightness
4. To fade out, need create another counting loop
5. Add 2 more LED to the board
// C++ code//
int sensorValue = 0;
int brightness = 0;
void setup()
{
pinMode(9, 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)
}
}
Here's how it looks
b. Interface the DC motor to maker UNO board and
program it to on and off using push button on the board
1. Create the following set up
2. Insert Resistor and set to 10k ohms
3. Create new variable called ButtonState
4. Go to input and set the button to read pin that we are using in this case is pin 2
5. Set up the following ensure that the correct pin is chosen
// C++ code
//
int sensorValue = 0;
int brightness = 0;
int ButtonState = 0;
void setup()
{
pinMode(2, INPUT);
pinMode(9, OUTPUT);
}
void loop()
{
//
read the state of the pushbutton
ButtonState = digitalRead(2);
//
check if pushbutton is pressed, if it is, HIGH
if
(ButtonState == HIGH) {
digitalWrite(9, HIGH);
}
else {
digitalWrite(9, LOW);
}
delay(10); // Delay a little bit to improve simulation performance
}
Here's how it looks!
What went wrong?
When I was coding the DC motor with the button I could not get the desired result which is pressing the button to start the DC motor. However, thanks to what I've learnt before using the button for LED, I used the same technique to set if pressed and what if it is not pressed, that is high and low respectively! Instead of selected the LED output block, I used the pin number output block instead to direct my instructions to the desired pin number!
With that when I press the button, my DC motor starts!
Reflection Time!
Well after doing arduino, I still certainly dislike it. To be honest, this is my second time doing arduino. My first experience using arduino was in secondary school. I realised that through my journey of using arduino, I've learned to better appreciate coding although coding isn't my cup of tea. I believe that coding is rather straight forward but you must know what affects each variable and the technique/ code to give the desired result.
Certainly, after this experience using arduino, I learned that if I'm stuck I can always refer to online videos for help! Not being afraid to seek help is remarkable thing to do. We learn from others and we can build on what we have learned. Being stuck in coding is a nightmare, even the smallest detail of missing a comma or a close bracket can ruin your entire code!
I feel that I have much more to learn and certainly enjoyed the process and the result always made me really relief that my code works!
Thank you for reading!
Comments
Post a Comment