Skip to main content

Arduino Programming

For our lesson on Arduino Programming, I was introduced to TinkerCad, a web-based simulation website that allows users to develop and test their own Arduino codes before uploading them to the Arduino Maker-Uno Board. It allows us to test our codes on the Arduino board virtually and safely online.


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

On TinkerCad, I created a new circuit and used a video as a guide to build my maker UNO board. The link of the video is shown below. Below is also the code and set-up of the potentiometer analog input.

Video: https://www.youtube.com/watch?v=-EDYMQ9lczA


The code below is a modified blink code, and the potentiometer is programmed as the input and the LED as the output .
Code that I used for this activity:
// C++ code
//
int sensorValue = 0;

void setup()
{
  pinMode(A0, INPUT);
  pinMode(13, OUTPUT);
}

void loop()
{
  sensorValue = analogRead(A0);
  digitalWrite(13, HIGH);
  delay(1000); // Wait for sensorValue millisecond(s)
  digitalWrite(13, LOW);
  delay(1000); // Wait for sensorValue millisecond(s)
}
Stimulation of the activity:



Activity 1b: Interface a LDR to maker UNO board and measure its signal in serial monitor Arduino IDE

Below is the code and the set-up of the board for this activity.


The code used is a modified digital input code, with the LDR set as the input and the LED set as the output.
Code that I used for this activity:
// C++ code
//

int LDRValue = 0;  //LDRValue is an integer and starts from 0
void setup()
{
  pinMode(A0, INPUT);           //analog pin A0 is set as input
  pinMode(13, OUTPUT); //Builtin LED aka. pin13 is set as output
  Serial.begin(9600);           //Serial connection is established at 9600bits/s
}

void loop()
{
  LDRValue = analogRead(A0);  //Reads LDR value from A0
  Serial.println(LDRValue);   //Display LDRValue
  digitalWrite(13, HIGH); //turns on LED
}
Stimulation of the activity:


Activity 2a: Interface 3 LEDs (Red, Yellow, Green) to maker UNO board and program it to perform something
I have programmed the 3 LEDs on my board to fade one after the other. Below is the code and set-up of this activity.
The code that I used as a modified fade code, with the 3 LEDs set as the output.
Code that I used for this activity:
int animationSpeed = 0; //animationSpeed is set as an integer which
//is equal to 0
int brightness = 0;  //brightness is set as an  integer which is 
//equal to 0
void setup()
{
  pinMode(11, OUTPUT); //pin11 set as output
  pinMode(10, OUTPUT);  //pin10 set as output
  pinMode(9, OUTPUT);   //pin9 set as output
}

void loop()
{
  animationSpeed = 100; //animationSpeed is set to 100ms
  for (brightness = 0; brightness <= 255; brightness += 17) {
    //brightness increases from 0 to 255 in the multiples of 17
    analogWrite(11, brightness);
    //tells pin11 the current brightness
  delay(animationSpeed); // Wait for animationSpeed millisecond(s)
  }
  for (brightness = 255; brightness >= 0; brightness -= 17) {
    //brightness decreases from 255 to 0 in the multiples of 17
    analogWrite(11, brightness);
    //tells pin11 the current brightness
  delay(animationSpeed); // Wait for animationSpeed millisecond(s)
  }
  for (brightness = 0; brightness <= 255; brightness += 17) {
    analogWrite(10, brightness);
  delay(animationSpeed); 
  }
  for (brightness = 255; brightness >= 0; brightness -= 17) {
    analogWrite(10, brightness);
  delay(animationSpeed); 
  }
  for (brightness = 0; brightness <= 255; brightness += 17) {
    analogWrite(9, brightness);
  delay(animationSpeed); 
  }
  for (brightness = 255; brightness >= 0; brightness -= 17) {
    analogWrite(9, brightness);
  delay(animationSpeed); 
  }
}
Stimulation of the activity:

Link to the TinkerCad activity: https://www.tinkercad.com/things/8wxBsUTYsX1

Activity 2b: Interface the DC motor to maker UNO board and program it to on and off using push button on the board

Below is the code and set-up of the board:


The code I used is a modified digital input code, with the push-in button as the input and the DC motor as the output.
Code used for this activity:
// C++ code
//
int ButtonState = 0; // setting ButtonState as an integer equal to 0

void setup()
{
  pinMode(3, INPUT); //set pin3 as input
  pinMode(8, OUTPUT); //set pin8 as output
}

void loop()
{
  ButtonState = digitalRead(3); //reads input at pin3
  if (ButtonState == HIGH) { //if input ButtonState is high
    digitalWrite(8, HIGH); //output is high so motor starts moving
  } else {
    digitalWrite(8, LOW); //else, output is low
  }
  delay(10); // Delay for 10ms to improve simulation performance
}
Stimulation for this activity:

Link to the TinkerCad activity: https://www.tinkercad.com/things/lccwxDOVU0p

Discussion
I learnt that the steps in connecting an input and output to the Arduino board is very mechanical. Once I did the connection multiple times in the multiple TinkerCad activities with the use of a video guide, I am able to easily do it on my own. I have also learnt that colour coding the wires in the TinkerCad stimulation is more important than I thought as it immediately tells me which wire is connect to where, which makes it less messy and confusing to understand the Arduino board set-up.

The main problem I faced is trying to understand the set-up of the board when I first started these activities. For example, in the first TinkerCad activity, I did not understand the functions of the wires and how they are connected in the stimulation. What I did to fix the problem is to refer to a video guide, and I tinkered with the simulation by doing trial-and-error with the LEDs, wiring and the multimeter to get a feel of how they work.

Reflection
Using TinkerCad has helped me appreciate this programme a lot more as I am able to perform many Arduino Programming functions online without the need and hassle of using a physical Arduino maker UNO kit. For Arduino Programming in general, at first it was very confusing as I did not understand the inputs used in the code at all. Even till now, I can still be stumped by the coding used in the activities. For example, during the competency test for our Arduino programming practical, I forgot to change the pinmode output from LED_BUILTIN , to pin3, pin5 and pin7. However, my coding still worked as intended, which I was very surprised about. I later found out that the reason the code still worked is because when an input pin high is written, the internal pullup resistor is turned on an internal pullup resistor which will source some current to the LEDs. 

Even though I find Arduino Programming very challenging and daunting, I still managed to grasp the basic coding principles and the main code inputs used, such as a for loop code. I have learnt a lot not just about Arduino, but programming in general as well. I actually hope that I can learn more about Arduino Programming in the future as it is a useful tool to have when creating chemical products.








Comments