An Introduction to Arduino Uno R3 Microcontroller - ECE-180D-WS-2024/Wiki-Knowledge-Base GitHub Wiki
A tutorial to Arduino Uno R3 Microcontroller
Introduction
Every aspect of our lives is permeated with the presence of the Internet of Things (IoT), from small monitoring doorbells, to large environmental monitoring and agricultural automation. All devices are just machines capable of collecting and exchanging data. Based on the data they receive, they can make appropriate responses. Arduino Uno R3 is a microcontroller that represents a qualitative leap in the field of IoT, from zero to one. By learning to use Arduino Uno, you can start from a board and create a project capable of collecting environmental data and providing feedback in the physical world.
Background of Arduino
Arduino originated in 2005 at the Interaction Design Institute Ivrea (IDII) in Ivrea, Italy. The project was initiated by a group of students and teachers seeking to create affordable and easy-to-use hardware for creative projects. The main goal was to develop a platform that artists, designers, hobbyists, and anyone interested in creating interactive objects could easily use.
Arduino typically contains two parts: a programmable integrated circuit board and a software used for programming it, which is the Arduino IDE. The hardware part of Arduino is open-source, allowing anyone to view its circuit diagrams and BOM(Bill of Materials). Users can even modify or create their own board based on the public information. The software part of Arduino is also open-source, with many existing libraries and finished projects available for use. The open-source nature of the software means that users do not necessarily need to use Arduino's own microcontroller to utilize their IDE. The dual open-source nature of both hardware and software makes Arduino a system with diversity and flexibility, and it is also the reason why this community has millions of users.
Why choose Arduino Uno
In the Arduino ecosystem, there are many development boards, modules and shields, and this article mostly focuses on Arduino UNO R3, which is the most suitable for beginners, with complete functions. This includes numerous digital and analog I/O pins for connecting sensors and actuators, capable for data processing and command execution, integrated serial communication for interfacing with other devices, and compatibility with various Arduino shields to enhance functionalities in connectivity, power management, and beyond. It is the most frequently used development board in the Arduino family.
People frequently choose Uno R3 because of its simplicity, affordability, and versatility. The Arduino Uno R3 is a microcontroller board based on the ATmega328P chip and is one of the core products of the Arduino family. This board has 14 digital input/output pins, of which 3,5,6,9,10,11 can be used as PWM outputs,6 analog inputs, a 16 MHz ceramic resonator used to provide a stable clock signal to the microcontroller, a USB connection, a power jack, an ICSP header and a reset button.
Here's why people choose the Arduino Uno R3:
- Flexible power supply: Users can power the board by connecting it to a computer with a USB Type-B cable, using a 12V AC-to-DC barrel adapter, or simply using battery and connecting to the Vin and Gnd pins to power it.
- Easy connection: The Arduino Uno is easily connected to the computer system via a USB Type-B port. This can be used both to power the board and to connect as a serial device, which can be useful when debugging and reading output on a PC serial monitor.
- Microcontroller Flexibility: The ATmega328 chip can be replaced or removed in case of damage or malfunction, and it costs only a few dollars to do sp. This is a flexibility that is not offered by all Arduino boards.
- Stable power output: Pins on the board provide a stable 5V power supply, and digital and analog pins are used to regulate the voltage supply on the board.
- Community Support: Because of its simple design, the Arduino Uno has extensive user and community support. The documentation and code examples provided online allow newcomers to easily debug and diagnose thier issues; it is likely that someone else has had the issue first!
- Hardware compatibility: The Arduino Uno is capable of interacting with many hardware components, including extension bluetooth, sensors, displays and more.
- Price factor: Compared with other boards, Arduino uno R3 is cheap, and even if it is broken, it can be repaired by replacing the chip, so it has become the first development board for many new users.
While the Arduino Uno is a rather basic development board with few additional features, it is a great tool for learning how to use and program embedded microcontrollers.
How to start using Arduino Uno R3
Assume that you already have an Arduino Uno R3, if this is your first experience with the Arduino, at this moment you should go to download a Arduino IDE (here is the link https://www.arduino.cc/en/software), select the appropriate installation package based on your system (Windows, macOS, or Linux), follow the installation instructions provided on the website. Use the USB cable to connect your Arduino Uno R3 to your computer. Your computer should recognize the connected Arduino Uno R3 board and install any necessary drivers automatically.
When the installation is complete, open your Arduino IDE and go to Tools > Board and select "Arduino Uno" from the list of available boards. Then, go to Tools > Port and choose the port that corresponds to your Arduino Uno. The port will vary based on your operating system and the specific USB port you're using on your computer.
Arduino Uno "Hello World"
There are many existing test example codes in the IDE. To test if everything is working correctly, you can upload a simple example sketch, such as "Blink", this code blinks the onboard LED of the Arduino. Go to File > Examples > 01.Basics > Blink to open the Blink example code.
Now you can see a piece of prewritten code like this on the screen, click the "Upload" button (the right arrow icon on the upper left of your screen) in the Arduino IDE. This compiles the "Blink" sketch and upload it to your Arduino Uno R3. Once the upload is successful, you should see the onboard LED (marked as L) on the Arduino Uno R3 start to blink. This example is often used to check whether the board has successfully connected to your computer and to check your IDE setup.
Arduino Uno R3 Programming Basics
As you can see from the Blink example, in Arduino programming, the setup() and loop() functions are two basic and necessary parts that form the core framework of Arduino programs.
setup()
and loop()
Functions
-
setup()
Function:- Executes once upon power-on or reset.
- Initializes pin modes, starts serial communication, and sets up libraries.
- Prepares the environment for the main operation phase of the sketch.
-
loop()
Function:- Runs infinitely after
setup()
completes. - Handles continuous monitoring of inputs and control of outputs.
- Core of the sketch, making it responsive and interactive.
- Runs infinitely after
Essential Commands for Pin Operations
-
digitalRead(pin)
:- Reads the state (
HIGH
orLOW
) of a digital input pin. - Essential for detecting button presses or switch states to inform actions within the sketch.
- Reads the state (
-
digitalWrite(pin, value)
:- Controls digital outputs by setting a pin to
HIGH
(5V or 3.3V) orLOW
(0V). - Activates or deactivates connected components like LEDs or relays.
- Controls digital outputs by setting a pin to
-
analogRead(pin)
:- Reads analog signals, converting them to a digital value between 0 and 1023.
- Interfaces with analog sensors to provide nuanced environmental data.
-
analogWrite(pin, value)
:- Outputs variable voltage levels (via PWM) on digital pins.
- Allows fine control over actuators like LED brightness or motor speeds with values from 0 (off) to 255 (full on).
-
pinMode(pin, mode)
:- Defines the operation mode of a pin (
INPUT
,OUTPUT
,INPUT_PULLUP
).
- Defines the operation mode of a pin (
-
Serial.begin(baudrate)
:- Begins the serial connection, enabling messages to be sent along the USB connection to a connected PC.
In Arduino programming, setting the mode of a digital pin is crucial for its intended operation in your projects. Here are the modes you can set using the pinMode()
function:
-
INPUT
- Description: Configures the pin as a high-impedance input. This mode is used when you want to read the state (HIGH or LOW) from an external device like a switch.
- Use case: Reading the state of a button or switch.
-
OUTPUT
- Description: Sets the pin as an output, allowing it to source (provide) or sink (absorb) current. This mode is suitable for driving LEDs, motors, or other actuators.
- Use case: Turning on an LED or running a motor.
-
INPUT_PULLUP
- Description: Similar to
INPUT
mode, but it additionally enables the internal pull-up resistor. This effectively pulls the pin to HIGH when it is not connected to ground. This mode simplifies the circuit needed to read inputs like switches by ensuring the pin reads as HIGH until it's actively pulled LOW. - Use case: Simplifying button circuits by removing the need for an external pull-up resistor and ensuring a stable default state.
- Description: Similar to
// Example usage of pinMode() in an Arduino sketch
pinMode(2, INPUT); // Set digital pin 2 as input
pinMode(3, OUTPUT); // Set digital pin 3 as output
pinMode(4, INPUT_PULLUP); // Set digital pin 4 as input with pull-up resistor
These foundational functions and commands enable the creation and control of a wide array of interactive projects and devices in the Arduino ecosystem.
Write Code for external LED
With the above knowledge, you are able to write your own small projects. Let's start with write your own LED blink code. This time, we are not using the onboard LED, instead, we use an external LED which connected on the breadboard like this:
// Define the pin where the LED is connected
// For example
int ledPin = 8;
void setup() {
// Set the LED pin to output mode
pinMode(ledPin, OUTPUT);
}
// The loop function runs over and over again forever
void loop() {
digitalWrite(ledPin, HIGH); // Turn on the LED by sending a high signal to the LED pin
delay(1000); // Wait for 1000 milliseconds (1 second)
digitalWrite(ledPin, LOW); // Turn off the LED by sending a low signal to the LED pin
delay(1000); // Wait for another 1000 milliseconds
}
###Detailed Explanation of the External LED Blink Code
This Arduino sketch controls an external LED, making it blink on and off every second. Below is a step-by-step explanation of how the code operates:
Global Variable Declaration
int ledPin = 8;
declares and initializes a global variableledPin
with the value8
, indicating the external LED is connected to digital pin 8 on the Arduino board. This pin controls the LED.
setup()
Function
- Called once upon Arduino's power-on or reset, it sets up configurations not meant to be repeated.
pinMode(ledPin, OUTPUT);
configures pin 8 (connected to the LED) as anOUTPUT
, allowing it to send signals to turn the LED on or off.
loop()
Function
- This function runs continuously in an infinite loop, executing the code that blinks the LED.
digitalWrite(ledPin, HIGH);
turns the LED on by sending a high voltage signal to pin 8, lighting up the LED.delay(1000);
pauses the execution for 1000 milliseconds (1 second), keeping the LED on.digitalWrite(ledPin, LOW);
turns the LED off by sending a low voltage signal to pin 8.- Another
delay(1000);
creates a 1-second pause with the LED off, producing a blinking effect.
Conclusion
Congratulations, you have completed your first Arduino project! The LED code example highlights fundamental Arduino programming concepts such as input/output operations, device control, and timing. By controlling an external LED, this project moves from concept to physical interaction, demonstrating digital pin control and setting the stage for more complex sensor and actuator projects. There are more interesting things to do with the Arduino Uno R3. I hope you find this tutorial helpful.
References
Gupta, Pooja. “What Is Arduino Uno?: Amazing 11 Features of Arduino Uno.” EDUCBA, 4 Mar. 2021, https://educba.com/what-is-arduino-uno/. Accessed 09 Feb. 2024.
“Uno R3.” Docs.Arduino.Cc, https://docs.arduino.cc/hardware/uno-rev3/. Accessed 09 Feb. 2024.
“What Is an Arduino?” What Is an Arduino? - https://learn.sparkfun.com/tutorials/what-is-an-arduino/all. Accessed 09 Feb. 2024.
Macfos. “Arduino Pin Configuration: A Detailed Guide (2021).” Robu.in | Indian Online Store | RC Hobby | Robotics, 9 Feb. 2021, https://robu.in/arduino-pin-configuration/. Accessed 09 Feb. 2024.
“Blink.” Docs.Arduino.Cc, https://docs.arduino.cc/built-in-examples/basics/Blink/. Accessed 09 Feb. 2024.