Arduino Uno Introduction
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);
}
Arduino Uno - Read Voltage of Solar PV Panel using Voltage Divider Circuit
- In this circuit the voltage of a small 5V 0.5 Watt solar panel is read using Analog Pin A2 on the Arduino Uno.
- The Arduino analog pins can read voltages from 0 to 5.0 volts.
- The Arduino then translates the voltage reading to a number (bin) between 0 and 1023.
- zero volts - 0 value
- 5.0 volts - 1023 value
- The Arduino analog pins can be damaged if the voltage exceeds 5.0 volts.
- As a safety measure we can reduce the voltage from the solar PV panel by connecting it to a Voltage Divider Circuit.
Voltage Divider Circuit
- A voltage divider circuit is simply two 10,000 Ohm (10K Ohms) resistors connected in series and connected to the output of the solar PV panel.
- The connection point (mid-point) between the two resistors will be exactly half the voltage of the solar PV panel.
- This mid-point is connected to Analog pin A2.
- Note that high precision resistors need to be used to make the Voltage Divider.
- These resistors have less than a 0.5% error in their stated value.
Arduino Uno - Photo of Solar PV Panel and Voltage Divider circuit
Arduino Uno - Code to read Analog Pin A2
- This code (Arduino Sketch) reads the voltage of the solar PV Panel.
- The code will output raw analog readings from Pin A2.
- Analog values will range from 0 to 1023.
- Note that it does not correct for the Voltage Divider Circuit.
/*
Reading an analog Input from solar PV panel
Read the analog input on analog pin A2
Pin A2 in connected to a Voltage Divider circuit (2 x 10,000 ohm resistors)
The Voltage divider circuit is connected to a 5V PV panel (0.5W)
Edmond Lascaris
10 April 2023
*/
int solarPin = A2; // select the input pin for the potentiometer
int solarValue = 0; // variable to store the value coming from the sensor
void setup() {
// Initiate serial communication
Serial.begin(9600);
}
void loop() {
// read the value from the sensor:
// analog pin returns reads 0-5V by returning a value between 0 and 1023
solarValue = analogRead(solarPin);
Serial.println(solarValue);
delay(1000);
}
Arduino Uno - Relay Circuit
- A relay is similar to an electrical switch, however this switch can be turned on and off by a computer or micro-controller.
- A relay is normally used to control larger electrical devices such as pumps and motors that need to be powered on a separate circuit.
- A separate circuit may be powered using a battery, power supply or even a solar PV panel.
- In this example we are using a 5V relay Pololu Basic SPDT Relay Carrier with 5VDC Relay
- Later we will also learn that special Power Transistors can also be to turn high power devices on and off Controlling Circuits with a Power Transitior
Relay Circuit
- input pins (left of photo) on the relay are:
- Ground - negative terminal
- VDD - positive terminal
- E - input pin. Setting to HIGH (5V) turns relay on. Setting to LOW (0 volts) turns relay off. The input pin is controlled using Pin 12 on the Arduino Uno.
- Output Pins are used to control an external circuit.
- COM - Common.
- NC - Normally Closed. When the relay is not powered or the E pin is set to LOW, the NC pin is connected to the COM pin.
- NO - Normally Open. When the relay is powered or the E pin is set to HIGH, the NO pin is connected to the COM pin. This is normally when an external circuit is connected.
- Input pin E on the relay is controlled by Pin 12 on the Arduino Uno.