Arduino Programming

For this week, I have been assigned tasks to complete using Tinkercad. 

    1. Input devices:
  • 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
    2. Output devices:
  • 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

Potentiometer Analog Input

Starting off with the potentiometer analog input. Using a breadboard, a resistor, a potentiometer, an LED and an Arduino board, I created the circuit shown below.



Now lets have a look at the code:

int sensorValue = 0;

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

void loop()
{
  Serial.println(sensorValue);
  sensorValue = analogRead(A0);
  digitalWrite(13, HIGH);
  delay(sensorValue); // Wait for sensorValue millisecond(s)
  digitalWrite(13, LOW);
  delay(sensorValue); // Wait for sensorValue millisecond(s)
}

How does the code work?

int sensorValue = 0;

This creates a variable sensorValue.

 pinMode(A0, INPUT);
  pinMode(13, OUTPUT);

The pinMode function is used to establish analog pin A0 as the input and digital pin 13 as the output.

Serial.begin(9600);

Serial.begin() establishes serial communication between your Arduino board and another device. The most common use of serial communication you will establish is between your Arduino and your computer via a USB cable.  

Serial.println(sensorValue);

It displays the value of sensorValue.
  
sensorValue = analogRead(A0);

analogRead is used to listen to the pin, in this case A0's, state.

delay(sensorValue);

delay is used to establish a pause for an amount of time, but instead of a fixed amount of time, the value passed to the delay function changed as the knob of the potentiometer is turned.

With Serial.begin and SerialprintIn, we are able to get a reading on the sensor value on the serial monitor. The sensor value can be seen going up from 0 to 1023 as it is turned clockwise.

LDR

Next, with the Light Dependent Resistor Using a breadboard, 2 resistors, an LDR, an LED and an Arduino board, I created the circuit shown below. 



Here is the code:

int PhotoSensor = 0;

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

  pinMode(9, OUTPUT);
}

void loop()
{
  PhotoSensor = analogRead(A0);
  Serial.println(PhotoSensor);
  analogWrite(9, map(PhotoSensor, 0, 1023, 0, 255));
  delay(1); // Wait for 1 millisecond(s)
}

This time, the sensor value rises from 26 to 923 as the light level increases.


LED Fade

Next, let's make an LED fade in and out shall we? Using 3 LEDs, a breadboard, 3 resistors and an Arduino board, the circuit is create as seen below.



Here is the code:

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);
    analogWrite(10, Brightness);
    analogWrite(11, Brightness);
    delay(30); // Wait for 30 millisecond(s)
  }
  for (Brightness = 255; Brightness >= 0; Brightness -= 5) {
    analogWrite(9, Brightness);
    analogWrite(10, Brightness);
    analogWrite(11, Brightness);
    delay(30); // Wait for 30 millisecond(s)
  }
}

How the code works

Pins 9,10 and 11 were chosen because they are some of the few pins labeled with this symbol: ~. This means that the pins are able to simulate an output between HIGH and LOW, or ON or OFF, allowing us to make the LEDs fade.

  for (Brightness = 0; Brightness <= 255; Brightness += 5) {
    analogWrite(9, Brightness);
    analogWrite(10, Brightness);
    analogWrite(11, Brightness);
    delay(30); // Wait for 30 millisecond(s)

This for() loop counts up from 0 to 255 by increments of 5. The numbers from 0 to 255 are how bright the LED would become.
The analogWrite() tells the pins to simulate the value of the variable 'brightness' which causes the LEDs to light up with a brightness of that value. But since the value of 'brightness' is constantly changing with increments of 5, the LEDs are seen as if they are fading from off to on.

  for (Brightness = 255; Brightness >= 0; Brightness -= 5) {
    analogWrite(9, Brightness);
    analogWrite(10, Brightness);
    analogWrite(11, Brightness);
    delay(30); // Wait for 30 millisecond(s)

This loop is the opposite of the previous for() loop, causing the LEDs to light up from on to off. With these 2 for() loops, the LEDs are seen as if they are fading from off to on and back to off constantly.

DC Motor with Button

Finally, let's end it off with a push button that makes a DC motor start turning! Using 2 resistors, a DC motor, a button, a breadboard and an Arduino board, let's get the circuit made as shown below!


Here is the code:

int buttonState = 0;

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

void loop()
{
  buttonState = digitalRead(2);
  if (buttonState == HIGH) {
    digitalWrite(13, HIGH);
  } else {
    digitalWrite(13, LOW);
  }
  delay(10); // Delay a little bit to improve simulation performance
}

How the code works

 pinMode(2, INPUT);
  pinMode(13, OUTPUT);

Pin 2 is set as the input and pin 13 is set as the output.

buttonState = digitalRead(2);

It reads the input of PIN2 and writes into a variable, buttonState.

if (buttonState == HIGH) {
    digitalWrite(13, HIGH);
  } else {
    digitalWrite(13, LOW);
  }

If() - This evaluates a condition using comparison operators liker greater than, less than, or in our case, equivalent to, as can be seen with the 2 equal signs '=='.

If the condition is met, the code executes, causing the DC Motor to turn. If not, the code in the else() statement is executed instead, causing the DC Motor to remain stationary.

This means that if the button is not pushed, the DC motor will not work, but if the button is pushed, the DC motor would turn.


Reflection
Using Tinkercad has been such an amazing and fun experience. I got to learn how to create a circuit digitally and simulate it working, and surprisingly, I learnt how to code much better using the coding blocks in Thinkercad. Since each code block is equivalent to a line of code, by using a code block and then referring to it in text form where I saw the line of code appear made it so much easier to understand how the codes worked and what code to use when I wanted something to happen. Additionally, I was able to work with new components that I have never worked with before, like potentiometers, and a light dependent resistor. While not real, was still as interesting as the real thing! Since Tinkercad has a bunch of other components that I have never seen before, like a temperature sensors, ultrasonic distance sensors and more, I can wait for the next time I can play with Tinkercad again!

Comments