Arduino Programming

In today's blog, I will be explaining the tasks that were done on tinkercad.

1. Input devices:

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

Guided video resource: Here

In this program, I made use of a potentiometer, a variable resistor, and the Arduino Maker UNO board. The potentiometer reads the voltage of 5V from the Arduino board and sends it signals to light up the LED which is connected to pin 13. The analog input A0 gradually senses the changing electrical signal from turning the potentiometer. The analog to digital converter of Arduino converts an incoming analog signal between 0-5V into a range of numbers from 0-1023 which can be seen from the serial monitor where 0 indicates the LED light to be OFF and 1023 indicates the LED light to be ON.

Code:

int sensorValue = 0;

The program starts with a variable sensorValue of 0

void setup()

{

  Serial.begin(9600);

 Serial.begin(9600); it is used to establish serial communication between the Arduino board and the computer via USB cable to communicate at 9600 bits per second

  pinMode(A0, INPUT);

  pinMode(13, OUTPUT);

}

As mentioned in the code we initialize and establish the digital pin(13) as the output and the analog pin(A0) as the input.

void loop()

{

  sensorValue = analogRead(A0);

This code inside the loop reads the value from the sensor called analogRead():to listen to the pin's state A0.

  digitalWrite(13, HIGH);

LED will turn on when the output is HIGH.

  delay(sensorValue); // Wait for sensorValue millisecond(s)

This means to pause the program for sensorValue milliseconds.The values passed the delay value which is the sensorValue will change as the knob is turned on the Potentiometer.

  Serial.println(sensorValue);

Serial.println(); is to see the values that the Potentiometer is registering to the serial monitor. In this case, the serial monitor will show 0 if the knob is turned to the left, and if the knob is turned to the right, it will show 1023.

 digitalWrite(13, LOW);

This means to turn LED off when the OUTPUT is LOW

 

Problems faced: At first, there was a problem in setting up the serial monitor. After consulting with some friends, I realized that I had to add the Serial.begin(9600): in the setup and Serial.printIn(); in the loop. These codes establish the communication between Arduino and the serial monitor so that the values for the signal can be recorded.

Source code: Code - Potentiometer








b. Interface an LDR to maker UNO board and measure its signal in serial monitor Arduino IDE 

Guided video resource:Here

In this program, I had to turn set up the program in a way where the LED turns on when the LDR is not receiving light(covered by fingers/hand around it) and turning off the LED when the LDR is receiving light(remove the fingers/hands around it). The resistor chosen is the 10kiloOhm resistor to be attached to the LDR and the 220 kiloOhm resistors to be attached to the LED. The purpose of the 10kiloOhm resistor with the LDR is to create a voltage divider that allows Arduino to see an accurate and linear change in voltage.

Code: 

const int ledPin = 13;   

const int ldrPin = A0;  

The variable const sets the code to be constant(read-only). So, in this case, the ledpin set as 13 and ldrpin set to A0.


void setup() {


  Serial.begin(9600);

 Serial.begin(9600); it is used to establish serial communication between the Arduino board and the computer via USB cable to communicate at 9600 bits per second.

  pinMode(ledPin, OUTPUT); 

  pinMode(ldrPin, INPUT);   

}

As mentioned in the code we initialize the LEP pin(13) as the output and the LDR pin(A0) as the input.

void loop() {


  int ldrStatus = analogRead(ldrPin);   

The above code is to read the status of the LDR value.

   if (ldrStatus <=300) {


    digitalWrite(ledPin, HIGH);              

    Serial.println("LDR is DARK, LED is ON");

    

   }

  else {


    digitalWrite(ledPin, LOW);       

    Serial.println("---------------");

  }

}

The "if and else" condition is used to command the LED to light up if the ldr status is <=300 otherwise, LED will not light up.digitalWrite(); is used as the LED pin is connected to digital pin 13. The code simply means that anything below 300 is dark and the LED pin will light up and vice versa. 

Source code: Code - LDR





2. Output devices: 

a. Interface 3 LEDs (Red, Yellow, Green) to maker UNO board and program it to perform something (fade or flash etc)

Guided video resource:Here

In this program, I had to adjust the LEDs brightness using Arduino's output.The PWM(~) pin was used to fade the LED brighter or dimmer. In this setup, The anodes of 3 LEDs were connected to the PWM pin 9, PWM pin 10, and PWM pin 11 respectively. Whereas the cathodes of the LEDs were connected to the resistor to control the voltage flow to LED so that it doesn't explode.

Code:

int brightness = 0;

The program sets the variable(brightness) to 0.

void setup()

{

  pinMode(9, OUTPUT);

  pinMode(10, OUTPUT);

  pinMode(11, OUTPUT);

}

These pins 9,10 and 11 are initialized as an output that sends the signal to the LEDs

void loop()

{

  for (brightness = 0; brightness <= 255; brightness += 5) {


    analogWrite(9, brightness);

    delay(30); // Wait for 30 millisecond(s)

    analogWrite(10, brightness);

    delay(30); // Wait for 30 millisecond(s)

    analogWrite(11, brightness);

    delay(30); // Wait for 30 millisecond(s)

  }

The void loop(){ uses 2 for(){ loops.To fade in, the brightness increases in increments of 5 from 0 to 255.0 indicating all the way off and 255 indicating all the way on. This is the condition for the first loop. Inside this counting loop, the output is set to pin 9,10,11 each to variable brightness.AnalogWrite(); function is added with ti.There is also a delay(30) function which indicates it has a waiting time of 30 milliseconds.

  for (brightness = 255; brightness >= 0; brightness -= 5) {

    analogWrite(9, brightness);

    delay(30); // Wait for 30 millisecond(s)

    analogWrite(10, brightness);

    delay(30); // Wait for 30 millisecond(s)

    analogWrite(11, brightness);

    delay(30); // Wait for 30 millisecond(s)

  }

}

To fade out, another counting loop is used with the same functions as fading in but this time around it will be counting down by increments of 5 for the variable brightness from 255 to 0.

Source Code: Code - Fade 3 LEDs





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

Guided video resource: Here

In this program, a DC motor is controlled using a push button where the motor will turn on when the button is pressed and turn off when the button is pressed again. The push-button will be added in tinkercad and as for the Arduino board, the push button will be set as pinMode(2, INPUT_PULLUP) as the maker UNO board has an inbuilt button set in pin 2.

In tinkercad, the push button is connected to a 10kΩ resistor and the transistor is connected to a 200Ω resistor. In the maker UNO board, however, only a 220Ω resistor will be connected to the transistor. A transistor is added in this activity as it acts as an electrical switch for the DC motor. The transistor uses the small current (5V )from the Arduino digital output to control the bigger current of the motor to switch the motor on and off. A transistor consists of an Emitter which will be connected to one of the wires of the DC motor, a Base to be connected to the 10kΩ resistor to the output pin 13, and a Collector which is connected to the 5V pin. The other end of the DC motor will be connected to GND.

Code: 

int button = 0;

The program starts with a variable called button and sets it to 0

int antState = 0;

The variable for the anterior state of button is set to 0

int buttonOff = 0;

This variable buttonOff = 0 means DC motor will be ON and 1 means Dc motor will be off


void setup() {

  pinMode(2,INPUT);// BUTTON as INPUT

  pinMode(13,OUTPUT);// DC MOTOR as OUTPUT

}

Pin 13 is initialized as output which sends signal to the DC motor and the built-in button from maker UNO (pin 2) with INPUT_PULLUP is the input.

void loop() {

  int button = digitalRead(2);// reading the state of the button

  

  if((button == LOW) && (antState == HIGH)){

    buttonOff = 1 - buttonOff;

  }

  The program's loop in void loop() { uses a control that uses 2 if(); statements. The state of the button on digital pin 2 will be read. For the first, if statement, the condition is if the variable button is LOW and the antState is HIGH, the DC motor will be ON as shown in the code buttonOff = 1 - buttonOff; 

  antState = button;

  This reads the actual value of the button

  if(buttonOff == 1){

    digitalWrite(13,HIGH); 

  }

  else {

    digitalWrite(13, LOW);

  }

}

The next if statement has a condition if buttonOff == 1 , which means if the DC Motor is OFF when the button is pressed, the output signal on pin 13 will be HIGH which indicates that the motor will be ON. From the video below, the LED light on pin 13 of the maker UNO board lights up when the button is pressed. The DC motor will turn on. Otherwise indicated by else { , if the condition is false when the DC Motor is ON, the output signal on pin 13 will be LOW which indicates that the motor will be OFF. This is shown in the video when the button is pressed again, the LED light on pin 13 disappears and the DC motor stops moving.

Problems faced: The first problem I faced was when I realized there isn't a push button in the tinkercad. I assumed that the red button on the tinkercad simulation was the inbuilt button. So, I used a guided video from Youtube to guide me in setting up the push button into the tinkercad. Next, there was a hiccup when I thought my motor was finally working. The hiccup was, when I pressed the button, the motor starts running but then when I press the button again, it would not stop. This was solved by inputting the if conditions. After a few trials, there was a problem in getting the motor started when the button was pressed. I thought the motor had spoilt due to multiple trials. However, when I changed the resistor from 10kiloOhm to 200kiloOhm resistor, the motor started working.

Source Code: Code - DC Motor









Reflection:
The context on Arduino programming is definitely a new knowledge to me.This was the first ever time i am learning about coding and it is really challenging for me as coding was like an alien-like language to me. There were times i was really frustrated and stressed when attempting the tasks given.However, with alot of help from external guided videos and discussion with friends from different classes who are taking this module,I was able to understand better the reasoning and the coding languages used.The lessons and task on this was really time consuming especially when i did not achieve the right outcome for the tasks or when the program used does not work.So, it was a series of trial and error as i did not quite understand and figure out where i went wrong.

From interfacing an input device, i learned that there is a need for serial communication between the Arduino board and the Arduino IDE in the laptop. Potentiometers and photoresistors are both varying resisters and they can be used to drop voltage that is present on an analog input pin.It is used for conversion of analog signals into digital signals.Whereas, for interfacing an output device, it is used to convert digital signals to analog signals.

I also learned that due during the setting up of the devices, we must make sure to check on the type of devices that we are using too.For an example, I had difficulty in turning on te motor due to the use of a lresistir with a lower resistance.But when i changed it to a higher resistance, the motor started to work.So, this taught me a lesson to be more aware of the equipment and devices voltage value and functionality.Overall, it was a challenging experience for me and I am glad that had to come out of my comfort zone to learn something new. Eventhough i am not entirely good in programming, I am glad that i have the basic knowledge on it and want to expand this knowledge in the future.






Comments

Popular Posts