Project #1 Ambient temperature sensor + “Wootoff” notifier.

Top view PIC12F683 + LM34 + BS170

Top view PIC12F683 + LM34 + BS170

I made a circuit that reads the output from a LM34 (temperature sensonr) and send the output to the computer using PIC12F683. I had some some spare pins on the PIC12F683 so I decided to use an extra pin to drive a BS170 Mosfet transistor. This transistor then powers the woot-off usb ligts. With a perl script I check if there is a wootoff and if so a signal is send to the PIC and the lights are turned on.

The project started with the curiosity to learn how to use the analog to digital converter inside the PIC12F683. At first it took power from the RS232 serial port connection on the server to power up the entire circuit. I also used bit banging to avoid the need of voltage level converter. This is the original schematic:

Schematics temp lm34

The first home server had a RS232 serial port available.  So I used this PHP code to read the data from the serial code:

                $fd = fopen('/dev/ttyS0', "r");
                if (!$fd) {
                 echo "No serial port";
                else {
                 stream_set_timeout($fd, 2);
                 stream_set_blocking($fd, FALSE );
                 $data_serial = fgets($fd,128);
                 $data_serial = fgets($fd,32);
                 $data_serial = fgets($fd,32);

	         $Ctemp = $data_serial + 5;
      		 $Ftemp = round((((9/5) * $Ctemp ) + 32) , 2);

                 echo '</pre><center><span class="Temp">'.$Ftemp.'°F | '.$Ctemp.'°C</span></center><pre>';


The source code running on the PIC was at first written for the CCS C compiler. Here is the code:

#include <12F683.h> 
#device *=16
#device adc=16
#use delay(clock=8000000)
#use rs232(baud=9600,parity=N,xmit=PIN_A5,rcv=PIN_A4,bits=8,invert)

void main()
   float tempI;
   int i;


     for (i=0;i      tempI+=read_adc();
     tempI= (tempI/10)*(5.0/65280)*100;


The Finished project looked like this:

PIC - RS232 LM34

After a few years I upgraded the server and I had an issue. The new server did not have a serial port. I had to do some redesign. I found online a USB-to-RS232 PCB that did the conversion and the output was +5, GND, RX and TX. I purchased a few of them thinking they may come in handy latter on. In addition the copy of CCS I used was from my old job. I had no access to it anymore, so I also needed to port the source code to something I could use.

I found a C++ compiler from a company called  MikroElektronika.  They have a free demo with some limitations, but the limitations did not affected the functionality of my code. I also was evaluating it, it is a great compiler and their forum was amazing.

As an added feature I had just received from the website a set of yellow lights. They are pretty much useless since all they do is drawn power from the USB port. There is no microcontroller or any smart mechanism inside them. I thought since I have a few extra free pins on the PIC, what if I use one of them to turn the lights on or off when there is a wootoff. Wootoff is an event when the website sells the the extra items they have not sold in the past weeks at a even lower price.  This happens about once every 2 to 3 months, but the only way to find out is by going to the website. I decided wouldn’t be neat to have the server check and let me know without opening the browser. So I combined the 2 ideas in to 1.

Here is the schematics of version 2:

The circuit looks like this:

To read the values from the USB port I used this PHP code:

                $fd = fopen('/dev/ttyUSB1', "r");
                if (!$fd) {
                 echo "No serial port";
                else {
                 stream_set_timeout($fd, 2);
                 stream_set_blocking($fd, FALSE );
                 $data_serial = fgets($fd,8);
                 $data_serial = fgets($fd,8);
                 $data_serial = fgets($fd,8);

                 $Ftemp = ((($data_serial * 5)/1023)- .0488)*100;
                 $Ctemp = round(((($Ftemp - 32)* 5)/9),2);
                 $Ftemp = round((((9/5) * $Ctemp ) + 32) , 2);

                 echo '<center><span class="Temp">'.$Ftemp.'&deg;F | '.$Ctemp.'&deg;C</span></center><br>';


The MikroC code looks like this:

int temp_res, counter = 0;
char temp_text[7], byte_read, error;
short i;

 void interrupt() {
  if (INTCON.T0IF) {
    if (counter >= 20) {
      counter = 0;              // reset counter
      counter++;                // increment counter
    INTCON.T0IF = 0;            // Clear Timer0 overflow interrupt flag

void main(){
  OSCCON = 0b01110001; /* set internal clock to 8 MHZ */
  OSCTUNE = 0x1F;
  ANSEL = 0x01;
  TRISIO = 0x01;
  CMCON0 = 0x07;
  OPTION_REG = 0x04;
  INTCON.GIE = 1;               // Global interrupt enable
  INTCON.T0IE = 1;              // Enable Timer0 overflow interrupt

  Soft_UART_Init(&GPIO, 5, 4, 9600, 0);

  while(1) {                    // Endless loop
      temp_res = ADC_Read(0);   // Get 10-bit results of AD conversion
      if  (byte_read == '1')
       GPIO.B1 = 1;
      else if (byte_read == '0')
       GPIO.B1 = 0;
      for (i=0;i<7;i++){
        if (temp_text[i]!=' ')

As you can see on this code I used interrupts on the 12F683. When it receive a ‘1’ from the serial port it turns on the GPIO.B1, and when it receives a ‘0’, it turn off this GPIO.

I put the *.hex file that you can use to burn the PIC, complete source code and more pictures with documentation of the datasheet of the components used here:

If you have any questions or comments, please leave it bellow.


1 Comment

Filed under Electronic Projects

One response to “Project #1 Ambient temperature sensor + “Wootoff” notifier.

  1. jesse dziedzic

    I dont disagree with this writing…

Leave a Reply

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

You are commenting using your 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