FUNCTIONS For controlling the Arduino board and performing computations.

Similar documents
More Arduino Programming

Arduino Part 2. Introductory Medical Device Prototyping

FUNCTIONS USED IN CODING pinmode()

Arduino Uno Microcontroller Overview

Arduino Prof. Dr. Magdy M. Abdelhameed

Arduino. (Digital-to-Analog Converter D/A D2A) (Digital to Analog Conversion) 3. (Analog to Digital conversion)

Chapter 2 The Basic Functions

Introduction to Microcontrollers Using Arduino. PhilRobotics

Introduction to Arduino

Introduction to Arduino. Wilson Wingston Sharon

arduino programming notebook brian w. evans revised by Paul Badger

GOOD MORNING SUNSHINE

Serial.begin ( ); Serial.println( ); analogread ( ); map ( );

BASIC ARDUINO WORKSHOP. Mr. Aldwin and Mr. Bernardo

Lab 01 Arduino 程式設計實驗. Essential Arduino Programming and Digital Signal Process

analogwrite(); The analogwrite function writes an analog value (PWM wave) to a PWM-enabled pin.

Robotics/Electronics Review for the Final Exam

IME-100 Interdisciplinary Design and Manufacturing

Arduino 101 AN INTRODUCTION TO ARDUINO BY WOMEN IN ENGINEERING FT T I NA A ND AW E S O ME ME NTO R S

IME-100 ECE. Lab 4. Electrical and Computer Engineering Department Kettering University. G. Tewolde, IME100-ECE,

USER MANUAL ARDUINO I/O EXPANSION SHIELD

Arduino Uno R3 INTRODUCTION

TANGIBLE MEDIA & PHYSICAL COMPUTING INTRODUCTION TO ARDUINO

Lab-3: LCDs Serial Communication Analog Inputs Temperature Measurement System

How to Use an Arduino

Topics: Microcontrollers Programming Basics: structure and variables Digital Output Analog to Digital Conversion. Arduino

The Arduino Briefing. The Arduino Briefing

Introduction to Arduino

Arduino and Matlab for prototyping and manufacturing

Note. The above image and many others are courtesy of - this is a wonderful resource for designing circuits.

Arduino Programming. Arduino UNO & Innoesys Educational Shield

Arduino Programming Part 4: Flow Control

keyestudio Keyestudio MEGA 2560 R3 Board

Adapted from a lab originally written by Simon Hastings and Bill Ashmanskas

Smart Objects. SAPIENZA Università di Roma, M.Sc. in Product Design Fabio Patrizi

ARDUINO MINI 05 Code: A000087

ARDUINO. By Kiran Tiwari BCT 2072 CoTS.

TABLE OF CONTENTS INTRODUCTION LESSONS PROJECTS

Microcontrollers and Interfacing week 8 exercises

ARDUINO M0 PRO Code: A000111

University of Portland EE 271 Electrical Circuits Laboratory. Experiment: Arduino

What s inside the kit

Laboratory 1 Introduction to the Arduino boards

ArdOS The Arduino Operating System Quick Start Guide and Examples

Arduino - DigitalReadSerial

Update: Ver 1.3 Dec Arduino Learning Guide For Beginner Using. Created by Cytron Technologies Sdn Bhd - All Rights Reserved

IME-100 ECE. Lab 3. Electrical and Computer Engineering Department Kettering University. G. Tewolde, IME100-ECE,

EP486 Microcontroller Applications

Update: Ver 1.3 Dec Arduino Learning Guide For Beginner Using. Created by Cytron Technologies Sdn Bhd - All Rights Reserved

Arduino For Amateur Radio

EXPERIMENT 7 Please visit to learn all features of arduino before you start the experiments

ARDUINO FOR COMPLETE IDIOTS. by David Smith

ARDUINO UNO REV3 Code: A000066

Arduino Uno. Arduino Uno R3 Front. Arduino Uno R2 Front

SX1509 I/O Expander Breakout Hookup Guide

The Arduino IDE and coding in C (part 1)

Introduction To Arduino

ARDUINO LEONARDO WITH HEADERS Code: A000057

cs281: Introduction to Computer Systems Lab03 K-Map Simplification for an LED-based Circuit Decimal Input LED Result LED3 LED2 LED1 LED3 LED2 1, 2

Arduino ADK Rev.3 Board A000069

Arduino Programming and Interfacing

Intro to Arduino. Zero to Prototyping in a Flash! Material designed by Linz Craig and Brian Huang

Specification. 1.Power Supply direct from Microcontroller Board. 2.The circuit can be used with Microcontroller Board such as Arduino UNO R3.

MassDuino User's manual

Lecture 6: Embedded Systems and Microcontrollers

Arduino Programming Part 3. EAS 199A Fall 2010

Earthshine Design Arduino Starters Kit Manual - A Complete Beginners Guide to the Arduino. Project 13. Serial Temperature Sensor

PROGRAMMING ARDUINO COURSE ON ADVANCED INTERACTION TECHNIQUES. Luís Carriço FCUL 2012/13

Arduino Lesson 15. DC Motor Reversing

Arduino Course. Technology Will Save Us - Tim Brooke 10th August Friday, 9 August 13

ARDUINO MEGA 2560 REV3 Code: A000067

Objectives: Learn how to input and output analogue values Be able to see what the Arduino is thinking by sending numbers to the screen

Project 2: Sensor Light

SPDM Level 2 Smart Electronics Unit, Level 2

Physical Computing Self-Quiz

Welcome to Apollo. For more information, please visit the website and select Apollo. Default Code

ARDUINO LEONARDO ETH Code: A000022

MEDIS Module 2. Microcontroller based systems for controlling industrial processes. Chapter 4: Timer and interrupts. M. Seyfarth, Version 0.

Distributed Real- Time Control Systems. Lecture 3 Embedded Systems Interfacing the OuterWorld

COMPUTING IN THE PHYSICAL WORLD

Digital I/O Operations

ARDUINO UNO REV3 SMD Code: A The board everybody gets started with, based on the ATmega328 (SMD).

Sanguino TSB. Introduction: Features:

University of Hull Department of Computer Science C4DI Interfacing with Arduinos

Interrupts Arduino, AVR, and deep dark programming secrets. What is an Interrupt?

Arduino Workshop. Overview. What is an Arduino? Why Arduino? Setting up your Arduino Environment. Get an Arduino based board and usb cable

ARDUINO YÚN MINI Code: A000108

<Table of content> <Parts and description> <Circuit board Guide> <Pins> <Basics of Coding> <Intro to Arduino> <Intro> <Downloading Arduino IDO>

PROGRAMMING WITH ARDUINO

TANGIBLE MEDIA & PHYSICAL COMPUTING MORE ARDUINO

Computer Architectures

LAB PROCEDURE. Lab Objectives 1. Generate a project using the GR-Sakura web compiler 2. Edit/Compile/Build/Debug the project using the web compiler

Alessandra de Vitis. Arduino

ARDUINO MEGA ADK REV3 Code: A000069

E11 Lecture 4: More C!!! Profs. David Money Harris & Sarah Harris Fall 2011

Løkkestrukturer. Trykknap: Button, Se eksempler / Digital / Button. Decision: If: Version 01/ Loops, løkker mm. i Arduino.

Arduino provides a standard form factor that breaks the functions of the micro-controller into a more accessible package.

CTEC 1802 Embedded Programming Labs

Robotics and Electronics Unit 5

ARDUINO MICRO WITHOUT HEADERS Code: A000093

Transcription:

d i g i t a l R e a d ( ) [Digital I/O] Reads the value from a specified digital pin, either HIGH or LOW. digitalread(pin) pin: the number of the digital pin you want to read HIGH or LOW Sets pin 13 to the same value as pin 7, declared as an input. int ledpin = 13; // LED connected to digital pin 13int inpin = 7; // pushbutton connected to digital pin 7int val = 0; // variable to store the read value pinmode(ledpin, OUTPUT); // sets the digital pin 13 as output pinmode(inpin, INPUT); // sets the digital pin 7 as input val = digitalread(inpin); // read the input pin digitalwrite(ledpin, val); // sets the LED to the button's value If the pin isn t connected to anything, digitalread() can return either HIGH or LOW (and this can change randomly). The analog input pins can be used as digital pins, referred to as A0, A1, etc. d i g i t a l W r i t e ( ) [Digital I/O] Write a HIGH or a LOW value to a digital pin.

If the pin has been configured as an OUTPUT with pinmode(), its voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW. If the pin is configured as an INPUT, digitalwrite() will enable (HIGH) or disable (LOW) the internal pullup on the input pin. It is recommended to set the pinmode() to INPUT_PULLUP to enable the internal pull-up resistor. See the digital pins tutorial for more information. If you do not set the pinmode() to OUTPUT, and connect an LED to a pin, when calling digitalwrite(high), the LED may appear dim. Without explicitly setting pinmode(), digitalwrite() will have enabled the internal pull-up resistor, which acts like a large current-limiting resistor. digitalwrite(pin, value) pin: the pin number value: HIGH or LOW The code makes the digital pin 13 an OUTPUT and toggles it by alternating between HIGH and LOW at one second pace. pinmode(13, OUTPUT); digitalwrite(13, HIGH); digitalwrite(13, LOW); // sets the digital pin 13 as output // sets the digital pin 13 on // sets the digital pin 13 off The analog input pins can be used as digital pins, referred to as A0, A1, etc. p i n M o d e ( ) [Digital I/O] Configures the specified pin to behave either as an input or an output. See the description of (digital pins) for details on the functionality of the pins.

As of Arduino 1.0.1, it is possible to enable the internal pullup resistors with the mode INPUT_PULLUP. Additionally, the INPUT mode explicitly disables the internal pullups. pinmode(pin, mode) pin: the number of the pin whose mode you wish to set mode: INPUT, OUTPUT, or INPUT_PULLUP. (see the (digital pins) page for a more complete description of the functionality.) The code makes the digital pin 13 OUTPUT and Toggles it HIGH and LOW pinmode(13, OUTPUT); digitalwrite(13, HIGH); digitalwrite(13, LOW); // sets the digital pin 13 as output // sets the digital pin 13 on // sets the digital pin 13 off The analog input pins can be used as digital pins, referred to as A0, A1, etc. a n a l o g R e a d ( ) [Analog I/O] Reads the value from the specified analog pin. The Arduino board contains a 6 channel (7 channels on MKR boards, 8 on the Mini and Nano, 16 on the Mega), 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.9 mv) per unit. The input range and resolution can be changed using analogreference(). It takes about 100 microseconds (0.0001 s) to read an analog input, so the maximum reading rate is about 10,000 times a second.

analogread(pin) pin: the number of the analog input pin to read from (0 to 5 on most boards, 0 to 6 on MKR boards, 0 to 7 on the Mini and Nano, 0 to 15 on the Mega) int(0 to 1023) The code reads the voltage on analogpin and displays it. int analogpin = 3; // potentiometer wiper (middle terminal) connected to analog pin 3 // outside leads to ground and +5Vint val = 0; // variable to store the value read Serial.begin(9600); // setup serial val = analogread(analogpin); // read the input pin Serial.println(val); // debug value If the analog input pin is not connected to anything, the value returned by analogread() will fluctuate based on a number of factors (e.g. the values of the other analog inputs, how close your hand is to the board, etc.). a n a l o g R e f e r e n c e ( ) [Analog I/O] Configures the reference voltage used for analog input (i.e. the value used as the top of the input range). The options are: Arduino AVR Boards (Uno, Mega, etc.) DEFAULT: the default analog reference of 5 volts (on 5V Arduino boards) or 3.3 volts (on 3.3V Arduino boards) INTERNAL: an built-in reference, equal to 1.1 volts on the ATmega168 or ATmega328P and 2.56 volts on the ATmega8 (not available on the Arduino Mega) INTERNAL1V1: a built-in 1.1V reference (Arduino Mega only) INTERNAL2V56: a built-in 2.56V reference (Arduino Mega only)

EXTERNAL: the voltage applied to the AREF pin (0 to 5V only) is used as the reference. Arduino SAMD Boards (Zero, etc.) AR_DEFAULT: the default analog reference of 3.3V AR_INTERNAL: a built-in 2.23V reference AR_INTERNAL1V0: a built-in 1.0V reference AR_INTERNAL1V65: a built-in 1.65V reference AR_INTERNAL2V23: a built-in 2.23V reference AR_EXTERNAL: the voltage applied to the AREF pin is used as the reference Arduino SAM Boards (Due) AR_DEFAULT: the default analog reference of 3.3V. This is the only supported option for the Due. analogreference(type) type: which type of reference to use (see list of options in the description). After changing the analog reference, the first few readings from analogread() may not be accurate. Don t use anything less than 0V or more than 5V for external reference voltage on the AREF pin! If you re using an external reference on the AREF pin, you must set the analog reference to EXTERNAL before calling analogread(). Otherwise, you will short together the active reference voltage (internally generated) and the AREF pin, possibly damaging the microcontroller on your Arduino board. Alternatively, you can connect the external reference voltage to the AREF pin through a 5K resistor, allowing you to switch between external and internal reference voltages. Note that the resistor will alter the voltage that gets used as the reference because there is an internal 32K resistor on the AREF pin. The two act as a voltage divider, so, for example, 2.5V applied through the resistor will yield 2.5 * 32 / (32 + 5) = ~2.2V at the AREF pin. a n a l o g W r i t e ( ) [Analog I/O] Writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying brightnesses or drive a motor at various speeds. After a call to analogwrite(), the pin will generate a steady square wave of the specified duty cycle until the next call to analogwrite() (or a call to digitalread() or digitalwrite()) on the same pin. 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.

On most Arduino boards (those with the ATmega168 or ATmega328P), this function works on pins 3, 5, 6, 9, 10, and 11. On the Arduino Mega, it works on pins 2-13 and 44-46. Older Arduino boards with an ATmega8 only support analogwrite() on pins 9, 10, and 11. The Arduino DUE supports analogwrite() on pins 2 through 13, plus pins DAC0 and DAC1. Unlike the PWM pins, DAC0 and DAC1 are Digital to Analog converters, and act as true analog outputs. You do not need to call pinmode() to set the pin as an output before calling analogwrite(). The analogwrite function has nothing to do with the analog pins or the analogread function. analogwrite(pin, value) pin: the pin to write to. Allowed data types: int. value: the duty cycle: between 0 (always off) and 255 (always on). Allowed data types: int Sets the output to the LED proportional to the value read from the potentiometer. int ledpin = 9; // LED connected to digital pin 9int analogpin = 3; // potentiometer connected to analog pin 3int val = 0; // variable to store the read value pinmode(ledpin, OUTPUT); // sets the pin as output val = analogread(analogpin); // read the input pin analogwrite(ledpin, val / 4); // analogread values go from 0 to 1023, analogwrite values from 0 to 255 The PWM outputs generated on pins 5 and 6 will have higher-than-expected duty cycles. This is because of interactions with the millis() and delay() functions, which share the same internal timer used to generate those PWM outputs. This will be noticed mostly on low duty-cycle settings (e.g. 0-10) and may result in a value of 0 not fully turning off the output on pins 5 and 6.