(go to the article link to find files for all of the different
code listings):
❏ You should get results that appear somewhat like those
in Figure 10.
// A101_ch10_light_sensor_voltage 7/2/14
// Joe Pardue
int sensorPin = A0; // analog input pin
int sensorValue = 0; // store the analog input
Lab 2: Converting ADC Sensor
Readings to Temperature
void setup() {
Serial.begin(57600);
Serial.println(“Measure light sensor voltage
rev 1.0”);
}
Converting Arduino ADC readings to voltage and then
converting those voltages to temperature readings is
somewhat complicated. To reinforce our earlier discussion,
we will write a program to create a series of fake ADC
readings and verify that the techniques we discussed work
as described.
void loop() {
if(Serial.available())
{
char c = Serial.read();
if(c == ‘r’)
{
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
Parts required:
1 Arduino
1 USB cable
Estimated time for this lab: 30 minutes
Serial.print(“LDR voltage: “);
Serial.print(sensorValue);
Serial.print(“ Voltage: “);
Check off when complete:
❏ Load the following program into the Arduino IDE:
Serial.println(((5.0*(float)sensorValue)/1024.0),
3);
}
}
}
// A101_ch10_volts_to_temperature_test 8/14/14
❏ Notice that this program is identical to the
#define VOLTS_PER_ADC_STEP 5.0/1024.0
#define VOLTS_PER_DEGREE 0.01
#define temp_BASE -40.0
#define LOW_ADC 20.49
#define HIGH_ADC 358.61
#define LOW_VOLTAGE 0.5
A101_ch7_pot_voltage.ino program from Chapter 7,
except that some comments are changed. This
powerfully illustrates the value of code reuse.
❏ Load the program onto your Arduino and then run the
Arduino serial monitor.
❏ Repeat the earlier experiment where you cover the
sensor with your hand and then move it roughly two
inches up for each subsequent measurement.
int tempSensorPin = A0; // analog input pin
float tempSensorADC = 0.0; // variable to for
// the ADC reading
float tempertureSensorVoltage = 0.0;
// variable for the converted voltage
float tempSensorVoltage = 0.0;
// variable for the sensor voltage
float tempCelsius = 0.0;
// variable for degrees Celsius
■ FIGURE 10:
Measuring light sensor voltage.
■ FIGURE 9: CdS sensor circuit photo.
November 2014 63