Archive

Posts Tagged ‘pic’

Using TMP100 temperature sensor with your PIC – PR5

January 31st, 2012 No comments

In this post I will describe how you can connect the TMP100 temperature sensor from TI to your PIC as well as receive temperature data. The sensor I am using is a generous sample from TI. Compared to the DS18S20 that I used before, this sensor is much faster, it takes 40ms to take a measurement with 0.5 degrees accuracy. The communication protocol is I2C. It could be a bit difficult connect it to your circuit since it is using a SOT-23-6 package.

For this project, I will be using my PIC prototyping board which carries a 18F4520. But you can pretty much use the C18 code in this post, with any PIC having a hardware I2C module.

Package problems!

Because SOT-23-6 is so small the first thing I needed was to create an adapter to make TMP100 compatible with my breadboard. This was a perfect excuse for improving my CNC skills :). So I created an adapter in EAGLE, cut it out using the CNC and then solder the sensor and 6 pins. The sensor is now breadboard ready!

The TMP100 adapter

 Connection

The connection is really simple. Add two pull-up resistors to the SDA and SDL lines. I used 10kOhm. The datasheet also suggests an optional capacitor on the supply.

TMP100 pinout

 

TMP100 Schematic

As you can see from the schematic there are two pins called ADD1 and ADD0. Those pins determine the I2C address of the sensor. You can find more details in the datasheet. I connected those two lines to GND therefore the address of the sensor I am using is 0×90. That’s all on the hardware side.

TMP100 – Point Register

I2c is the language this sensor talks :). So we need to use your PICs I2C hardware capabilities. Initialize your communications using

OpenI2C(MASTER,SLEW_OFF);

This sensor has a Point Register (PR)which (guess what!) points to the address of the register you want  to read or write :) Before doing anything else we should point PR to the temperature register. To do that we have to initiate an I2C communication, send the address of the sensor and tell that sensor whether we like to write or read from it. This is done using a single byte. You see, the address is 7-bits starting from the MSB and bit 0 indicates whether is read or write operation (0=Write 1=Read). Let’s do an example. Our sensor’s address is 0×90. We wish to read something from the sensor so the byte should be 0×91. If we wish to write something to the sensors it should be 0×90. That’s it.

Therefore to write to point PR to the temperature register we should issue the following

StartI2C();
WriteI2C(0x90); // Call the sensor with WRITE
WriteI2C(0x00); // Write Temp reg to point register;
StopI2C();

Notice the 0×00? That is the address of the temperature register. The sensor is now ready to give us some temperatures.

TMP100 – Read that temperature

To read the temperature off the sensor, we need to call it using its address and READ (what we said above, remember?) and the sensor will transmit two bytes of data back. We read that using the appropriate I2C commands shown below. We have to acknowledge the reception of the bytes to let TMP100 know that we are ready for the next byte. Temperature is read as follows

StartI2C();
WriteI2C(0x91);
first_byte = ReadI2C();
AckI2C();
second_byte = ReadI2C();
StopI2C();

The temperature is made out of two bytes. If you don’t care about getting any decimals you can just use the first byte. The 4 most significant bits of the second byte contain information on the decimal value of the temperature.

TMP100 – C18 Library

I made a really simple library for tmp100 in C18. Just include the tmp100.h file to your project and make sure you initiate the I2C (the OpenI2C() command shown in this post) before calling any functions.

 

OK that is pretty much. You should be able to get some measurements out of that little sensor. If you need further help take a look at the example below

#pragma config OSC = HSPLL
#pragma config WDT = OFF
#pragma config PWRT = ON
#pragma config LVP = OFF
#pragma config PBADEN = OFF
 
#include <p18cxxx.h>
#include <i2c.h>
#include <usart.h>
#include <delays.h>
#include <p18f4520.h>
#include <stdio.h>
#include "tmp100.h"
 
void main(void) {
 
    unsigned int raw_tmpr;
    char str_tmpr[8];
 
    OpenUSART(USART_TX_INT_OFF &
              USART_RX_INT_OFF &
              USART_ASYNCH_MODE &
              USART_EIGHT_BIT &
              USART_CONT_RX &
              USART_BRGH_HIGH,
              21);
 
    Delay10KTCYx(250);
 
    OpenI2C(MASTER,SLEW_OFF);   // Initialize I2C module
    tm_setconf(0x60);   // Set sensor to full sensitivity
 
    while(1)
    {
        Delay10KTCYx(500);
        raw_tmpr = tm_gettemp();    // Get 2 bytes of temperature
        tm_tostr(raw_tmpr,str_tmpr);    // Convert the temperature to text
        printf("Temperature is %s\r",str_tmpr); // And output through USART
    }
    return;
}

I connected the USART TX/RX line using my CP2102 and used the cutecom program on Ubuntu to read the output on my PC. The screenshot below shows the terminal

The terminal on Ubuntu

Incoming search terms:

  • tmp100 (45)

Serial Interface Between PIC and PC – PR4

October 4th, 2011 2 comments

This one is really useful.

Most of the times you are developing a project, you will need some sort of monitoring or visual output to understand the status of your program. Sure some LEDs blinking are great but some times you need more. This is where the good old serial communication comes in. Through this communication you will be able to send information in the form of data or text, to and from your computer. You can use it as a debugging tool to monitor what is going on in your microcontroller during execution time. Or you can use this communication to send data to the PIC for processing and then receive the result back. I am sure you will appreciate the usefulness of this as soon as you implement it.

And all this happens by sending a sequence of zeros and ones… Read more…

Incoming search terms:

  • cp2102 (76)
  • cp2102 pic (24)
  • program to interface pic and pc (1)
Categories: electronics, projects Tags: , ,

PIC and 5110 Interface with SPI hardware – PR3

September 26th, 2011 11 comments

This is the first “official” project I am doing with my PIC prototyping board. This project is about interfacing and using a simple Graphic LCD. So to follow this project you need a PIC to have SPI hardware, in order to communicate with the device. The GLCD (Nokia 5110) I am using is well known in the community and it is a cheap device. I bought it off ebay for about 5 Euros. Read more…

Incoming search terms:

  • nokia 5110 lcd pic (30)
  • interface pic18f con lcd (13)
  • pic pr3 (2)
  • mikroc nokia 5110 (2)
  • 5110 display (1)
  • pic nokia 5110 lcd projects (1)
  • nokia 5110 pic (1)
  • nokia 5110 display spi protokoll (1)
  • libreria ccs nokia 5110 (1)
  • eroor spi nokia 5110 css (1)
Categories: electronics, projects Tags: , , , ,

PIC Prototyping Platform – PR2

September 7th, 2011 No comments

Hey,

In PR1 project, I talked about creating a stable prototyping platform that would save me from the trouble of setting up the microcontroller and all its required components. However, since then, I worked on tidying up this design into a more usable design (I wanted to play around with creating PCB actually :D ). So I designed the board sent it for fabrication and finally put it together. Read more…

Categories: electronics, projects Tags: , ,

Low Cost PCB Fabrication

August 13th, 2011 2 comments

Hey guys. I am really excited today because my first ever PCBs arrived from the fabrication house! I always wanted to have my own PCB printed but I was deterred by the price. So I used other methods such as developing my own board which can get quite messy or just sticking with the traditional prototyping boards. Few weeks back, I read a post from Kenneth, which led me to this fabrication house. They actually offer 10 PCBs maximum sized 5cm*5cm, for only $9.90 and $4 for delivery! This is really really cheap! Read more…

Incoming search terms:

  • iteadstudio pcb (25)
Categories: electronics Tags: ,

Filtering a Sensor with Noise and Spikes

January 5th, 2011 3 comments

Recently I have been working on a robot that required accurate measurement of distance (more on that robot on a later post). I noticed some unpredictable behavior of the robot and so I investigated further to find out what was wrong with it. It turns out that the Sharp GP2D120 sensors I used are pretty noisy! I didn’t do much research about it before adding it on my project. When I needed to know the distance value, I took 10 consecutive readings and averaged them. But that was not such a clever choice for filtering and you will understand why, in a while.

Let me first explain you my testing platform. First of all we have the robot that has the GP2D120 sensor on. The PIC micro (used a PIC18F4520) on the robot is able to transmit wirelessly using an XBEE module. Data is received on my PC using another XBEE module through USB. Finally, data is analysed in MATLAB. So I position my robot to be stable, so that the distance sensor should read the same value constantly. Each value read, is transmitted and captured on my PC.

Experiment 1 – No filtering

No filtering

Notice that the y axis is the ADC value and not the voltage value directly. As you can see there are some pretty nasty spikes over there! They occur quite ofter as well. Converting those values to distance, spikes reach up to 3cm ! So you have a wrong measurement by 3cm. For a project that requires accuracy that is a nightmare! Read more…

Incoming search terms:

  • median filter (22)
  • spike filter (21)
  • sensor noise filter (20)
  • how to filter spikes sensor (16)
  • median filter adc (13)
  • c code simple spike filter (10)
  • noise filter for sensor (2)
  • building a noise spike filter volt smoothing (1)
  • sprikes in adc (1)
  • noise spike filter (1)
Categories: electronics Tags: , , ,

Simple Serial User Interface on PIC

March 30th, 2010 No comments

So today I needed to control some stuff on my PIC by using the terminal on my PC through RS-232 serial. I want to control the speed on 2 motors connected to the PIC, either separately or both of them together. To do that I divided the tasks that the User Interface (UI) had to provide. So from the main menu the user should be able to choose to control

  • Motor A
  • Motor B
  • Both Motors

In each of the sub-menus shown above the user should have some choices for acting on the motors. Before complicating this stuff more, I will show you the method I used to create this simple menu on my 18F2525 PIC. It doesn’t matter if you don’t have this PIC or even any other microcontroller. The concept is the same.

If you try to tackle the problem an if-then-else structure may seem like an easy solution but I can tell you it is going to messy pretty soon! We need a more clear, robust solution. But first let’s see what we need to do.

PIC User Interface Layout

The image pretty much explains it all. If we are at Main Menu the only valid keys are 1, 2 or 3. Each leads to a new sub menu. From there, there are again three choices 0 that takes us back to Main Menu and + or - that will need to take an action (colored green). So if we implement this menu you will be able to implement more complex ones based on the same concept.

I got this idea based on Finite State Machine concept but its not quite right so we cannot call it like that! Anyway. Each of the menu item (Main Menu, Motor A, Motor B, Both Motors), is a different state in the program let assign them 0,1,2,3 accordingly. So if we are at state 0 we can go to state 1,2 or 3 with the right key pressed in the terminal. However if we are at state 1, we can only go back to state 0 or stay at the same state and complete some actions (forwards, backwards) with the right buttons pressed, once more.

So the way to implement this is by using switch/case. I think it will be much clearer if I show you the code. First let’s see how to we switch between the states. Read more…