Arduino Practice: Photoresistors, PWM, Potentiometers, Motors

Slides:



Advertisements
Similar presentations
Anurag Dwivedi & Rudra Pratap Suman.  Open Source electronic prototyping platform based on flexible easy to use hardware and software.
Advertisements

Basic DC Motor Circuits
Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.
Intro to the Arduino Topics: The Arduino Digital IO Analog IO Serial Communication.
Digital & Analog Inputs. Review Fundamental parts of an Arduino program are … Setting output types using pinMode. Declaring variables Can write a digital.
Introduction to Arduino Programming January MER-421:Mechatronic System Design.
Introduction.
Embedded Programming and Robotics Lesson 2 C Programming Refresher C Programming1.
Basic Circuits – Lab 2 Arduino and Sensors Xmedia Spring 2011.
Embedded Programming and Robotics
Image of Arduino. Arduino discussion Address issues with circuit walk-through – Electricity, Programming, Arduino Concepts Work on BeatTable (next week)
1 Applied Control Systems Technology. 2 Pin configuration Applied Control Systems.
Microprocessors Tutorial 1: Arduino Basics
ProtoSnap Introduction to Arduino Casey Haskell, Pete Lewis, David Stillman, Jim Lindblom, Pete Dokter, Lindsay Levkoff, Trevor Zylstra.
Cascade switching of an LED EAS 199B Winter 2013.
Tweaking Your Simon Adding a photoresistor and changing code Instruction by Pete Lewis and Linz Craig.
Sparkfun Electronics ATtiny85 Arduino Quick Reference Sheet
Good LED Circuit 5V0 GND. What Voltage Does Meter See? Answer: 5 V.
Microprocessors Tutorial 1: Arduino Basics
ARDUINO 1. Basics  Comments  /* * Blink * * The basic Arduino example. Turns on an LED on for one second, * then off for one second, and so on... We.
Arduino Circuits and Code. int ledPin = 9; void setup() { pinMode(ledPin, OUTPUT); } void loop() { digitalWrite(ledPin, LOW); delay(1000); digitalWrite(ledPin,
INTERNET OF EVERYTHING SDU 2016 Week 4. Simple Digital and Analog Inputs  The Arduino’s ability to sense digital and analog inputs allows it to respond.
Microcontroller basics Embedded systems for mortals.
ME 120: User-defined functions: average analog input reading Arduino Programming – Part 5: User-defined functions ME 120 Mechanical and Materials Engineering.
Microcontroller basics Embedded systems for mortals.
1 Introduction to Haptics Introduction to the Hapkit board Allison M. Okamura Stanford University.
Arduino “Getting Started” Instructor : Dr Matthew Miss Khin Yi Kyaw
Pulse-Width Modulation: Simulating variable DC output
Arduino Application: Speed control of small DC Motors
ME 120: Photoresistors and Arduino Programming Arduino Programming Case Study: Photoresistor Measurements ME 120 Mechanical and Materials Engineering Portland.
1 Microcontrollers. 2 Programmers work in the virtual world Machinery works in the physical world Microcontrollers connect the virtual and physical world.
Hacking on Arduino George Patterson
physical computing .2 – Day 3
Prototyping with Microcontrollers and Sensors. Overview Objective Background Information Problem Statement Materials Procedure Assignment Closing.
Harpeth Hall Jan 2016 Introduction to Arduino Prepared for Harpeth Hall Winterim January 2016.
Microcontrollers & Electronics Basics OR…
Arduino.
Application of Programming: Scratch & Arduino
Sparkfun Electronics ATtiny85 Arduino Quick Reference Sheet
Assist. Prof. Rassim Suliyev - SDU 2017
Microcontroller basics
Microcontroller basics
Wireless Cue Light Project
Microprocessors Tutorial 1: Arduino Basics
Microcontroller basics
UTA010 : Engineering Design – II
Cascade switching of an LED
Get Your Project Started with Arduino
UCD ElecSoc Robotics Club 2017/2018
Lab 1: Arduino Basics Topics: Arduino Fundamentals, First Circuit
Arduino Part 1 Topics: Microcontrollers Programming Basics
3.0 ARDUINO WORKSHOP PRESENTATION FOR STUDENTS IN 4º DEGREE OF COMPULSORY SECONDARY EDUCATION 3.0.
Въведение в Arduino.
How to avoid catching things on fire.
Analog Input through POT
Roller Coaster Design Project
Introduction to Arduinos
ARDUINO     What is an Arduino? Features 14 Digital I/O pins 6 Analogue inputs 6 PWM pins USB serial 16MHz Clock speed 32KB Flash memory 2KB SRAM.
What is an Arduino ? Open Source electronic prototyping platform based on flexible easy to use hardware and software.
Arduino Application: Speed control of small DC Motors
IoT Programming the Particle Photon.
Arduino Week 2 Lab ECE 1020 Prof. Ahmadi.
Welcome to Digital Electronics using the Arduino Board
Chapter 1 Introduction of Arduino
Arduino : Introduction & Programming
Programming 2: The Arduino IDE & First Sketches
Sensors and actuators Sensors Resistive sensors
Arduino Uno circuit basics
Introduction to Arduinos
Pulse-Width Modulation: Simulating variable DC output
Presentation transcript:

Arduino Practice: Photoresistors, PWM, Potentiometers, Motors EGR 326 February 7, 2019

Overview Recap Arduino pins Discuss use of pull-up and pull-down resistors Write and implement sketches and circuits for Photoresistor Termistor Potentiometer LED – on/off and PWM Buzzer – on/off, tones, PWM Motor (and transistors), can also use PWM for motor speed Serial monitor – text output to screen, sensor data, debugging

https://www.arduino.cc/en/reference/board

Analog In Pins 0-5 (light blue) Digital Ground (light green) Digital Pins 2-13 (green) Digital Pins 0-1 (dark green) Either for digital I/O or for serial communication, TX/RX Analog In Pins 0-5 (light blue) Power & Ground Pins (power: orange, grounds: light orange) External Power In (9-12VDC), Barrel Jack – X1 (pink) USB (is also used to power the board) (yellow) For uploading sketches to the board For serial communication between the board and the computer

* Read Book and Try This * // a sketch for a circuit that has switch controlling an LED int inputPin = 5; int ledPin = 13; void setup() { pinMode(ledPin, OUTPUT); pinMode(inputPin, INPUT); digitalWrite(inputPin, HIGH); // activate the internal pull-up R } void loop() int switchOpen = digitalRead(inputPin); digitalWrite(ledPin, ! switchOpen); Remember this sketch for the future – the use of digitalWrite() and the pull-up resistor. See figure ~6.7, and sketch 6.04 in “Getting Started with Sketches”

Pullup / Pulldown Resistors Available with digital and analog pins Think about a switch being open or closed ‘Closed’ creates a path for current ‘Open’ does what?  ‘floats’ Pull-up Resistor (McRoberts book)

Pulldown Resistor Pull-down Resistor

Sketch reads pin value of 1/0 (high/low) Arduino board pin set to 5V/0V Input value of 5V/0V

Formal: Pull-up/down Resistors It is good practice to set an input pin to a known state if no input is present Pin should be definitely 5V or 0V, and not be allowed to ‘float’ This can be done by adding a pullup resistor (to +5V), or a pulldown resistor (resistor to ground) on the input, with 10K (or 20k) being a common value.

Formal: Pull-up/down Resistors There are 20KΩ pullup resistors built into the Arduino chip that can be accessed from software. These built-in pullup resistors are accessed in the following manner. pinMode(pin, xxxx); // set pin to input/output digitalWrite(pin, HIGH); // turn on pullup resistor

Formal: Pull-up/down Resistors The analog pins also have pull-up resistors enabled by // set pull-up on analog pin 0 pinMode(A0, INPUT_PULLUP); Be aware that turning on a pull-up will affect the values reported by analogRead(). To use analog pins as digital pins, use the aliases A0 (for analog input 0), A1, etc. to set analog pin 0 as output, and to set it HIGH: pinMode(A0, OUTPUT); digitalWrite(A0, HIGH);

Photoresistor Circuit 5 V 10 kΩ photoresistor analog input pin

Photoresistor Sketch: Complete int sensorVal; const int sensorPin = 4; float voltage; const float input2volts = 5.0/1023.0; // 5V, 10 bits void setup() { Serial.begin(9600); } void loop() { sensorVal = ____________( _________ ); voltage = ______________ * ________; Serial.print(“SensorVal, Voltage = “); Serial.print(sensorVal); Serial.print(“ “); Serial.println(voltage);

Photoresistor Sketch int sensorVal; const int sensorPin = 4; float voltage; const float input2volts = 5.0/1023.0; // 5V, 10 bits void setup() { Serial.begin(9600); } void loop() { sensorVal = analogRead( sensorPin ); voltage = float(sensorVal)*input2volts; Serial.print(“SensorVal, Voltage = “); Serial.print(sensorVal); Serial.print(“ “); Serial.println(voltage);

Recap: Sensor Data Conversion Convert the input sensor (analog) data voltage value to a digital output value Input pins are allocated 10-bits Analog voltage input from 0V to 5V is converted to a digital value in the range 0 to 1023 Output PWM pins have 8-bits PWM pins are a subset of the digital output pins PWM output must be in the range 0 to 255

Recap PWM Code // Initialze LED pin const int led_pin = 6; void setup() { // declare LED pin as output pinMode (led_pin, OUTPUT); } void loop() { // Fade the LED up and down for (int i = 0; i <=255; i++) { analogWrite (led_pin, i); delay (30); for (int i = 255; i <=0; i--) {

Photoresistor & LED - expanded on following slides /* photoR_PWM sketch from https://www.instructables.com/id/Project-2-Photo-Resistor/ Sketch, to modify, so that the LED output intensity will be defined via PWM based in light level input from the photoresistor J. Cardell, modified from download, Feb 2019 */ // define constants, pin numbers for the circuit I/O const int pResistor = A0; const int LED = 9; // Define pin function as OUTPUT and INPUT void setup() { pinMode(LED, OUTPUT); pinMode(pResistor, INPUT); } // Read the analog input from the photoresistor and turn // the LED on or off depending upon the input level void loop(){ static int value; value = analogRead(pResistor); if (value > 300) { digitalWrite(LED, LOW); } else { digitalWrite(LED, HIGH); } delay(500);

Photoresistor & LED const int pResistor = A0; const int LED = 9; // define constants, pin numbers for the circuit I/O const int pResistor = A0; const int LED = 9; // Define pin function as OUTPUT and INPUT void setup() { pinMode(LED, OUTPUT); pinMode(pResistor, INPUT); }

Photoresistor & LED static int value; value = analogRead(pResistor); // Read the analog input from the photoresistor and turn // the LED on or off depending upon the input level void loop(){ static int value; value = analogRead(pResistor); if (value > 300) { digitalWrite(LED, LOW); } else { digitalWrite(LED, HIGH); } delay(500); }

Options In Class Write or modify the sketch to: Convert analog input to digital output Use PWM for LED output based on photoresistor input Use a potentiometer input instead of the photoresistor, and decide upon an interesting output

Potentiometer Circuit 5 V potentiometer analog input pin

Potentiometer Sketch const int sensorPin = 4; // Connect output pin of pot to pin 4 void setup() { Serial.begin(9600); } void loop() { int potVal; potVal = analogRead( sensorPin ); Serial.print(“Reading = “); Serial.println( potVal ); // ----- EXPAND SKETCH to light an LED -----

Potentiometer Sketch con’t const int sensorPin = 4; // Connect output pin of pot to pin 4 const int LEDpin = 5; void setup() { Serial.begin(9600); pinMode (LEDpin, OUTPUT); } void loop() { int potVal, LEDbright; potVal = analogRead( sensorPin ); // map input to output value, using floating point math LEDbright = potVal * 255.0/1023.0; analogWrite (LEDpin, LEDbright);

Controlling a Motor The current output from an Arduino pin is WAY TOO LOW to power a motor (and many other devices) The solution is to use the Arduino to control a transistor.

Controlling a Motor A transistor is powered by a separate power supply (C & E pins – collector & emitter) A transistor is an electronic switch, controlled through “B” the ‘base’ terminal Compare to mechanical switch Controlling the transistor allows you to control access to, or use of, the separate power supply.

Circuit with a Motor & a Transistor

When using a transistor, check closely for how pins are labeled!! Base Emitter Collector

Sketch sets pin to 1 or 0 (high/low) Arduino pin then outputs 5V or 0V This ‘control’ input to transistor uses 5V/0V to switch motor on/off

Motor Sketch and Circuit Circuit elements Push button 10k ohm resistor 9V DC motor TIP120 transistor 1N4001 diode 9V battery https://www.arduino.cc/en/Tutorial/TransistorMotorControl

TIP 120 Transistor

Motor Sketch and Circuit https://www.arduino.cc/en/Tutorial/TransistorMotorControl

/* Motor Control with a Transistor This example shows you how to control a motor's using a transistor. When a pushbutton on pin 2 is pressed, the Arduino will control a transistor via PWM, which will slowly ramp up the motor's speed, then slow it down. The circuit : * momentary switch with one end connected to 5V, the other end connected to GND through a 10-kilohm resistor, and digital pin 2. * TIP120 tranistor, with the Base connected to digital pin 9, the Emitter to ground, and the Collector to one lead from a 9V DC motor * a 9V battery, with the ground connected to the Arduino's ground, and the power connected to the motor * 1N4001 diode across the motor's leads, with the striped side conneted to the 9V The Arduino can only provide 40mA at 5V on its pins. Most motors require more current and/or voltage to overcome intertia and run. A transistor can act as a digital switch, enabling the Arduino to control loads with higher electrical requirements. Created on 03 January 2013 by Scott Fitzgerald http://www.arduino.cc/en/Tutorial/TransistorMotorControl This example code is in the public domain. */

int pushButton = 2; // Define digital pin 2, which has a pushbutton attached int motorControl = 9; // transistor to control motor attached to digital pin 9 void setup() {   pinMode(pushButton, INPUT); // set the pushbutton's pin an input   pinMode(motorControl, OUTPUT);   // set the transistor's pin an output } void loop() {   // read the state of the button and check if it is pressed   if(digitalRead(pushButton) == HIGH){     // ramp up the motor speed     for(int x = 0; x <= 255; x++){       analogWrite(motorControl, x);       delay(50);     }     // ramp down the motor speed     for(int x = 255; x >= 0; x--){       analogWrite(motorControl, x);       delay(50);     }       }   delay(1);        // delay in between reads for stability }

To Get Motor Code, Go To: https://www.arduino.cc/en/Tutorial/TransistorMotorControl and scroll to the bottom of the pane of code you want, and click “get code” https://www.arduino.cc/en/Tutorial/TransistorMotorControl?action=sourceblock&num=4

Thermistor Sketch 1, More at WebLink Below // Thermistor sketch from https://learn.adafruit.com/thermistor/using-a-thermistor #define SERIESRESISTOR 10000 // the value of the 'other' resistor #define THERMISTORPIN A0 // What pin to connect the sensor to void setup(void) { Serial.begin(9600); } void loop(void) { float reading; reading = analogRead(THERMISTORPIN); Serial.print("Analog reading "); Serial.println(reading); // convert the value to resistance reading = (1023 / reading) - 1; // (1023/ADC - 1) reading = SERIESRESISTOR / reading; // 10K / (1023/ADC - 1) Serial.print("Thermistor resistance "); delay(1000);

Thermistor Circuit

Summary Try two or three new circuits and sketches today in class Select your Input: button, potentiometer, sensor: thermistor, photoresistor Output: LED, motor, buzzer LED – on/off, PWM? Use the serial communication (built in, or CoolTerm) in some way to monitor your circuit and what the Arduino is receiving or doing. Expand comfort with what you can do with the Arduino