Sensing the world

Our Launchpad computer has the ability to sense when things change in its environment. Simple changes, such as someone pressing on the on-board switch, we have already discussed to some extent. But the Launchpad can also sense voltages on several of its pins, and it can use its timers to detect the width of HIGH or LOW pulses on an input pin. Using these capabilities, we can detect changes in light levels, temperature, distance to objects, pressure, sound, and many other things going on in the nearby environment.

Measuring temperature requires nothing but the Launchpad itself. It has an internal temperature sensor. At the factory, they calibrate this sensor using by recording its output at two different temperatures -- 30 degrees Celsius, and 85 degrees Celsius. They them write these to values into the memory on the chip. When we read the sensor, we can use the known values at those two temperatures to convert the reading into degrees.

The code is too long to show as a single image, but you can click on this link (Calibrated thermometer) to see it.

The Launchpad has an internal analog-to-digital converter. It can convert a voltage into a number the computer can use. The analog-to-digital converter (ADC for short) needs to compare the voltage to a known reference value. We can choose to use the voltage that runs the chip, but that might not be well regulated (we can run the chip from a battery, whose voltage drops as it weakens). But the Launchpad chip also contains a 1.5 volt reference voltage that is quite stable and accurate. So we will use that. Our setup and loop functions look like this:

In setup(), we use the function analogReference() to tell the chip to use the internal 1.5 volt reference. Then we use analogRead( TEMPSENSOR ) to read the temperature. We throw away the first reading, since it is generally unreliable.

In the loop() function, we use our own function temperature() to read the temperature sensor many times and average the results, and then convert the average temperature into degrees, using the factory calibration values. That function looks like this:

For those who would rather see the temperature in Celsius, I left that line in as a comment.

In the program above I use the Serial class to send text back to the computer that we use to program the Launchpad. In order for this to work, you need to change the jumpers (labeled J3) on the Launchpad board from their default "software UART" positions to the "hardware UART" positions. This is quite easy, as there is a picture printed on the board to remind you which way to set the jumpers. Pull out the two left-most jumpers, turn them 90 degrees, and put them back, as shown in the photo below:

A UART is the Universal Asynchronous Receiver Transmitter. That is geek for serial port -- a way for computers to talk to one another.

Once you have set the jumpers, and downloaded the program to the Launchpad, you can bring up the Serial Monitor window (using the Tools Menu, Serial Monitor menu item, or just holding down the Control and Shift keys while you type M for Monitor). That window will show the text the Launchpad is printing.

However, in a sculpture, you might use a servo motor to move an arm to indicate the temperature outside ("the temperature is this high...). Or you might have  the computer play a different tune depending on the temperature. Or maybe say something about the weather. Or just indicate the temperature using lights or an LCD display.


Calibrated Thermometer

Passive Infrared Motion Detector

Light level detector using a cadmium sulfide photoresistor