Wednesday, 24 November 2021

Arduino Programming

Arduino Programming

Today, we are tasked to complete activities on interfacing input and output devices.

These are the activities that we need to complete:

1. Input devices:

        a. Interface a Potentiometer Analog Input to maker UNO

            board and measure its signal in serial monitor Arduino IDE

        b. Interface an 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


Potentiometer Analog Input

TinkerCAD

As I rotate the knob on the potentiometer clockwise, the frequency of blinks on the LED bulb decreases. This is because the resistance of the potentiometer will increase as the knob is rotated. clockwise. A resister is needed when interfacing with an LED to prevent the LED from exploding. By adding "Serial.begin(9600);" under "voidsetup()" and "Serial.println(sensorValue);" under "voidloop()", we can monitor the sensor value in the serial monitor. In the serial monitor, the sensor value will change whenever the knob is rotated.


The serial monitor shows a increase in the sensor value as the knob on the potentiometer is turned clockwise, with the lowest point being a senservalue of 0 and the highest point having a sensorValue of 1023.

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> millseconds
  delay(sensorValue); // Wait for sensorValue millisecond(s)
  // turn the LED off
  digitalWrite(LED_BUILTIN, LOW);
  // pause the program for <sensorValue> millseconds
  delay(sensorValue); // Wait for sensorValue millisecond(s)
}



How The Code Works
void setup()
{
  pinMode(A0, INPUT);
  pinMode(13, OUTPUT);
}
Pin A0 is configured as an input and Pin 13 is configured as an output to control the LED.

void loop()
{
  // read the value from the sensor
  sensorValue = analogRead(A0);
Within the main loop, a function called "analogRead();" checks the state of pin A0  and stores that value in the variable "sensorValue".

 // turn the LED on
  digitalWrite(13, HIGH);
  // pause the program for <sensorValue> millseconds
  delay(sensorValue); // Wait for sensorValue millisecond(s)
  // turn the LED off
  digitalWrite(13, LOW);
  // pause the program for <sensorValue> millseconds
  delay(sensorValue); // Wait for sensorValue millisecond(s)
}
This string of code controls the flashing of the LED. The "digitalwrite" cause the LED to be "HIGH" or "LOW" which turns the LED on or off respectively. As the potentiometer is rotated and the sensor value changes, which will result in a corresponding change in the duration of each flash of the LED.


LDR

TinkerCAD

LED is dim when LDR is covered (low light intensity)

LED is bright when LDR is exposed to high light intensity

When the LDR detects a high light intensity, the LDR's resistance will decrease causing the LED to be brighter. When the LDR detects a low light intensity, the LDR's resistance will increase causing the LED to be dimmer. The LDR is interfaced with the LED in series.


Code:

// C++ code
//
int sensorVal = 0;

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

void loop()
{
  digitalWrite(13, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(13, LOW);
  delay(1000); // Wait for 1000 millisecond(s)
}

How The Code Works

int sensorVal = 0;

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

"int sensorVal = 0;" creates a variable for the sensor. Under void setup, we declare PIN 13 as the output.

void loop()
{
  digitalWrite(13, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(13, LOW);
  delay(1000); // Wait for 1000 millisecond(s)
}

When PIN 13 is HIGH, LED will turn on, When PIN 13 is LOW, LED will turn off. There is a 1000ms delay between each action

Flashing LED

TinkerCAD




The 3 LEDs are consecutively light up for 1 second in a sequence with a 1 second pause in between. This is similar to what we did in the competency test during our Arduino practical. I used PINs 12, 10 and 8 as the output. The red LED is connected to PIN 12, green LED is connected to PIN 10 and the yellow LED is connected to PIN 8.


Code:

// C++ code
//
int i = 0;

void setup()
{
  pinMode(12, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(8, OUTPUT);
}

void loop()
{
  digitalWrite(12, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(12, LOW);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(10, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(10, LOW);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(8, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(8, LOW);
  delay(1000); // Wait for 1000 millisecond(s)
}

How The Code Works

int i = 0;

void setup()
{
  pinMode(12, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(8, OUTPUT);
}

Under void setup, we declare PINs 12, 10, 8, as the output.

void loop()
{
  digitalWrite(12, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(12, LOW);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(10, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(10, LOW);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(8, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(8, LOW);
  delay(1000); // Wait for 1000 millisecond(s)
}

This string of code causes the LED to flash in sequence consecutively. The "digitalwrite" cause the LED to be "HIGH" or "LOW" which turns the LED on or off respectively. The PIN 12 output is set to HIGH which causes an LED to light up. The LED lights up for 1 second as the delay is 1000ms. The PIN 12 output is then set to LOW for 1 second which causes the LED on pin 12 to turn off for 1 second. The same thing repeats for the LED connected to PIN 10 and 8 in sequence. Since the code is in a voidloop, it will repeat infinitely.


DC Motor

TinkerCAD




When the button is pushed, the DC motor will turn on. The DC motor is turned off when the button is pushed once more. Initially, when interfacing the DC motor, I had trouble getting it to work. After some inspection and research, I realised that I have interfaced the transistor in the opposite direction. I was previously not aware that the transistor had different pins, the emitter (e), base(b) and collector(c) pins. After interfacing the transistor in the correct direction, the DC motor finally turned on. I also had to interface on the Arduino Uno board slightly different as I would on TinkerCAD as on TinkerCAD we had to add in a button whereas the Arduino Uno board already has a build-in button.
 
Code:
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);
}

How The Code Works

int buttonState = 0;
int status = true;

void setup(){
  pinMode(2, INPUT_PULLUP);
  pinMode(13, OUTPUT);
}
Within the void setup, PIN 2 is declared as the input and enable the internal pull up resistor. So, by default, PIN2 status will always be HIGH. PIN 13 is declared as the 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);
}
Within the void loop, "buttonState = digitalRead(2);" check if pushbutton is pressed.  if it is, the buttonState is HIGH and the output PIN 13 will be HIGH, causing the DC motor to turn on. When the button is pressed again, the buttonState will be LOW and the DC motor will turn off.




Reflection
For the activities, I had to interface some input and output devices on TinkerCAD and use the breadboard and Arduino UNO board. Since this was my first time using TinkerCAD, I was initially unfamiliar with using it. But with the help of the videos provided, I quickly got accustomed to using it. I also had to program Arduino code to be uploaded to the Arduino board to complete each task. I used certain codes which were publicly available online and modified them to suit my objective. Interfacing the input and output devices were quite challenging for me and required many trial and error for it to work. I believe that what I learnt from doing these activities is important as my group plan to use Arduino programming and interfacing in our chemical product. Learning how to interface input and output devices on an Arduino board as most of the Arduino project will likely include both input and output devices.

Initially, I used to think that Arduino interfacing was very complicated and difficult to learn. However, how that I learnt the function and how to interface each component, I feel more confident in interfacing devices on the Arduino.  I hope to be able to create far more complicated projects with the use of Arduino in the future, either for the CP5070 module or for personal interest.

No comments:

Post a Comment