Saturday, March 26, 2011

Class 3

Today's class is going to be an exciting one. We have some new concepts to work with that are a little hard at first to understand but are very powerful. Here is a look at what we are going to learn today:

  1. Analog Input: Potentiometer (lab)
  2. Thresholds and calibration: Brightness sensor (lab)
  3. Arrays and for loops
  4. Add movement 2: Servo Motors (group lab)

Analog Input:

The Arduino has 6 pins that allow you to receive analog values from sensors attached to those pins. The Arduino determines the value by reading the voltage coming into those pins and comparing it to the Arduino standard voltage of 5volts dc. We are going to discover how this works by using a potentiometer. Create the circuit below and then upload the following code onto the Arduino.

/*
Analog Input with Potentiometer(pot)
using the arduinos analog input pins
Matt Richard
http://idblabs.blogspot.com
*/

int potPin = A0; // the pin which the middle pin of the potentiometer is attached
int potValue = 0; // the value that is sent from the potentimeter to the analog pin

void setup() {
// create a serial connection
Serial.begin(9600);
}

void loop() {
// read the value from the potentiometer
potValue = analogRead(potPin);

// display the value of the potentiometer in the serial monitor Serial.println(potValue);
}

Now that we have used a potentiometer lets use the value to vary the delay between turning an LED on and off. Create the circuit below and then upload the following code onto the Arduino. The code is an example available with the Arduino ide found under 3.Analog/AnalogInput.

/*
Analog Input
Demonstrates analog input by reading an analog sensor on analog pin 0 and
turning on and off a light emitting diode(LED) connected to digital pin 13.
The amount of time the LED will be on and off depends on
the value obtained by analogRead().
The circuit:
* Potentiometer attached to analog input 0
* center pin of the potentiometer to the analog pin
* one side pin (either one) to ground
* the other side pin to +5V
* LED anode (long leg) attached to digital output 13
* LED cathode (short leg) attached to ground
* Note: because most Arduinos have a built-in LED attached
to pin 13 on the board, the LED is optional.
Created by David Cuartielles
Modified 4 Sep 2010
By Tom Igoe
This example code is in the public domain.
http://arduino.cc/en/Tutorial/AnalogInput
*/

int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor

void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}

void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for milliseconds:
delay(sensorValue);
}
We can also use a potentiometer to control PWM signals to control an LED. Follow the diagram and then paste the following code. This circuit is almost identical to the last one, other than the LED is changed from pin 13 to pin 6.


/*
Analog input, analog output, serial output
Reads an analog input pin, maps the result to a range from 0 to 255
and uses the result to set the pulsewidth modulation (PWM) of an output pin.
Also prints the results to the serial monitor.
The circuit:
* potentiometer connected to analog pin 0.
Center pin of the potentiometer goes to the analog pin.
side pins of the potentiometer go to +5V and ground
* LED connected from digital pin 6 to ground
created 29 Dec. 2008
Modified 4 Sep 2010
by Tom Igoe
This example code is in the public domain.
*/

// These constants won't change. They're used to give names
// to the pins used:
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 6; // Analog output pin that the LED is attached to

int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}

void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);

// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);

// wait 10 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(10);
}

Thresholds and Calibration:

The potentiometer generated a value from 0-1024, an even range to use as an input. Most sensors do not generate such a nice range of values under normal working conditions. To accurately map a sensor you need to now what the thresholds of the sensor. Each sensor will have a minimum and maximum threshold. Let's use a photocell to change the brightness of an LED. To do this first we will need to determine the minimum and maximum threshold of the photocell. Create the circuit below and then upload the following code onto the Arduino.


/*
Analog input from photocell to determine LED brightness
Matt Richard
http://idblab.blogspot.com
*/

int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 6; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
int minThresh = 0;// replace this number with a lowest number from the serial monitor
int maxThresh = 1000;// replace this number with a highest number from the serial monitor

void setup() {
Serial.begin(9600);
}

void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
analogWrite(ledPin, map(sensorValue, minThresh, maxThresh, 0, 255));
}

Once the program has been uploaded, open the serial monitor to view the incoming values from the photocell. Make note of the value of the photocell in ambient light, this is your maximum threshold. Now cover the photocell with your hand or an object to make it as dark as you can. This value is your minimum threshold. I like to add a small buffer of +-20 to insure that the photocell values do not go out of range under normal conditions, but make sure not to have a number less than 0. This is a simple way to calibrate your sensor. By replacing the values for minThresh and maxThresh with values from the serial monitor the effect of dimming the LED by placing your hand over the photocell has a greater effect on the brightness of the LED.

Arrays and For Loops:

Arrays are a way of declaring multiple of the same type of variable in a row in memory. Usually they are used with the idea that the values are multiples of the same type of object. What I mean is while the value of the photocell is an integer it doesn't make sense to include it in an array of integers that represent the digital pins used to power LEDs. Arrays are declared a little differently then other variables. Here are a few ways to declare an array:

int myInts[6];// this creates an array that has 6 places but does not define what the values are
int myPins[] = {2, 4, 8, 3, 6};// this creates an array of 5 places and defines the values
int mySensVals[6] = {2, 4, -8, 3, 2};// this creates an array of 6 places but only defines the first 5 values

This is how you access the values that are store in the arrays above:

myPins[0], where the value would equal 2.

The number inside of the square brackets [ ] indicates which element to access. Arrays are 0 based, meaning the first element is considered the 0 place not the 1st place.

Here is how you change the value of an element of an array:
myPins[1] = 9;// now the values for myPins look like this: {2, 9, 8, 3, 6}

The real power of arrays is unleashed when combined with for loops. The structure of a for loop is a little confusing at first, but once you understand how to use them they are essential. Basically a for loop is a way of repeating a command a determined number of times. You can use an array to count from any number by any number to any number. This makes it perfect for working with arrays because instead of manually putting 0-4 inside of the [ ] to access the values of myPins array, you can use a variable.

Here is a for loop that prints the values of myPins array to the serial monitor:

for(int i = 0; i < 5; i++){
Serial.print(myPins[i]);
Serial.print(", ");
}
Serial.println();

This snippet of code will display the values with a comma and space between each value and then a line break after the last element has been printed. I recommend that you take a look at the for loop link above. The guys at Arduino have a wonderful description of how the mechanics work.

Let's put it into action so you can see how easy they are to use. Create the circuit below and then upload the following code onto the Arduino.


Working with Motors

The arduino can allow you to control much more than LEDs. In fact you can use the arduino to physically change your environment by using motors. There are many different types of motors, but the most common are brush DC motors, servo motors, and stepper motors.

Brush DC motors : These are the most common motors. You will find them inside of toys, fans, blenders, printers, drills… the list goes on and on. The motor has two terminals, a positive and negative terminal. Simply apply power to one terminal and ground to the other and the motor will spin. Reverse which terminal gets power and ground and the direction reverses. An easy way to control the direction and power source of a motor is to use and H-Bridge.

Stepper Motor : Stepper motors break down a full rotation into a number of steps. Rather than continually rotating like a DC motor, the stepper increments in steps or fractions of a rotation. While you can also use an H-Bridge to control a stepper it is easiest to use the EasyDriver stepper controller.

Servo Motor : Servos are similar to Steppers, in that you tell it to go to a certain angle and it will stop at that angle. The difference is that most packages only have a rotational range of 180 degrees. Servos are used a lot in RC cars, boats and planes to control the steering mechanisms. At its heart a servo is a DC motor with circuitry to understand how far it has rotated. It also usually has gearing to increase its torque.

Servos are incredible easy to use with the arduino. They have three wires, power, ground, and signal. A PWM signal is sent down the signal line and the servo uses that to determine where to rotate to. Arduino has a library that makes this all very easy for us. Build the circuit below and we will control the servo with a potentiometer.


// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott

#include
Servo myservo; // create servo object to control a servo
int potpin = 2; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}

0 comments:

Post a Comment