Sensing temperature

Sensing temperature seems to be an easy enough task using Arduino etc. You take a thermistor that changes resistance with temperature, such as a 10K ohm thermistor, which has a nominal resistance of 10K ohm at 25DegC. You take a fixed-value resistor, say 10K ohm, and make a voltage divider with the thermistor. You use analog input to sense the voltage of the divider and calculate the resistance from the voltage, then temperature from resistance. You can call this fixed resistor a serial resistor since it is in series with the thermistor. The following diagram is a voltage divider. The R_known is the fixed-value resistor. The R_unknown is the thermistor. A 5V voltage is applied to the resistors and a “sense” pin is sensing the divider voltage.

voltage divider

Then why am I wasting time to reiterate this simple task? Consider, the fixed-value resistor is made of a carbon film. What does carbon do at different temperatures? It changes resistance! So the “fixed-value” resistor you have is no longer fixed in value, especially if you live in places with temperature extrema, like where I live. I can get below 30DegC and above 30DegC outdoors in winters and summers. When you consider temperature, you can easily span almost 60DegC temperature range from a 25DegC nominal room temperature, where the fixed resistance values are specified, to a -35DegC where your data logger is logging data. If your fixed-value resistor doesn’t come with a temperature coefficient, well, it should! You can assume maybe 200ppm/DegC. This means 200 parts per million change of resistance per degree C. With -35DegC at 60 DegC below nominal temperature, you are looking at:

200*60/1,000,000=0.012=1.2% resistance change

This change of resistance could result in several percent of temperature reading error. The exact relation requires some calculus maybe I can discuss if there is interest. So you can expect to have several DegC or more error.

Here is what I noticed by driving two cars:

They both exhibit this behavior: After I park the car outside for several hours in cold temperature, I start the car, the “outside temperature display” says it is something like 10 degF. After several minutes, the temperature starts to drop to lower values.

Car 1: there is a few DegF drop. Say it will say 10DegF at startup, then it will say 7DegF after a few minutes

Car 2: there is as much as 15 DegF drop. If is says 10DegF at startup, it may say -5DegF after a few minutes.

Car 2 is newer than Car 1 but both cars are newer cars.

So my thought, although not tested (that might require disassembling the car computer), is that, the newer car is using a lower-grade serial resistor with the temperature sensor (thermistor) than the older car. This serial resistor must be inside the cabin, on the car computer’s board. When it is cold, its value increases considerably to maybe a couple percent higher than the nominal value. This trend of increasing resistance with decreasing temperature is shared between carbon and semiconductor (thermistor). So the effect of thermistor increasing resistance with decreasing temperature is countered by the effect of carbon increasing resistance with decreasing temperature. Then once the engine warms up the cabin enough, the fixed resistor warms up and the temperature display changes.

So how do you counter this effect i.e. temperature coefficient of fixed resistor? You can buy better resistors with less temperature coefficient, such as 20ppm instead of 200ppm. You will drop that effect to 1/10, which will be able to provide you the right accuracy. But these resistors aren’t exactly cheap. For instance, the 10K resistor I used as serial resistor in one of my designs has 10ppm/DegC:

279-RN73CA-10K

This resistor is about a dollar each. It is not only low in temperature coefficient, but also high in precision, 0.1% (i.e. it is at most 0.1% off from 10K when measured at nominal temperature)

 

When I don’t need this precision, just need a ~10K pull-up resistor for reset pin, I use this one:

652-CRT0805FZ1002ELF

This resistor is only 20 cents but it’s not too bad. 1% precision and 50ppm/DegC. I suspect the car 2 has something like this or even a bit worse! A 5% precision 200ppm resistor is only 10 cents. What would I use if I needed to make lots of cars?!

Debouncing a mechanical rotary encoder

As an active member of the Arduino user community, I felt I should keep up with my library updates. So after some planning and “careful” coding, I’ve added a new digital rotary encoder class to my phi_interfaces library. The phi_interfaces library includes buttons, rotary encoders, analog buttons, joysticks, matrix keypads, serial ports, etc. as input devices and one can use getKey() to sense any of them.

http://liudresllc.com/libraries/phi_interfaces/

The original rotary encoder class already works well. This addition expands the scope, supporting both normally open and normally closed encoders, and also supporting encoders that have twice the detents as they have complete pulses (have some hardware but haven’t tested yet). Also, this addition paves the way of an analog encoder class that will help people using nano/ATMEGA328P-AU save two pins, which has two analog-only pins with no digital functions.

Along the way I met some difficulties that I thought was useful to share. Although it stems from library development, the nature of the problem is actually misunderstanding of hardware.

Hardware I used: Bourns PEC11 series 18 pulses/rev. encoder with 18 detents./rev. (no obsolete, replaced by PEC11R)

So here is what I discovered: the mechanical rotary encoder that I have has a delay between channel A closing and channel B closing about 14 ms, then delay between channel A opening and channel B opening about 8 ms. Not only is opening asymmetric with closing, but they are also faster than my expectation. I started programming the new class expecting to debounce the encoder. I first used 20ms for debouncing, like what I do for tactile switches. That didn’t yield good results. I thought I need longer time, 50 ms, not good, or about the same, only 1/3 detents got detected. I then tried shorter debouncing like 10 ms and still few detents got detected. Running the original rotary encoder code I developed initially, without debouncing, was find, with every detent registered. So I checked the encoder with a logic analyzer. Wow the action was faster than I expected, 14ms and 8 ms. I also looked at the bouncing of the contacts. It was around 0.5 ms after initial contact was made, many bouncing occurred. So if I’m using millis() to debounce, I’d rather not debounce at all.

Here is one part of the pulse when channel A closes then channel B closes, about 14 ms:

Here is another part of the pulse when channel A opens then channel B closes, about 8 ms:

Here is a typical bouncing of the contacts, in the order of 0.5 ms:

An afterthought, I read the spec sheet, which says at 15RPM, close to how fast I was turning the shaft, bouncing is 5ms max. I’ve seen bouncing around that length in time. If I read the spec sheet, I’ve probably avoided the whole learning experience smiley

Here is diagram showing symmetric pulses on the spec sheet:

So, take the spec sheet with a grain of salt.

BTW, I’ve recently designed a system that uses one analog pin to sense a mechanical rotary encoder. The purpose was to save pins. The ATMEGA328P-AU has two analog pins that have no digital functions. Why wasting them if I can hook a rotary encoder to one of them?

How to use a breadboard

Introduction:

This is a basic skill for anyone trying to tinker with electronics yet everyone starting off can use some general guidance on how to use a breadboard.

A breadboard, or a solderless breadboard, is a platform where you lay out your circuit, similar to a piece of paper where you can draw the circuit. You will be able to put components down on the breadboard and interconnect them to form your circuit. It’s important to first draw up a connection diagram on a piece of paper so you can use it as guide to connect up components on your breadboard, especially for complex circuits, or you are just starting with electronics.

How does a breadboard work?

So how does a breadboard work? Simple. If you draw up a circuit, you notice these junctions that connect up several wire ends, right? If you build this circuit, you will use something to hold these wire ends, possibly with a metal paper clip so you get good contact and they don’t fall apart, right? That’s exactly how a breadboard works. There are tiny metal clips that can hold on to your wires so you can make these junctions. Each clip has 5 holes above it so you can connect up to 5 wire ends by inserting them into these 5 holes. Take a look at the following pictures:

On the first picture you will see a miniature breadboard. It has a groove in the middle, separating its two halves. You will notice each half has 5 columns of holes. Take at a look the back side of the breadboard on the next image in the gallery. I removed the protective backing and walla! You can see these metal clips behind the holes. Each clip is behind a row of 5 holes. See the third picture. So now you know that each row of holes should represent a junction that can connect up to 5 wire ends, using the metal clip below these holes. In the fourth picture I was holding one clip in my hand. You can see the clip is cut into 5 sets of separate prongs. This allows the clip to grab on to 5 wire ends of different diameters and have good grip on them all instead of only holding the thickest wire and letting loose the rest.

Now that you know how a breadboard works, it’s time to learn how to use a breadboard. Before moving on, take a look at the last image. It’s a project I built on a breadboard. There are 8 individual LEDs that will light up and flash different patterns and programmable all via a remote control. Pretty fancy?

Enjoy a video before moving on so you know if you can use a breadboard, you can do something just as fancy:

How to use a breadboard?

To use a breadboard, you first need a circuit to build on it. Let’s build something useful. In this previous post, I described how to use a thermistor to measure temperature:

http://liudresllc.com/2011/02/10/measure-temperature/

There is a circuit in the post:

voltage divider

This is a voltage divider. You have a known resistor (10Kohm), and an unknown resistor (the thermistor, which changes resistance with temperature). You also have 5V and ground, besides an analog sensing pin from Arduino.

To build this circuit on a breadboard, we realize that there are 3 junctions:

  • The junction above R_known, with 5V and R_known coming together
  • The junction between R_known and R_unknown, with both resistors and arduino pin coming together
  • The junction between R_unknown and GND, with R_unknown and GND coming together

To build the circuit, we need to use three rows of holes to do these three junctions. Here is the finished circuit:

Thermistor_bb

Well, you noticed the image of the finished circuit is not an actual breadboard. To be honest, I don’t have a breadboard at my desk and photographing real breadboards often gives you the wrong perception of which wire goes to which header due to the different depth between the jumper wire end and the header. I have helped enough people with their circuit by viewing their breadboard photos. This common problem is seen on nearly every overhead photo. Another nice thing about this photo generated in Fritzing is that the rows of 5 holes are highlighted with green so you know these 5 holes are interconnected. They even do the resistor color coding right.

If you have a regular power supply, such as a student +-25V power supply, you should wire the power from the power supply to the banana sockets on a full-size breadboard and wire the power from there to the power buses, which are the columns on the outside with red and blue lines. All the holes along the same red line are connected. Different red columns are not connected. Once you have these convenient buses, you can just derive power from the buses instead of wiring multiple wires from the power supply. Take a look at the following images for a more complete understanding of what are connected together and how the power buses are used in an actual circuit:

Now you probably noticed that there is an integrated circuit (IC) in that last picture. Take another look at it just to remember how it is put onto the breadboard. Aha! That’s why there are these grooves! With an IC, you put it on the breadboard so that it sits right above the groove. This way you will have access to all its pins instead of shorting their pins if you push it onto the rows of 5 connected pins.

For more complicated circuits, you will need to plan ahead on how to lay it on the breadboard, try to spread out instead of squeezing at a corner.

Now take a look at this picture again just to have a better understanding on how the breadboard is used and you’re ready for the road!

breadboard01_32

Minimal equipment for Arduino newbies

As a long-time member of the Arduino forum, I often find it frustrating to help some newbies get their feet wet with DIY electronics. Partially it is because that the newbies don’t have the basic tools and parts to proceed with simple projects. So I thought to myself, after a few members’ discussion, that I should put together a list of minimal equipment for an Arduino newbie to start their journey with Arduino so when they need help, they have basic parts and tools to get the help. Here is a list:

  1. Arduino UNO with USB cable – Highly recommend newbies to purchase genuine Arduino UNO to avoid potential problems of a clone
  2. One small breadboard at least 30 rows prefer with buses – sooner or later you will need to set up a circuit on this breadboard, not everything can be simply plugged into Arduino
  3. One bag of random resistors a few small tactile push buttons and LEDs – you should have some 10Kohm, 300 ohm, 150 ohm, etc for various circuits
  4. One set of jumpers, at least include 5 colors and 30 pieces – this ensures when you are seeking help, the circuit is connected not with all same color wires, a horrible scene for any helper
  5. One simple multimeter – This ensures if you are instructed to check voltage or continuity you can do so with it
  6. One or two 10K potentiometers that can be plugged into breadboards – This ensures you can make some voltage divider to play with analog inputs in absence of any actual sensors and to hook up an LCD contrast circuit

A lot of newbies ask what kits to buy but the above should get anyone a good starting point. Your future project relies on your own interest so you can pick your own sensors or actuators when you have a project in mind.

As I was finishing this post I searched some well-known Arduino distributors and the following from Adafruit doesn’t look too bad for beginners:

Starter kit:

http://www.adafruit.com/products/193

Adafruit budget kit

 

Multimeter:

https://www.adafruit.com/products/71

Adafruit basic multimeter

I just asked Adafruit for permission to use their pictures on my blog and they replied within 10 second with a yes 🙂

Sourcing and sinking current

Occasionally you see terms such as sourcing and sinking current and you keep wondering what they mean. So to make this clear, let’s look at sourcing and sinking current to light up LEDs:

Say you hook up an LED with a serial resistor and between an arduino output pin and gnd. If you direct the output to HIGH, you put 5V over the LED and resistor so current flows from the arduino pin to ground through the LED and resistor. In this case, you are sourcing current from the output, meaning flowing current from the pin through the LED, like turning a faucet on and flowing water out of the faucet. If you direct the output to LOW, the potential on both sides of the LED and resistor is zero so no current flows.

Say now you connect the LED and resistor between an arduino output pin and 5V. When you set the output pin to gnd, you are sinking current to the output, meaning flowing current from 5V to the output pin through the LED and resistor. It is like the arduino pin is a sink in a kitchen. Your 5V source will flow water via the LED and resistor to the sink.

Arduino pins can both source and sink when they are outputting while some other output devices such as a TLC5940, can only sink current, while others can only source current. In case only one-way conduction is allowed, you need to set up the circuit correctly to source or sink otherwise the circuit will not work or get damaged.

How to calculate current-limiting resistor for LEDs

Here is typically how you calculate the “current-limiting resistor” for an LED.

You will need to know ahead of time:

1) Supply voltage Vdd, in case of the typical 5V arduino boards, Vdd=5V

2) Typical forward bias voltage of the LED Vfb, read the spec sheet. My personal experience is that more powerful LEDs have higher value of Vfb.
Take this one for example:

http://dipmicro.com/store/LED5G

A range of 3.0 to 3.6V is given. I would try to use the average of 3.3V. Some other spec sheets give typical values and you will use that value.

3) Current rating i_m. The above LED is said to have 20mA current rating. It’s the maximal sustained current you want it to have. Again some other spec sheets give you current at typical forward bias voltage and you can use that value.

Now to calculate the current-limiting resistor, just do this:

Vdd=Vfb+Vr, where Vr is the voltage dropped on the current-limiting resistor. So Vr=i_m*R. Where i_m is the LED current rating.

So R=(Vdd-Vfb)/i_m

For the above LED, R=(5V-3.3V)/0.02A=85 ohm. That is when you are pushing a little bit. Plus, arduino pins can only supply 20mA and the entire ATMEGA chip can supply 200mA. So to be safe, I would use a 150 ohm resistor. Then the LED will be less bright. If you use the LED for lighting purpose, its brightness is relevant to you. But you use it as an indicator instead of illumination, make it not that bright by using larger resistors.

Typically, if an LED is passing 1mA of current, you can see it in the dark, or straight on in a room but hard to see from sideways. If you run 20mA on an LED, it hurts your eyes to stare at it.

The following is taken in a dark bedroom. I believe only 3 LEDs were on together, passing around 6mA each only 🙂