W2: Resistors, Serial Communication
Potentiometer:
A potentiometer is a knob that provides a variable resistance as an analog value. The value controls the rate at which an LED blinks.

1x Arduino Uno
1x Breadboard
4x Jumpers
1x Potentiometer
1x Resistor
1x LED
int potPin = 2; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
}
void loop() {
val = analogRead(potPin); // read the value from the sensor
digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(val); // stop the program for some time
digitalWrite(ledPin, LOW); // turn the ledPin off
delay(val); // stop the program for some time
}
Light Depedent Resistors:

Photocells are light-sensitive, variable resistors. As more light shines of the sensor’s head, the resistance between its two terminals decreases. Used in ambient-light sensing.
In pitch-black conditions, the photocell’s resistance will be in the megaohm’s (1.0MΩ+) range. Shining an LED on the sensor can drop the resistance to near-zero, but usually the resistance of the photocell falls between 8-20kΩ in normal lighting conditions.
By combining the photocell with a static resistor to create a voltage divider, you can produce a variable voltage that can be read by a microcontroller's analog-to-digital converter.
Photocell that switches on an LED in darkness and turns it off when exposed to light:
1x Photocell
2x Resistors
1x LED
Code: const int sensorPin = 0; const int ledPin = 9; int lightCal; int lightVal;
void setup() { pinMode(ledPin, OUTPUT); lightCal = analogRead(sensorPin); } void loop() { lightVal = analogRead(sensorPin); if (lightVal < lightCal - 50) { digitalWrite(ledPin, HIGH); } else { digitalWrite (ledPin, LOW) } }
TMP36 Temperature Sensor:
5V Arduino - connecting directly to Analog pin:"Voltage at pin in milliVolts = (reading from ADC) * (5000/1024)
This formula converts the number 0-1023 from the ADC into 0-5000mV (= 5V)3.3V Arduino:
Voltage at pin in milliVolts = (reading from ADC) * (3300/1024)
This formula converts the number 0-1023 from the ADC into 0-3300mV (= 3.3V)Convert millivolts into temperature:
Centigrade temperature = [(analog voltage in mV) - 500] / 10" - https://learn.adafruit.com/tmp36-temperature-sensor/using-a-temp-sensor
Code: int sensorPin = 0; void setup() { Serial.begin(9600); } void loop() { int reading = analogRead(sensorPin); float voltage = reading * 5.0; voltage /= 1024.0; Serial.print(voltage); Serial.println(" volts"); float temperatureC = (voltage - 0.5) * 100 ; Serial.print(temperatureC); Serial.println(" degrees C"); float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0; Serial.print(temperatureF); Serial.println(" degrees F"); delay(1000); }

No comments:
Post a Comment