Chapter 3(Arduino) PDF
Document Details
Uploaded by HalcyonAndradite
null
Tags
Summary
This document provides an introduction to embedded systems and the Arduino platform. It covers the basic components and concepts of embedded systems, including advantages, disadvantages, and structural elements. Further, it details the Arduino platform, its software and programming. This will be helpful to learn about embedded systems and Arduino programming.
Full Transcript
# Introduction to Embedded System ## What is an Embedded System? An Embedded System is a computer system with a dedicated function within a larger mechanical or electrical system, often with real-time computing constraints. It is embedded as part of a complete device often including hardware and m...
# Introduction to Embedded System ## What is an Embedded System? An Embedded System is a computer system with a dedicated function within a larger mechanical or electrical system, often with real-time computing constraints. It is embedded as part of a complete device often including hardware and mechanical parts. Embedded systems control many devices in common use today. ### Advantages - Easily Customizable - Low power consumption - Low cost ### Disadvantages - High development effort - Larger time to market ## The following illustration shows the basic structure of an Embedded System | Sensor | A-D Converter | Processor & ASIC | D-A Converter | Actuator | |---|---|---|---|---| | | | | | | | | | Memory | | | ## Description of the Basic Structure of an Embedded system - **Sensor**: It measures the physical quantity and converts it to an electrical signal which can be read by an observer or by any electronic instrument like an A-D converter. - **A-D Converter**: An analog-to-digital converter converts the analog signal sent by the sensor into a digital signal. - **D-A Converter**: A digital-to-analog converter converts the digital data fed by the processor to analog data - **Actuator**: An actuator compares the output given by the D-A Converter to the actual (expected) output stored in it and stores the approved output. ## Example: A digital thermostat as an example of an embedded system. The system comprises of: - Keypad - Processor - Memory - Display - ADC - Thermostat circuit - Heater / AC control # Arduino ## Introduction - **Arduino** is a prototype platform (open-source) based on an easy-to-use hardware and software. It consists of a circuit board, which can be programmed (referred to as a microcontroller) and a ready-made software called Arduino IDE (Integrated Development Environment), which is used to write and upload the computer code to the physical board. - **Arduino** boards are able to read analog or digital input signals from different sensors and turn it into an output such as activating a motor, turning LED on/off, connect to the cloud and many other actions. ## **Arduino Software (IDE)** - The open-source Arduino Software (IDE) makes it easy to write code and upload it to the board. It runs on Windows, Mac OSX, and Linux. You can control your board functions by sending a set of instructions to the microcontroller on the board via Arduino IDE (referred to as uploading software). The Arduino IDE uses a simplified version of C++, making it easier to learn to program. ## **Data Types in Arduino** The Arduino programming language supports a wide range of data types, each designed to store different kinds of information. Here's a table summarizing the commonly used data types along with their bit allocation and value ranges: | Data Type | Value Assigned | Value Ranges | |---|---|---| | boolean | 8 Bit | True or False | | byte | 8 Bit | 0 to 255 | | char | 8 Bit | -127 to 128 | | unsigned char | 8 Bit | 0 to 255 | | word | 16 Bit | 0 to 65535 | | unsigned int | 16 Bit | 0 to 65535 | | int | 16 Bit | -32768 to 32767 | | long | 32 Bit | -2,147,483,648 to 2,147,483,647 | | float | 32 Bit | -3.4028235E38 to 3.4028235E38 | ## **Program Structure in Arduino** A typical Arduino program consists of two main functions: - **setup()**: This function is executed only once when the Arduino board is powered on or reset. It's typically used for setting up initial conditions, like configuring pins, initializing variables, or establishing communication with other devices. - **loop()**: This function runs continuously after the setup() function is complete. It contains the main logic of your program, handling sensor readings, controlling outputs, making calculations, and performing other operations. ## **Arduino - Board** - Various kinds of Arduino boards are available depending on different microcontrollers used. However, all Arduino boards have one thing in common: they are programmed through the Arduino IDE. - The differences are based on the number of inputs and outputs (the number of sensors, LEDs, and buttons you can use on a single board), speed, operating voltage, form factor, etc. Some boards are designed to be embedded and have no programming interface (hardware), which you would need to buy separately. Some can run directly from a 3.7V battery, others need at least 5V. ## **Arduino - Board** The Arduino board has a variety of pins that allow you to interact with the world around it. Here's a breakdown of the pin layout: - **Digital I/O Pins:** These pins can be configured as either inputs or outputs. They support PWM (Pulse Width Modulation) for controlling analog-like behavior. - **Analog Input Pins:** These pins are specifically designed for reading analog signals, like those from sensors. - **Power Pins:** These pins provide different voltage levels for powering the Arduino board and external components. - **Communication Pins:** These pins are used for communicating with other devices, such as computers, sensors, or other Arduino boards. - **Special Pins:** These pins have specific functions, like the LED on pin 13, which can be turned on or off for visual feedback. ## **Voltage Levels in Arduino** Most electronics systems operate on a specific voltage level. Arduino typically uses 5V TTL (Transistor-Transistor Logic) levels. This means that signals are represented as either high (5V) or low (0V). - **VOH:** This is the minimum output voltage level that a TTL device will provide for a high signal. - **VIH:** This is the minimum input voltage level that a TTL device will consider as a high signal. - **VOL:** This is the maximum output voltage level that a TTL device will provide for a low signal. - **VIL:** This is the maximum input voltage level that a TTL device will consider as a low signal. ## **Arduino - I/O** - The Arduino provides both digital and analog I/O (input/output) pins, enabling you to read data from sensors and control external devices. - **Digital Mode in Arduino**: You can set the mode of any Pin (either an input or output) by using the following command: `pinMode(pin, mode)`. - **pin:** Represents the number of the Pin whose mode you want to set. - **mode**: You can specify either `INPUT` or `OUTPUT`. - **Arduino Digital Input**: You can read the status of any Pin by using the command: `Val=digitalRead(pin)`. - **Val:** A variable to save the return value of the function. - **pin:** The Pin number whose status you want to read. - **Note:** Before reading the status, ensure that the pin is already configured as an `INPUT` port. - **Arduino Digital Output:** You can set the value (sending logic value) of any Pin by using the command: `digitalWrite(pin, value)`. - **pin:** The Pin number. - **value:** Set it to either `HIGH` or `LOW`. ## **First Example** - **Turn the LED (ON) for one second and then turn it (OFF) for two seconds connected to digital pin 2**. - **Circuit Diagram**: The circuit diagram shows a connection of an LED and resistor to the Arduino board. - The LED is connected to the digital pin 2. - The resistor is connected to the Positive leg of the LED while the negative leg is connected to the Ground. - The positive leg of the resistor is connected to the 5V of the board. ## **Operating an LED** - To turn on the LED, create a path for the current to flow through it. Current flows from more positive to less positive voltages. - Here's how to connect an LED to an Arduino: - **+V**: The LED's longer leg is connected to the positive 5V power supply. - **Resistor**: A resistor (220 ohms) is connected in series to limit the current flowing through the LED, preventing it from burning out. The positive leg of the resistor goes to the 5V (positive) power supply, and the negative leg is connected to the positive leg of the LED. - **GND**: The LED's shorter leg is connected to ground (GND). ## **Blinking an LED** - **The circuit Assumptions:** - Wire the LED with the resistor. - Connect one leg of the LED to the digital pin 2 of the Arduino. - Connect the other leg of the LED to the ground pin of the Arduino. - **Code:** ```c++ byte x = 2; void setup(){ pinMode(x, OUTPUT); // Sets digital pin 2 as output } void loop(){ digitalWrite(x, HIGH); // Turns the LED on delay(1500); // Waits for 1.5 seconds digitalWrite(x, LOW); // Turns the LED off delay(2000); // Waits for 2 seconds } ``` - **Code Explanation:** The code above sets the digital pin 2 (connected to the LED) as an output and then toggles the LED on and off with a delay in between. ## **Arduino - I/O** - **Arduino Analog Input:** - The Arduino Uno board contains a 6 channel, 10-bit analog to digital converter. This means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023. This yields a resolution between readings of: 5 volts / 1024 units or, .0049 volts (4.9mV) per unit. - **Syntax:** `Var=analogRead(pin)`. - **Var:** A variable to save the return value of the function (int). - **pin:** Represents the number of the analog input pin you want to read from (0 to 5). - **Note:** This function returns a number between 0 and 1023, which represents voltages between 0 and 5 volts. ## **Arduino - I/O** - **Producing PWM in Arduino:** - Writing an analog value to a pin can be used to generate a PWM (Pulse Width Modulation) signal. - The frequency of the PWM signal on most pins is approximately 490 Hz. On the Uno and similar boards, pins 5 and 6 have a frequency of approximately 980 Hz. - **Syntax:** `analogWrite(Pin, Value)` - **Pin:** The pin you want to write to. - **Value:** The duty cycle: between 0 (always off) and 255 (always on). ## **Arduino - I/O** - **Mapping Variables:** - Mapsa number from one range to another. - **Syntax:** `X= map(value, fromLow, fromHigh, toLow, toHigh)` - **X:** Represents the new remapped value. - **value:** The number or variable you want to map. - **fromLow:** The lower bound of the value's current range. - **fromHigh:** The upper bound of the value's current range. - **toLow:** The lower bound of the value's target range. - **toHigh:** The upper bound of the value's target range. # **Arduino Mega** ## **Arduino Mega Pinout** This pinout provides a visual representation of the Arduino Mega's pin layout: - **Digital I/O Pins:** These pins can be configured as inputs or outputs, and many can be used for PWM (Pulse Width Modulation). - **Analog Input Pins:** These pins are designed for reading analog signals, like those from sensors. - **Power Pins:** These provide various voltage levels for powering the board and external components. - **Communication Pins:** These are used for communicating with other devices (computers, sensors, or other Arduino boards). - **Special Pins:** Some have specific roles, like the LED on pin 13, which can be used to provide visual status feedback. ## **Summary of Arduino Mega Features** The Arduino Mega packs a powerful set of features: - **Microcontroller:** ATmega2560 - **Operating Voltage:** 5V - **Input Voltage (recommended):** 7-12V - **Digital I/O Pins:** 54 (of which 15 provide PWM output) - **Analog Input Pins:** 16 - **Flash Memory:** 256 KB (of which 8 KB used by boot-loader) - **SRAM:** 8 KB - **EEPROM:** 4 KB - **Clock Speed:** 16 MHz ## **Power** The Arduino Mega can receive power through multiple ways: - **USB Connection:** Can be powered directly from a computer's USB port. - **External Power Supply:** Can be powered using an external power supply. - **Power Source Selection:** The Arduino Mega can automatically detect the power source and switch between USB and external power as needed. ## **Power Pins** The Arduino Mega's power pins: - **VIN:** This pin accepts input voltage, generally from an external power source. - **5V:** This pin outputs a regulated 5V, which is appropriate for powering devices. - **3V3:** Provides a regulated 3.3V output, suitable for some components. - **GND:** Represents the ground pin, used as a common reference point for electrical connections. # **Arduino Mega 2560** - The Arduino Mega 2560 is a popular microcontroller development board with a powerful ATmega2560 processor onboard. It comes with a wealth of digital and analog I/O pins, memory, communication capabilities, and other features that make it versatile for various projects. ## **Memory** The ATmega2560 features three types of memory: - **Flash Memory:** Used for storing the program code you upload. It has sufficient space to accommodate large programs. - **SRAM:** This memory is for storing temporary data that the program is currently using. It's often used to store variables. - **EEPROM:** Persistent memory that allows you to store data that’s retained even after the Arduino is powered off. ## **Digital I/O Pins** The Arduino Mega 2560 boasts 54 digital input/output pins: - **General Purpose:** Can be individually configured as either inputs or outputs, enabling you to interact with various components. - **PWM Support:** Many of these pins support PWM (Pulse Width Modulation) for controlling analog-like behavior with digital outputs. - **Specialized Functions:** Some digital I/O pins have dedicated functions, like: - **Serial Communication:** Certain pins are used for communication with other devices using serial protocols. - **External Interrupts:** These pins trigger interrupt routines when their value changes, allowing for event-driven programming. - **SPI Communication:** Designated pins support SPI (Serial Peripheral Interface) protocol for high-speed communication with peripherals. - **TWI/I2C Communication:** Reserved pins enable communication using the TWI (Two Wire Interface) protocol. ## **Analog Input Pins** The ATmega2560 provides 16 analog input pins. - **Analog-to-Digital Conversion:** They allow you to measure analog signals (like those from sensors) and convert them to digital values that the Arduino can process. - **Resolution:** Each analog input has a resolution of 10 bits, enabling readings with 1024 different values. ## **Communication** The Arduino Mega 2560 offers a variety of communication features: - **UARTs (Universal Asynchronous Receiver/Transmitter):** The ATmega2560 provides four hardware UARTs for TTL (5V) serial communication. These can be used for: - **Serial Communication:** Sending and receiving data with other devices, including computers. - **Accessing Peripherals:** Connecting to peripheral devices that utilize serial communication. - **USB Communication:** The Arduino Mega 2560 uses an ATmega16U2 microcontroller, which acts as a USB-to-serial converter. This enables: - **Uploading Code:** Transferring programs from a computer to the Arduino board using the Arduino IDE. - **Serial Communication:** Communicating with the computer through a virtual serial port. - **TWI (Two Wire Interface)**: Also known as I2C. The Arduino Mega 2560 supports this protocol for communication with external devices: - **Wire Library:** The Arduino software includes a Wire library to simplify TWI communication. - **SPI (Serial Peripheral Interface):** The Arduino Mega 2560 supports this protocol for high-speed communication with peripherals. - **SPI Library:** The Arduino software includes a SPI library for simplifies SPI communication. ## **Programming** The Arduino Mega 2560 can be easily programmed with the Arduino software: - **Arduino IDE:** The Arduino IDE is a user-friendly environment for writing and uploading sketches (programs) to the board. - **Bootloader:** The ATmega2560 comes pre-programmed with a bootloader. - **Bootloader Advantages:** This bootloader allows you to upload new code to the Arduino without requiring an external hardware programmer. It essentially acts as a tiny program that simplifies the process of programming the microcontroller. ## **Automatic Reset** The Arduino Mega 2560 has an automatic reset feature that occurs during code upload: - **Reset Trigger:** When you upload a new sketch to the Arduino Mega 2560, the Arduino IDE sends a signal to the board that causes a reset. - **Bootloader Execution:** The reset triggers the bootloader, which is then ready to receive the new code from the IDE. - **Program Execution:** After the code is uploaded, the bootloader jumps to the execution of your program.