Arduino Uno Introduction: Difference between revisions
Jump to navigation
Jump to search
| Line 82: | Line 82: | ||
= Arduino Uno - Connect a Distance Sensor to an Arduino Uno = | = Arduino Uno - Connect a Distance Sensor to an Arduino Uno = | ||
== Wiring Diagram == | |||
[[File:Screenshot 2023-04-24 at 7.36.10 am.png | 900px]] | |||
[[File:Screenshot 2023-04-24 at 7.35.46 am.png | 900px]] | |||
* Photo showing the arrangement of pins on the distance sensor. | |||
* From left to right | |||
** Ground - Negative terminal | |||
** Echo - connected to pin 13 | |||
** Trigger - connect to pin 10 | |||
** VCC - Positive terminal | |||
[[File:Screenshot 2023-04-24 at 7.43.05 am.png | 900px]] | |||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
Revision as of 21:45, 23 April 2023
Light up LED 13 on an Arduino Uno
- Open the Arduino IDE and create a new sketch.
- In the sketch, write the following code:
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
- This code sets pin 13 as an output using the `pinMode()` function in the `setup()` function.
- In the `loop()` function, it turns the LED on by setting pin 13 to `HIGH` using the `digitalWrite()` function, waits for a second using the `delay()` function, turns the LED off by setting pin 13 to `LOW`, and waits for another second.
- Verify that your Arduino Uno board is connected to your computer and select the correct board and port under the "Tools" menu.
- Upload the sketch to the Arduino Uno board by clicking on the "Upload" button.
- Once the sketch is uploaded, the LED connected to pin 13 should start blinking on and off every second.
- Video - Program lights up Arduino UNO on-board LED connected to pin 13 for 1 second.
Arduino UNO - Safety
- Wear eye protection.
- Always get a teacher to inspect your circuit before powering up.
- Wear cotton shirts and pants and closed shoes.
- Even low voltages can cause burns because wires can heat up if shorted.
Wire up an LED connected to Pin 13 on the Arduino Uno
- To light up an LED connected to pin 13 on an Arduino Uno board, you can follow these steps:
- Connect the positive (anode) leg of the LED to pin 13 on the Arduino Uno board.
- Add a 300 Ohm resistor in series
- Connect the negative (cathode) leg of the LED to the GND pin on the Arduino Uno board.
- Open the Arduino IDE and create a new sketch.
- In the sketch, write the following code:
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
- This code sets pin 13 as an output using the `pinMode()` function in the `setup()` function.
- In the `loop()` function, it turns the LED on by setting pin 13 to `HIGH` using the `digitalWrite()` function, waits for a second using the `delay()` function, turns the LED off by setting pin 13 to `LOW`, and waits for another second.
- Verify that your Arduino Uno board is connected to your computer and select the correct board and port under the "Tools" menu.
- Upload the sketch to the Arduino Uno board by clicking on the "Upload" button.
- Once the sketch is uploaded, the LED connected to pin 13 should start blinking on and off every second.
Arduino UNO - Building a LED Blink Circuit on a Breadboard
- Video - Build an external LED Blink Circuit for the Arduino UNO.
Arduino Uno - Connect a Distance Sensor to an Arduino Uno
Wiring Diagram
- Photo showing the arrangement of pins on the distance sensor.
- From left to right
- Ground - Negative terminal
- Echo - connected to pin 13
- Trigger - connect to pin 10
- VCC - Positive terminal
#define trigPin 10
#define echoPin 13
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
float duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) * 0.0344;
if (distance >= 400 || distance <= 2){
Serial.print("Distance = ");
Serial.println("Out of range");
}
else {
Serial.print("Distance = ");
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
delay(500);
}