6th Inning

From LVL1
Revision as of 00:49, 26 June 2015 by Notyou007a (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Introduction

Potentiometers and voltage dividers This inning we will hook up a potentiometer and play with voltage dividers. A voltage divider is a simple circuit where you have two resistors in series and you are reading the voltage in between the two resistors. These are very important because there are many sensors that are resistive in nature, so all you have to do is put the sensor in series with a resistor and read the changing value on an analog pin! Check out the wikipedia article...http://en.wikipedia.org/wiki/Voltage_divider

Resistive divider.png

A potentiometer is nothing but one long resistor - with a movable center tap. The center tap, electrically, is what divides the voltage!

Reochord.jpg

Components Needed

  • Freeduino or Arduino or clone
  • USB cable for Freeduino
  • Freeduino development software - download here!
  • Solderless Breadboard
  • Hookup wire 22gauge solid
  • potentiometer soldered up to a couple of long leads

How to

  1. Connect the two outer wires to the power bus (5v) and Ground (0v)
  2. Connect the middle wire to Analog 0

Schematic

Lvl1-Inning6 schem.jpg

Fritzing

Lvl1-Inning6 bb.jpg

Code

//This code builds upon Inning 5 by
//showing the value of a pot on analog 0
//bpw 5/11/10


// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

int inPin = 2;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status

int potpin = 0;  // analog pin 0
int potval = 0;     // variable for reading the pin status

void setup() {
  // set up the LCD's number of rows and columns: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("hello, world!");
  
  pinMode(inPin, INPUT);    // declare pushbutton as input
  digitalWrite(inPin,HIGH);  //engages internal pullup resistor


}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis()/1000);
  
  lcd.setCursor(6, 1);  //col 6, row 1
  //code for button pressed!
  val = digitalRead(inPin);  // read input value
  if (val == LOW) {         // check if the input is HIGH (button released)
    lcd.print("DOWN");
  } else {
    lcd.print("UP  ");
  }
  
  //read the pot - values are 0..1023
  lcd.setCursor(12, 1);  //col 12, row 1
  lcd.print("    ");
  val = analogRead(potpin);    // read the value from the (center lead of ) potentiometer connected to potpin
  lcd.setCursor(12, 1);  //col 12, row 1
  lcd.print(val);
  
}


Troubleshooting

References

http://www.arduino.cc/en/Tutorial/Potentiometer