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);
}
}
Source Code: Code - DC Motor
Reflection:
Comments
Post a Comment