Arduino Programming

 Using TINKERCAD

From this week's practical we were supposed to complete the 4 following tasks:

  • Interface a Potentiometer Analog Input to maker UNO board and measure its signal in serial monitor Arduino IDE.
  • Interface a LDR to maker UNO board and measure its signal in serial monitor Arduino IDE.
  • Interface 3 LEDs (Red, Yellow, Green) to maker UNO board and program it to perform something (fade or flash etc)
  • Interface the DC motor to maker UNO board and program it to on and off using push button on the board
You can find all the codes and videos to the following tasks here: Arduino files

Potentiometer:



In order to design the potentiometer, I used a YouTube video as reference in order to assemble the setup both in TINKERCAD and with the Arduino board.

Components:
1 Arduino Board
1 LED
1 220 Ω resistor 
1 potentiometer 
8 jumper wires

Set-up:


Observation:

Both LEDs on the Arduino board and on the Breadboard started blinking as a high rate at the beginning when the resistance was at the lowest. However, as I turned the potentiometer clockwise, I increases the resistance which caused the rate of the blinking to decrease.

Serial Monitor:

Next, I implemented the Serial monitor function in order to detect and observe the change in sensorValue which can be seen below. 0 = when potentiometer not turned, 492 is when the potentiometer is turned half way and 1023 when the potentiometer is turned all the way.



Code:
For the set-up, i programmed pin A0 as the input and pin13 (LED_BUILTIN) as the output. Serial.begin(9600) is also used to activate the serial monitor.

For the loop, analogRead reads the status of pin A0 and the value is saved under sensorValue which can be used for controlling the LED. The digitalWrite also programs the LED to turn on when the status is at HIGH and turn off when the status is at LOW. Since the potentiometer will cause blinking of the LED, the delay takes the value of the sensorValue and thus it varies base on the value read from A0.
int sensorValue = 0;

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

void loop()
{
  // read value from sensor
  sensorValue = analogRead(A0);
  // turn LED on
  digitalWrite(LED_BUILTIN, HIGH);
  delay(sensorValue); // Wait for sensorValue millisecond(s)
  // turn LED off
  digitalWrite(LED_BUILTIN, LOW);
  // pause program doe <sensorValue> milliseconds
  delay(sensorValue); // Wait for sensorValue millisecond(s)
}

LDR:


From a video that I referenced from, the LDR must be vertically aligned to the resistor for it to work.
The way LDR works is that the higher the surrounding brightness, the lower the resistance. By referring to the pictures below, by comparing the LDR when it is not covered and covered, the LED is brighter when the LDR is not covered as higher brightness = lower resistance = more voltage. 



Although it is hard to tell the difference, the serial monitor can be used to tell the difference. When the value is around 1015, the LDR is not covered but when it is covered, the value will be around 1023.

Code:
For the set-up, i programmed pin A0 as the input and pin9 as the output. Serial.begin(9600) is also used to activate the serial monitor.

For the loop, analogRead reads the status of pin A0 and the value is saved under sensorValue which can be used for controlling the LED. The sensorValue is also mapped to a range of 1023 to 255.

int sensorValue = 0;

void setup()
{
  pinMode(A0, INPUT);
  
  Serial.begin(9600);
  
  pinMode(9, OUTPUT);
}

void loop()
{
  // read value from sensor
  sensorValue = analogRead(A0);
  analogWrite(9 map(sensorValue, 0, 1023, 0, 255));
  delay(100); // Wait for sensorValue millisecond(s)
  Serial.println(sensorValue);
}


Flashing LED:



Based on the Arduino board, the pins with the squiggle mean that it is always approximate and it it used for Pulse-Width Modulation (PWM), which simulates analog output like fading an LED in and out.






Code:
For the setup, the 3 pins selected were pin9, pin 10 and pin11 is selected as the output.
pin9 is the red LED, pin10 is the green LED and pin11 is the yellow LED. 


The 2 main loops are for fading up from 0 to 255 and fading out from 255 to 0 which emulates the fading of the LED. As the brightness changes across the loop between 0-255, the value written to the LED will be different. There is also a delay of 30ms and the loops were repeated for each pin which resulted in the fading of the LEDs.





int i = 0;

int brightness = 0;

void setup()
{
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, 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(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(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)
  }
}


DC motor:


At first, I made a mistake by thinking connecting the transistor to the resistor to the ground will work but after asking around and experimenting, I realised that the resistor does not need to be grounded but the transistor does. So after connecting it correct way, I managed to make the DC motor work.
When the button is pressed, the DC motor will start to spin but in order to keep it going, the button has to be pressed constantly. 



















Code:














This code selects pin2 as input to enable the pullup resistor and pin2 will always be be HIGH while pin 13 is the output to control the DC motor. buttonState = digitalRead means that when the button is pressed, pin13 status will become HIGH and activate the DC motor.


int buttonState = 0;
int status = true;

void setup(){
  pinMode(2, INPUT_PULLUP);
  pinMode(13, OUTPUT);
}

void loop()
{
  // read the state of the pushbutton value
  buttonState = digitalRead(2);
  // check if pushbutton is pressed.  if it is, the
  // buttonState is HIGH
  if (digitalRead(2) == true) 
  {
    status = !status;
    digitalWrite(13, status);
  }
  while(digitalRead(2) == true);
  delay(50);
}


Reflection:

This Arduino Programming assignment was very useful in terms of helping me understand and learn that there are many different functions and uses of the Arduino board. It also allowed me to revisit some of the skills learnt from the previous lessons. This 4 tasks also allowed me to make use of tinkerCAD to experiment with the codes and set-up online, making it easier to figure out the things I need to change before setting it up using the actual components. For future programming tasks, I am now able to make use of the things I've learnt and also online resources to help me successfully program something. I can also use this skill for my final project development.














Comments