Download presentation
Presentation is loading. Please wait.
Published byValerija Perović Modified over 6 years ago
1
Arduino Practice: Photoresistors, PWM, Potentiometers, Motors
EGR 326 February 7, 2019
2
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
4
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
5
* 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”
6
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)
7
Pulldown Resistor Pull-down Resistor
8
Sketch reads pin value of 1/0 (high/low)
Arduino board pin set to 5V/0V Input value of 5V/0V
9
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.
10
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
11
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);
12
Photoresistor Circuit
5 V 10 kΩ photoresistor analog input pin
13
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);
14
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);
15
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
16
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--) {
17
Photoresistor & LED - expanded on following slides
/* photoR_PWM sketch from 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 */ // 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);
18
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); }
19
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); }
20
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
21
Potentiometer Circuit
5 V potentiometer analog input pin
22
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
23
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);
24
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.
25
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.
26
Circuit with a Motor & a Transistor
27
When using a transistor, check closely for how pins are labeled!!
Base Emitter Collector
28
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
29
Motor Sketch and Circuit
Circuit elements Push button 10k ohm resistor 9V DC motor TIP120 transistor 1N4001 diode 9V battery
30
TIP 120 Transistor
31
Motor Sketch and Circuit
32
/* 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 This example code is in the public domain. */
33
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 }
34
To Get Motor Code, Go To: and scroll to the bottom of the pane of code you want, and click “get code”
35
Thermistor Sketch 1, More at WebLink Below
// Thermistor sketch from #define SERIESRESISTOR // 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);
36
Thermistor Circuit
37
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.