Arduino Programming Part 3. EAS 199A Fall 2011

Similar documents
Basic DC Motor Circuits. Living with the Lab Gerald Recktenwald Portland State University

Basic DC Motor Circuits

Basic Pulse Width Modulation

Arduino Lesson 13. DC Motors. Created by Simon Monk

Microcontroller Programming Beginning with Arduino. Charlie Mooney

Lab 6 Introduction to Serial and Wireless Communication

Electronic Brick of Current Sensor

Your Multimeter. The Arduino Uno 10/1/2012. Using Your Arduino, Breadboard and Multimeter. EAS 199A Fall Work in teams of two!

Theory and Practice of Tangible User Interfaces. Thursday Week 2: Digital Input and Output. week. Digital Input and Output. RGB LEDs fade with PWM

Lecture 7: Programming for the Arduino

Introduction to Arduino

Arduino Microcontroller Guide W. Durfee, University of Minnesota ver. oct-2011 Available on-line at

Arduino Lesson 16. Stepper Motors

CanSat Program. Stensat Group LLC

Arduino Motor Shield (L298) Manual

Arduino project. Arduino board. Serial transmission

1602 LCD adopts standard 14 pins(no backlight) or 16pins(with backlight) interface, Instruction of each pin interface is as follows:

Programming the Arduino

Arduino Shield Manual

Computer Architectures

1 of 5 12/31/ :51 AM

Ordering Part Numbers: Universal Signal Converter: USC-CVB Universal Signal Converter with a 4-20mA input and +/- 10V output: USC-CVB225-B10V

Experiment 8 : Pulse Width Modulation

Automation System TROVIS 6400 TROVIS 6493 Compact Controller

Temperature Measurement with a Thermistor and an Arduino

4/Really Getting Started with Arduino

Eric Mitchell April 2, 2012 Application Note: Control of a 180 Servo Motor with Arduino UNO Development Board

Electronics 5: Arduino, PWM, Mosfetts and Motors

Temperature Measurement with a Thermistor and an Arduino

Arduino-Based Dataloggers: Hardware and Software David R. Brooks Institute for Earth Science Research and Education V 1.2, June, , 2015

RAMPS 1.4 Assembly Guide

Using Arduino Microcontrollers to Sense DC Motor Speed and Position

AirCasting Particle Monitor Bill of Materials

dspic30f4012 Microcontroller

Arduino Lesson 5. The Serial Monitor

1 Coffee cooling : Part B : automated data acquisition

H-Bridge Motor Control

cs281: Introduction to Computer Systems Lab08 Interrupt Handling and Stepper Motor Controller

SYSTEM 4C. C R H Electronics Design

Ultrasonic Distance Measurement Module

Arduino Shield Manual

Arduino Lesson 1. Blink

AN2680 Application note

How To Control A Car With A Thermostat

Three Arduino Challenges to Connect the Logical World with the Physical One. ISTE Philadelphia

Arduino Lesson 14. Servo Motors

How To Use An Ams 5812 Pressure Sensor With A Usb Starter Kit

Data Acquisition Module with I2C interface «I2C-FLEXEL» User s Guide

SYSTEM 45. C R H Electronics Design

DS1307 Real Time Clock Breakout Board Kit

Conversion Between Analog and Digital Signals

Servo Info and Centering

Sending an SMS with Temboo

What Does Rail-to-Rail Operation Really Mean?

Cornerstone Electronics Technology and Robotics I Week 15 Voltage Comparators Tutorial

Operational Amplifier - IC 741

3½ DIGIT VOLTMETER MODULE LED- / LCD- SERIES

GM862 Arduino Shield

A Practical Guide to Free Energy Devices

Arduino Lesson 4. Eight LEDs and a Shift Register

Arduino DUE + DAC MCP4922 (SPI)

Arduino Lesson 9. Sensing Light

If an occupancy of room is zero, i.e. room is empty then light source will be switched off automatically

Accurate Measurement of the Mains Electricity Frequency

Current Loop Tuning Procedure. Servo Drive Current Loop Tuning Procedure (intended for Analog input PWM output servo drives) General Procedure AN-015

Theory of Operation. Figure 1 illustrates a fan motor circuit used in an automobile application. The TPIC kω AREF.

DEPARTMENT OF ELECTRONICS ENGINEERING

Microcontroller-based experiments for a control systems course in electrical engineering technology

Programming PIC Microcontrollers in PicBasic Pro Lesson 1 Cornerstone Electronics Technology and Robotics II

Pulse Width Modulation (PWM) LED Dimmer Circuit. Using a 555 Timer Chip

Current Loop Application Note 1495

Electronics. Discrete assembly of an operational amplifier as a transistor circuit. LD Physics Leaflets P

A REST API for Arduino & the CC3000 WiFi Chip

ANDROID BASED FARM AUTOMATION USING GR-SAKURA

EXPERTS IN WATER COOLING SINCE aquastream ULTIMATE SUPER SILENT PUMP TECHNOLOGY. The ultimate pump system.

ZX-NUNCHUK (# )

Creating a Project with PSoC Designer

Working with microcontroller-generated audio frequencies (adapted from the Machine Science tutorial)

Surveillance System Using Wireless Sensor Networks

PLL frequency synthesizer

Thermistor. Created by Ladyada. Last updated on :30:46 PM EDT

C4DI Arduino tutorial 4 Things beginning with the letter i

The Flyback Converter

LIFT CONTROL SYSTEMS CATALOGUE 2007

MSFET MICROSENS Miniature ph Sensor Module

Electronic Control Devices The European Product Catalogue 2007

Lab Experiment 1: The LPC 2148 Education Board

IR Communication a learn.sparkfun.com tutorial

ETEC Digital Controls PIC Lab 10 Pulse Width Modulation

EMC6D103S. Fan Control Device with High Frequency PWM Support and Hardware Monitoring Features PRODUCT FEATURES ORDER NUMBERS: Data Brief

Arduino Library for the Pololu QTR Reflectance Sensors

1. Learn about the 555 timer integrated circuit and applications 2. Apply the 555 timer to build an infrared (IR) transmitter and receiver

Contents. Document information

Trinket Bluetooth Alarm System

Principles of Adjustable Frequency Drives

DMX2PWM 9 Channel Dimmer Setup Manual

Option field bus: without bus with profibus DP. Design-Index (Subject to change)

An Introduction to MPLAB Integrated Development Environment

GT Sensors Precision Gear Tooth and Encoder Sensors

Modern Robotics, Inc Core Device Discovery Utility. Modern Robotics Inc, 2015

Transcription:

Arduino Programming Part 3 EAS 199A Fall 2011

Overview Part I Circuits and code to control the speed of a small DC motor. Use potentiometer for dynamic user input. Use PWM output from Arduino to control a transistor. Transistor acts as variable voltage switch for the DC motor. Part II Consolidate code into reusable functions. One function maps 10-bit analog input to 8-bit PWM output. Another function controls the motor speed. Using functions provides modular features that are useful for more complex control tasks, e.g. the desktop fan project. 2

Part 1: Control motor speed with a pot Increase complexity gradually 1. Use a pot to generate a voltage signal (a) Read voltage with analog input (b) Print voltage to serial monitor to verify 2. Convert 10-bit voltage scale to 8-bit PWM scale (a) Voltage input is in range 0 to 1023 (b) PWM output needs to be in the range 0 to 255 (c) Print voltage to serial monitor to verify 3. Write PWM data to DC motor 4. Write a function to linearly scale the data 5. Write a function to update the motor 3

Potentiometer Circuit Use the potentiometer from the Arduino Inventor s Kit 5V V in Analog input pin V out 4

Code to print potentiometer reading // Function: read_potentiometer // // Read a potentiometer and print the reading int sensor_pin = 3; void setup() Serial.begin(9600); // Wire sweeper of pot to // analog input pin 3 void loop() int val; val = analogread( sensor_pin ); Serial.print("reading = "); Serial.println( val ); 5

DC Motor Control Circuit +5V Snubber Diode motorpin 330Ω NPN Transistor Add this to the breadboard with the potentiometer circuit 6

DC Motor Control Circuit 5V +5V Analog input pin motorpin 330Ω Arduino 7

Control the DC motor with PWM Output // Function: DC_motor_control_pot // // Use a potentiometer to control a DC motor int sensor_pin = 3; int motor_pin = 5; // must be a PWM digital output void setup() Serial.begin(9600); pinmode(motor_pin, OUTPUT) void loop() int pot_val, motor_speed; pot_val = analogread( sensor_pin ); motor_speed = pot_val*255.0/1023.0; analogwrite( motor_pin, motor_speed); // Include decimal Subtle: Don t use integer values of 255 and 1023 here. Aggressive compilers pre-compute the integer division of 255/1023 as zero. 8

Take Stock You now have a working solution Potentiometer creates a variable voltage input that is read by the Arduino (analogread) Motor speed is controlled via PWM Next step: Use functions to encapsulate code Reuse code Organize code: Isolated activities happen inside a function. 9

Part II: Create functions for reusable code // Function: DC_motor_control_pot // // Use a potentiometer to control a DC motor int sensor_pin = 3; int motor_pin = 5; // must be a PWM digital output void setup() Serial.begin(9600); pinmode(motor_pin, OUTPUT) Adjust motor speed void loop() int pot_val, motor_speed; pot_val = analogread( sensor_pin ); motor_speed = pot_val*255.0/1023.0; // Include decimal analogwrite( motor_pin, motor_speed); Map input values to output scale 10

Final version of the loop() function // Function: DC_motor_control_pot // // Use a potentiometer to control a DC motor int sensor_pin = 3; int motor_pin = 5; // must be a PWM digital output void setup() Serial.begin(9600); pinmode(motor_pin, OUTPUT) void loop() adjust_motor_speed( sensor_pin, motor_pin); // do other useful stuff adjust_motor_speed takes care of the two main tasks: reading the potentiometer output sand setting the PWM signal to the transistor 11

Arduino web site Using and Writing Functions http://www.arduino.cc/en/reference/functiondeclaration Functions are reusable code modules: Functions encapsulate details of tasks into larger building blocks Well-written functions can be reused Functions can accept input (or not) and return output (or not) All Arduino sketches have at least two functions setup: runs once to configure the system loop: runs repeatedly after start-up is complete Users can add functions in the main sketch file, or in separate files 12

The setup() Function Consider the simple blink sketch void means Returns nothing // Blink.pde: Turn on an LED for one second, then // off for one second. Repeat continuously. void setup() pinmode(13, OUTPUT); void loop() digitalwrite(13, HIGH); delay(1000); digitalwrite(13, LOW); delay(1000); No inputs setup is the name of the function // set the LED on // wait for a second // set the LED off // wait for a second Desktop fan: EAS 199A 13

A Function to Translate Linear Scales Linear scaling from x values to y values: y = f(x) where f is a linear mapping y max y y min x min x x max In words: Given x, xmin, xmax, ymin, and ymax, compute y Desktop fan: EAS 199A 14

A Function to Translate Linear Scales Enter the code at the bottom into your sketch The code is not inside any other program block (like setup or void) How would you test that this function is working? int int_scale(int x, int xmin, int xmax, int ymin, int ymax) int y; y = ymin + float(ymax - ymin)*float( x - xmin )/float(xmax - xmin); return(y); N.B. This code is essentially a reimplementation of the built-in map function. See http://arduino.cc/en/reference/map 15

A Function to Translate Linear Scales returns an int name is int_scale first input is an int named x Use float for better precision int int_scale(int x, int xmin, int xmax, int ymin, int ymax) int y; y = ymin + float(ymax - ymin)*float( x - xmin )/float(xmax - xmin); return(y); return the value stored in y 16

Functions are not nested // Contents of sketch, e.g. motor_control.pde void setup() void loop() int int_scale(int x, int xmin, int xmax, int ymin, int ymax) 17

Functions call other functions // Contents of sketch, e.g. motor_control.pde void setup() void loop() motor_speed = int_scale( pot_val, 0, 1023, 0, 255); int int_scale(int x, int xmin, int xmax, int ymin, int ymax) return( y ); 18

Functions call other functions // Contents of sketch, e.g. motor_control.pde void setup() void loop() motor_speed = int_scale( pot_val, 0, 1024, 0, 255); int int_scale(int x, int xmin, int xmax, int ymin, int ymax) return( y ); 19

Use the int_scale function // Function: DC_motor_control_pot // // Use a potentiometer to control a DC motor int sensor_pin = 3; int motor_pin = 5; // must be a PWM digital output void setup() Serial.begin(9600); pinmode(motor_pin, OUTPUT) void loop() int pot_val, motor_speed; pot_val = analogread( sensor_pin ); motor_speed = int_scale( pot_val, 0, 1023, 0, 255; analogwrite( motor_pin, motor_speed); int int_scale(int x, int xmin, int xmax, int ymin, int ymax) int y; y = ymin + float(ymax - ymin)*float( x - xmin )/float(xmax - xmin); return(y); 20

Inputs A Function to update motor speed sensor pin motor output pin Tasks: Read potentiometer voltage Convert voltage from 10 bit to 8 bit scales Change motor speed void adjust_motor_speed(int sensor_pin, int motor_pin) int motor_speed, sensor_value; sensor_value = analogread(sensor_pin); motor_speed = int_scale(sensor_value, 0, 1023, 0, 255); analogwrite( motor_pin, motor_speed); Serial.print("Pot input, motor output = "); Serial.print(sensor_value); Serial.print(" "); Serial.println(motor_speed); 21

Functions call functions, call functions, // Contents of sketch, e.g. motor_control.pde void setup() void loop() adjust_motor_speed(, ) void adjust_motor_speed(int sensor_pin, int motor_pin) motor_speed = int_scale(,,,, ); int int_scale(int x, int xmin, int xmax, int ymin, int ymax) return( y ); 22