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
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.
void setup() { pinMode(A0, INPUT); pinMode(13, OUTPUT); }
void loop() { // read the value from the sensor sensorValue = analogRead(A0);
// 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) }
LDR
TinkerCAD
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:
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:
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.
How The Code Works
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); }
No comments:
Post a Comment