The Make It Last Build Series Newsletter, Build #1, Dispatch #3
Hi and welcome to the third newsletter for our Make It Last Build Series. We're midway through our first project, building a data logger. In this dispatch, we're going to discuss using sensors in battery-powered applications. By now, you should have a basic system up and running. If you haven't already, check out the first two newsletters to get up to speed. Now, let's get sensing!
Also: Don't forget that we have a sweet offer from Microchip for 20% off select products (see the list at the bottom of this newsletter). To get your 20% off discount code, send email to firstname.lastname@example.org.
Matt and Gareth
back to top
Sensing the World
According to Wikipedia
, a sensor is a device that measures a physical quantity and converts it into a signal that can be read by an observer or an instrument. For it be useful to use with a microcontroller, however, a sensor has to convert the physical quantity into an electrical signal. There are two classes of signal it can put out -- analog or digital. Sensors that put out an analog signal work by outputting a voltage* that varies proportionally with the thing being sensed. The microcontroller then uses an analog-to-digital (ADC) circuit to convert the value of the analog signal into a digital one.
Sensors that put out a digital signal are becoming more and more common. The simplest ones just transmit an on/off condition. For example, you can think of a switch as a device that senses whether it is turned on or not. When it is on, it transmits a "high" logic signal, and when off, a "low" signal. The microcontroller uses a digital logic input to read in this value, and the program can react accordingly. More advanced digital sensors use a serial
or parallel interface to transmit multiple bits of information at a time. The nice thing about using sensors like this is that they take care of all the analog dirty work, but the disadvantage is that they can be more expensive than their analog companions, and sometimes harder to find.
* There are other ways analog sensors can create outputs (curent, pulse width modulation, etc), but we'll ignore these for now.
back to top
Using Other Programmers
Already into PIC programming, and have something other than a PICkit 3 programmer on hand?
In the MAKE Forum, GigaMegaWatts raised a question about using a PICkit 2 programmer to complete the build. The programmer itself supports the 18lf25k22 chip we are using, however, the example program is missing some of the configuration bits necessary to work with this programmer. Check out the Forum discussion for an ongoing discussion on this topic.
back to top
Building a Digital Thermometer
Starting with last week's Hello, world project, this time around we will add a temperature sensor and serial port. First, lets gather the new parts:
- Completed 'Hello, world' project
- FTDI cable or breakout board (note: you can use 3.3v or 5v)
- Six-pin .1" header, modified for use on a breadboard
- MCP9700 temperature sensor
- 10k resistor (if you're using a 5v FTDI cable)
First, place the temperature sensor IC on the board. Leave some space between it and the processor, so that we can add an external memory chip later. Connect Pin 1 on the sensor to power, and Pin 3 to ground (click the photo for a higher-res version). Next, place the header for the serial port on the opposite side of the breadboard from the sensor. Connect the first pin (closest to the processor) to ground.
Run a wire from Pin 13 on the processor to Pin 2 on the temperature sensor. The temperature sensor we are using puts out an analog voltage, and we can use We'll use that microcontroller pin (Port C, Pin 2) as the analog input from the temperature sensor. Place another wire between Pin 17 on the microcontroller and Pin 5 on the serial port header. Note: If you are using a 5v FTDI cable, place a resistor in series with this wire to protect the microcontroller.
That's it for the hardware portion -- the final step is to load the new software. Grab the project here
, load it in MPLAB, look it over, then upload it to your microcontroller. The program measures the output voltage of the temperature sensor every five seconds, and prints the result to the serial port. In order to see the results, you'll need to plug in your FTDI cable, and configure your serial terminal program (I used HyperTerminal) to connect at 9600 baud. You should see a new number appear every five seconds or so. To test if it's working, cover the temperature sensor with your hands. If the value goes up slightly, you're good to go! The next step is to calibrate the sensor, so that the micrcontroller can convert those raw values into temperature readings we can understand.
back to top
Calibrating the Sensor
The final step to using our temperature sensor is to calibrate it. Up till now, we've been reading measurements in ADC counts, which translate into voltage. To calibrate it, we'll need to make measurements from at least two known temperatures, then use those points to convert ADC counts into temperature values.
The easiest way to generate two known temperatures is to use water -- at freezing, it should be 0 degrees C, and at boiling, it will be 100 degrees C (assuming you are near sea level). It probably wouldn't be a good idea to stick your whole breadboard into boiling water, so the first step is to make an extension cord for the temperature sensor. You can use three alligator clips for the job, taking care not to let any of the clips touch while handling them.
Make the first measurement by boiling some water in a pot, and carefully lowering the probe until the sensor is submerged. Using the temerature sensor program, I got a measurement of 738 counts at 100 degrees C.
To measure freezing, put some very cold water in a bowl with ice cubes, then stick the probe in. This gave me a reading of 255 counts.
Finally, to make use of these readings, we'll need to extend our temperature sensor program to use a map function. There is a nice example on the Arduino website, so we can just grab that. Then, we need to add in an extra line to our program to make use of it:
temperature = map(temperature, 255, 738, 0, 100);
and we have a calibrated electronic thermometer!
back to top
Microchip Discount Code
Want to pick up a PIC programmer or development kit? As part of the contest, Microchip is offering a 20% discount on the following development tools:
PICkit3 Debug Express
ICD3 In-Circuit Debugger
XLP 16-bit Development Board
F1 Evaluation Platform
F1 Evaluation Kit
If you've been thinking about getting started with PIC programming, this could be a great opportunity to get a good deal on a programmer. To request a discount code, send an email to email@example.com.
back to top