Analog Input/Output

part 1

Modified code to fade instead of blink.

int sensorPin = A0;      // variable for analog pin (specified as A0-A5)
int ledPin = 13;         // change LED blink rate based on sensor input!
int sensorValue = 0;     // variable to read sensor value

void setup() {
pinMode(ledPin, OUTPUT);

}

void loop() {

// read the sensor’s value (0-1023**)
sensorValue = analogRead(sensorPin);

// set the LED’s brightness to the sensor value
for (int brightness = sensorValue; brightness <= 200; brightness+=5) {             // for loop for adjusting brightness
analogWrite(ledPin, brightness);                                                                         // value of brightness is analog write to pin 12

}

}

Part 2

I only had two of the three sensors

A giant pot and photo resistor. Again the usual initiating variables and setup commands.

Here’s the code

/* POT to LED
*/

int potPin = 0;    // Analog input pin that the potentiometer is attached to
int potValue = 0;  // value read from the pot
int led = 9;      // PWM pin that the LED is on.  n.b. PWM 0 is on digital pin 9
int sensorPin = A1; // 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() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
// declare the led pin as an output:
pinMode(led, OUTPUT);
}

void loop() {
potValue = analogRead(potPin); // read the pot value
analogWrite(led, potValue/5);  // PWM the LED with the pot value (divided by 4 to fit in a byte)
Serial.println(“hello”);      // print the pot value back to the debugger pane
delay(10);                     // wait 10 milliseconds before the next loop
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
analogWrite(ledPin, map(sensorValue, minThresh, maxThresh, 0, 255));
}

Part 3

Essentially just added the Pin Value (0-255) = 255 * (AnalogVolts / 5);

Here’s the code:

int pwmPin = 9; // output pin supporting PWM
int inPin = A0; // voltage connected to analog pin 3, e.g. a potentiometer
int val = 0; // variable to store the read value
float volt = 0; // variable to hold the voltage read
void setup()
{
pinMode(pwmPin, OUTPUT); // sets the pin as output
}
void loop()
{
val = analogRead(inPin); // read the input pin
volt =(5.0 * val) / 1023;
val = 255 * (volt / 5);
analogWrite(pwmPin, val);
}

This also works for outputting a analog value (PWM) of 0-255 to control the brightness of a LED.

int sensorPin = A0;    // Analog input pin to pot
int ledPin = 9;      // PWM pin to LED
int sensorValue = 0;  // variable to store pot value

void setup() {}

void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// converts 0-1023 to 0-255
sensorValue /=4;
// outputs PWM signal to LED
analogWrite(ledPin, sensorValue);

}

Part 4

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s