Lecture 2: Arduino & Arduino

School of Science and Technology Lecture 2: Arduino & Arduino Programming ITEC10281 Systems Technology- Embedded System Part

Dr. Gadelhag Mohmed (Module Leader for System Technology)

Lecture Overview

  • Overview of the lecture's topics:

    • What Arduino is.

    • Difference between microcontroller and microprocessor.

    • Write the simplest Arduino program.

    • Understand breadboards and their uses.

Microcontroller Definition

  • A microcontroller is:

    • A small computer on a single chip.

    • Contains a processor, memory, and input/output ports.

    • Typically embedded inside devices that they control.

    • Often small and low-cost.

    • Reference: www.mikroe.com/chapters/view/1

Development Board Definition

  • A development board is:

    • A printed circuit board designed to facilitate work with specific microcontrollers.

    • Typical components include:

    • Power circuit.

    • Programming interface.

    • Basic input, usually including buttons and LEDs.

    • I/O pins.

Arduino Uno Hardware

  • Arduino platform includes several models:

    • Arduino Uno:

    • The basic model with a replaceable chip.

    • Arduino Mega 2560:

    • Provides numerous inputs and outputs.

    • Arduino LilyPad:

    • Designed for wearables, washable, to be integrated into clothing/fabric projects.

    • Arduino Nano:

    • A very compact version.

Shields

  • Shields are additional boards that can be plugged on top of the Arduino PCB to extend its capabilities.

    • Examples of Shields:

    • Joystick Shield.

    • GPS Shield.

    • Wi-Fi Shield.

Arduino Microcontroller Specifications

  • Microcontroller: ATMEL Atmega328.

  • Specifications include:

    • Clock Speed: 16 MHz.

    • SRAM: 2 Kbytes.

    • Flash Memory: 32 Kbytes (2 Kbytes reserved for boot loader).

    • EEPROM: 1 Kbyte.

    • User Accessible LEDs: 3.

    • I/O Pins: 20 (6 are PWM).

  • Boot Loader:

    • Preloaded program (~2Kbytes) stored in non-volatile memory.

    • Executes automatically when the power is applied.

    • On manual reset, it jumps to the application location in flash memory.

  • Development Platform:

    • Open source.

    • Integrated Development Environment (IDE):

    • A Java program for writing and compiling source code into ATMEL Atmega.

Arduino Integrated Development Environment (IDE)

  • Main Functions:

    • void setup(): Initializes the board with the setup code, runs once.

    • void loop(): Main code that runs repeatedly until the board is turned off.

Adding an External LED to pin 13

  • Procedure:

    • File > Examples > Basics > Blink.

  • LED Facts:

    • LEDs have polarity:

    • Negative is indicated by a flat side of the housing and a shorter leg.

Terminology

  • Key Terms:

    • wiring: Library of functions (e.g., pinMode()).

    • sketch: A program created to run on an Arduino board.

    • pin: An input or output connection (e.g., output to an LED, input from a knob).

    • digital: Values are either HIGH (1) or LOW (0).

    • analog: Values range, typically from 0 to 255 (e.g., LED brightness, motor speed).

Digital Input/Output and Timing in Arduino

  • Functions:

    • pinMode(pin, mode): Sets pin to INPUT or OUTPUT.

    • digitalRead(pin): Reads the state (HIGH/LOW) from a pin.

    • digitalWrite(pin, value): Writes HIGH or LOW to a pin.

    • delay(ms): Pauses execution for milliseconds.

    • delayMicroseconds(us): Pauses execution for microseconds.

  • More commands are available at arduino.cc/en/Reference/HomePage.

Input and Output Functions

  • Common Functions:

    • digitalRead(): Reads the state of a digital pin.

    • digitalWrite(): Sets a digital pin to HIGH or LOW.

    • analogRead(): Reads voltage on an analog pin returning values from 0-1023.

    • analogWrite(): Changes PWM rate on specified pins (0-255).

    • pulseIn(pin, value): Measures pulse duration on digital inputs.

Variables

  • Definition: Variables store values of specific data types.

  • Types and Examples:

    • int number = 1: Stores integer.

    • int sum = 500500: Stores integer.

    • double radius = 5.5: Stores floating-point number.

    • double area = 95.0334: Stores floating-point number.

    • String greeting = "Hello": Stores text.

    • String statusMsg = "Game Over": Stores text.

Special Symbols

  • Key Symbols in Code:

    • ; (semicolon): Ends instructions (statements).

    • {} (curly braces): Marks blocks of code.

    • // or //: Marks comments in the code, ignored by the processor.

    • Example comment: /* Blink. Turns on an LED for one second, then off for one second, repeatedly. */

Understanding Breadboards

  • breadboards are used to prototype circuits without soldering.

Electrical Circuits

  • Arduino focuses on electronics:

    • Electric charge: Amount of electrical energy that can be positive or negative.

    • Electric current: Flow of electrical charge, described as the flow of electrons, with conventional current flowing in the opposite direction.

Simple Circuit Analogy

  • To understand circuits, we can use waterflow as an analogy:

    • Pipe: Represents a wire where water flows (current).

    • Pump: Source of pressure (voltage).

    • Pressure difference: The force causing the flow.

Voltage Reference Points

  • Voltages within a circuit must be referenced to another point, commonly referred to as ground (0V).

Basic Electronic Components

  • Components:

    • Resistors: Opposes current flow, measured in Ohms (Ω).

    • Capacitors: Store energy in an electric field, measured in Farads (F).

    • Inductors: Store energy in a magnetic field, measured in Henry (H).

Ohm’s Law

  • Expresses the relationship between voltage, current, and resistance:

    • V=I⋅RV = I \cdot R

    • Where:

    • II = Current

    • VV = Voltage

    • RR = Resistance

  • Alternate forms:

    • I=VRI = \frac{V}{R}

    • R=VIR = \frac{V}{I}

Resistors in Series and Parallel

  • In Series:

    • R=R<em>1+R</em>2+R3R = R<em>1 + R</em>2 + R_3

  • In Parallel:

    • 1R=1R<em>1+1R</em>2+1R3\frac{1}{R} = \frac{1}{R<em>1} + \frac{1}{R</em>2} + \frac{1}{R_3}

Resistive Potential Dividers

  • General formula for voltage division is given by:

    • V=V<em>1(R</em>1+R2)V = \frac{V<em>1}{(R</em>1 + R_2)}

    • Example with values:

    • V=6V(200Ω+300Ω)(10Ω+10Ω)V = \frac{6V(200 \Omega + 300 \Omega)}{(10 \Omega + 10 \Omega)}

Working with Variables

  • Variable Storage: Arduino allows variable creation in two steps:

    • Declare the variable for use (e.g., datatype variablename;).

    • Assign values (e.g., variablename = value;).

  • Naming Conventions: Variable names must only include letters, numbers, underscores, or dollar signs; must start with a letter; case sensitive; unlimited length.

Defining Variable Values

  • Use the assignment operator = for assigning values:

    • Example: redPin = 8; assigns a value to a location in memory.

  • Type Memory Size: The data type defines how much memory is reserved:

    • boolean: 1 byte

    • char: 1 byte (-128 to +127)

    • byte: 1 byte (0 to 255)

Arduino Data Types

  • Once a data type is defined, only that specific type can be assigned:

    • Types include:

    • int: 2 bytes (-32,768 to 32,767)

    • word: 2 bytes (0 to 65,535)

    • long: 4 bytes (-2,147,483,648 to 2,147,483,647)

    • float: 4 bytes; range varies.

    • double: 4 bytes; range varies.

Variable Qualifiers

  • const: Indicates that a variable's value will not change.

    • Example: const int greenLED = 5;

  • unsigned: Specifies to ignore a sign bit, storing only positive values.

    • Examples: unsigned char, unsigned int.

Declaring Variables

  • The Arduino programming language is built on C. Essentially involves:

    • Declaration: datatype variablename;

    • Assignment: variablename = value;

Exploring Arduino Functions

  • Prebuilt functions allow for simple usage of operations.

    • Functions perform actions by sending values, then return results.

  • Serial Output:

    • Serial.begin(speed): Initializes serial communication.

    • Serial.print(): Outputs to serial port.

    • Serial.println(): Outputs and includes a newline character.

  • Random Numbers:

    • random([min], max): Returns a random number between the specified range.

Conditional Statements

  • Syntax example:

    • if (someCondition) { // do stuff if the condition is true },

    • else { // do stuff if the condition is false }

Reading from Serial Port

  • Example code to read a character from the serial port:

    • If there are bytes available:

  if(Serial.available() > 0) {  
    char letter = Serial.read();  
    if(letter == '1') {  
      Serial.println("You entered ONE");  
    }  
    else if (letter == '0') {  
      Serial.println("You entered ZERO");  
    }  
  }

For Loop

  • Structure:

    • for (int i = 0; i < 10; i++) { // Loop body }

While Loop

  • Structure:

    • while(expression) { statement(s); }

    • Example:

  int var = 0;  
  while (var < 200) {  
    // Action repeated 200 times  
    var = var + 1;  
  }

Arithmetic and Formulas

  • Basic Operations:

    • +, -, *, /, %

  • Examples:

    • a = 2 + 2;

    • light = ((12 * sensorValue) - 5) / 2;

    • remainder = 3 % 2;

Comparison Operators

  • Used for conditional testing in code:

    • ==: equals

    • !=: not equal to

    • <: less than

    • >: greater than

    • <=: less than or equal to

    • >=: greater than or equal to

  • Boolean Operators:

    • &&: logical AND

    • ||: logical OR

Input/Output Functions

  • Important functions for handling pin modes:

    • pinMode(pin, mode): Sets the mode of a pin.

    • Example: pinMode(7,INPUT);

    • digitalWrite(pin, value): Controls pin output state.

    • Example: digitalWrite(8, HIGH);

    • digitalRead(pin): Reads state from a pin.

    • analogRead(pin): Reads voltage from analog pin.

    • analogWrite(pin, value): Changes PWM output for pins designated as PWM.

    • unsigned long pulse(pin, value): Measures duration of an input pulse.

Math Functions

  • Common mathematical functions available in Arduino Programming:

    • min(x, y): Returns smaller of x or y.

    • max(x,y): Returns larger of x or y.

    • abs(x): Returns the absolute value of x.

    • map(value, fLow, fHigh, tLow, tHigh): Maps a value from one range to another.

    • double sqrt(x): Calculates square root of x.

    • double pow(base, exponent): Calculates the power of base raised to exponent.

    • long random(min, max): Returns a long integer within a specified range.

Serial Communication Functions

  • Essential functions for handling serial data:

    • Serial.begin(speed): Start communication at specified speed.

    • Serial.print(data, [encoding]): Sends data to the serial port, allowing various encoding types.

    • int serial.available(): Returns the number of unread bytes in the serial buffer.

    • Serial.flush(): Clears the serial buffer.

Further Reading

  • Recommended resources for additional study:

    • Beginning Arduino Programming by Brian Evans (2011, electronic resource)

    • Chapters 1 & 2

    • C Programming for Arduino by Julien Bayle (2013, free online version)

    • URL: http://arduino.cc/en/Tutorial/Foundations

Lab 2 Objectives

  • By end of this lab, you should be able to:

    • Use a breadboard to construct a complete circuit.

    • Describe the basic characteristics of resistors.

    • Write a basic Arduino program.

Additional Notes on Components

  • Components Used in Circuits:

    • Anode and Cathode

    • Resistors

    • Battery