LM335 – Measuring Temperature

July 12, 2012 Leave a comment

I recently ordered a whole bunch of components, sensors and parts off of Mouser, one of which was the LM335A temperature sensor.

The LM335A is part of the LM135 series, which are precision, easily-calibrated, integrated circuit temperature sensors. It operates as a 2-terminal zener, and has a breakdown voltage directly proportional to absolute temperature at +10 mV/°K. With less than 1 Ohm dynamic impedance the device operates over a current range of 400 mA to 5 mA with virtually no change in performance. When calibrated at 25°C the LM135 has typically less than 1°C error over a 100°C temperature range. Unlike other sensors the LM135 has a linear output.

All this and more information about the sensor can be acquired in the datasheet here.

The important parts to know are that it works like a zener diode with a breakdown voltage that is proportional to the temperature. A 10 mV increase correlated to a 1°K increase in temperature. Calibration of the sensor can be done via a potentiometer connecting output to ground. At this time I will be using the sensor uncalibrated, which will give a temperature error of +/- 1 – 3°C, I may attempt to calibrate it at a later date using the known temperatures of boiling/freezing water.




  • 2.2k Ohm resistor
  • LM335A

To use the sensor I set up this circuit on my protoshield:

5V is supplied to the (+) pin of the sensor through a 2.2k resistor, and GND is connected to the (-) pin. Analog 0 of the Arduino is also connected to the (+) pin.  This forms a circuit known as a voltage divider, and is a linear circuit that produces an output voltage that is a fraction of the input voltage. In my case, the voltage out changes as the break down voltage changes in the sensor, and is representative of the temperature.

Note: To calculate the resistor used in this circuit you would have to take into consideration the voltage drop of the sensor, which I don’t quite understand at this point so I will be saving this comment from SparkFun user “superbrad” for later use:

I believe the issue that most of you are having is that you are neglecting to take into account the ~3V drop of the LM335 into account. It is a diode, which needs to be included in the resistor calculation.
Ohm’s law: V = IR. But if you’re running on 5V, don’t use V = 5V! The diode blocks ~3V, so V = (5V – 3V) = 2V.
Solving for R where V = 2V and I = 1mA (R = V/I = 2V/.001A), you should find that a 2Kohm resistor at 5V is appropriate.
Best of luck.

Another thing to keep in mind is that since we are working with such a low voltage signal, it is important to keep the sensor close to the Arduino or the signal will be degraded and inaccurate. So I wouldn’t advise stringing up the sensor on a 20″ wire cable unless you use some kind of shielding or filtering/amplification like that which is demonstrated in the datasheet.


To get the sensor to start giving us data we tell the Arduino to do this:

int sensePin = 14; // Pin A0
float sensorValue = 0; //Set the sensor value as a floating number
void setup()
 pinMode(sensePin, INPUT);
void loop() 
 int sensorValue = analogRead(sensePin); //reads voltage on Pin A0
 Serial.println(sensorValue); //returns voltage on a 0 - 1023 scale (0 - 5 Volts)

And we get a response in the serial logger of something like this:

Now, this isn’t quite useful to us as it isn’t in the °C or °F temperature format we are used to so we have to convert what the sensor is spitting out to a human readable number. First thing we can do is convert the number to Kelvin, as we know that every 10 mV change in the output is equivalent to 1°K

float kelvinValue = (((sensorValue / 1023.0) * 5.0) * 100.0); // convert sensorValue to Kelvin

This is better, but we aren’t doing any science (yet) so we don’t want Kelvin, we want Celsius or Fahrenheit.

float celsiusValue = kelvinValue - 273.0; // convert Kelvin to Celsius
float fahrenheitValue = (celsiusValue) * (9.0/5.0) + 32.0; // convert Celsius to Fahrenheit

Now we can finally have the Arduino return the temperature of its environment in Celsius or Fahrenheit! Wooohoo! Here are the results with a little added formatting flair:

Click here to go to the completed sketch with formatting on pastebin. (updated Jul-14-2012)

Now that we have the ability to roughly (since our sensor is still uncalibrated) sense the temperature of the environment we can make all kinds of things! Computer fan controllers, green house temperature monitoring, self-regulating homebrewing fridges are projects that we are ready to take on.


June 28, 2012 Leave a comment

Capacitors are a device used to store an electric charge, consisting of one or more pairs of conductors separated by an insulator. Though they function somewhat similarly to batteries, the main difference is that they are able to charge and discharge very quickly and they can only hold a charge and do not produce it.

To better understand capacitors I built a simple circuit based on the simulation found on falstad.

The circuit is split in two for the charge/discharge phases by a SPDT switch. A battery is hooked up to the capacitor with a resistor, and once the capacitor is charged the switch can be thrown to drive the current to light the LED.

When a capacitor is connected to the battery two things happen:
The plate on the capacitor that attaches to the negative terminal of the battery accepts electrons that the battery is producing.
The plate on the capacitor that attaches to the positive terminal of the battery loses electrons to the battery.
Once it’s charged, the capacitor has the same voltage as the battery (a 9 volt battery will give you 9 volts on the capacitor).

And yes, using a larger capacitor will result brighter and longer lit LED. This capacitor is 5600 μF:

It’s important to note that while the capacitor is charged one direction, it is discharged the opposite direction therefore the LED must be positive side towards the discharging end of the capacitor. This puzzled me the first time I set up the circuit and it didn’t work, but it makes sense if you visualize a capacitor as a pipe with a rubber sheet dividing it in two lengthwise.

While working with large capacitors it is important to discharge them prior to handling, this can be done by shorting both pins. In attempting to discharge a capacitor it is not advisable to use your finger, even though my curious attempts to do so were unsuccessful, varying skin moisture and conductance may cause different results.

More information can be found at:

HowStuffWorks: “How Capacitors Work”

Wiki: Electrolytic capacitor

And if you don’t know, now you know.

January 18, 2012 Leave a comment

I’ve got to put my projects on the back-burner as school starts up again, but I’ll be trying to keep in touch with electronics and the Arduino as best as I can. I’ll probably get sucked into mini projects every now and then anyway since I constantly bombard myself with sources of intriguing information relating to electronics.

So that’s what this post will be about, spilling the beans on my current sources of stories and general info on electronics and the Arduino. I’ll try to organize this list by what I frequent the most.


Reddit’s electronics related subreddits combined into one link. All of those constantly provide me with new ideas and interesting articles to read, and I see people submit questions that I have all the time. /r/Breadboard and /r/Letslearnelectronics also merit a look.

EEVblog – eevblog.com

“An off-the-cuff video blog for electronics engineers, hobbyists, hackers and makers.” This guy puts out some really entertaining and educational videos, some of the topics may be a bit advanced for the beginner but it gets you thinking in that electronics mindset.

Ladyada / Adafruit Industries Tutorials

Some great tutorials by Ladyada on the Arduino, electronic components, projects, and programming.

Arduino.cc Tutorials and Arduino.cc Reference

Nothing better than having your source of info be right from the creators of the Arduino.

bildr – bildr.org

“Documented methods for doing one thing, and offering them for as many microcontrollers as possible.” Puts out beautifully done tutorials.

falstad Electronic Circuit Simulator

Simple circuit simulator. You can add wires and various components to try out some basic ideas before you build them.

kpsec The Electronics Club

Very nice site for easy to digest explanations of basic electronic principles, various components and parts, and sample projects and prototyping.

Others worth mentioning:





SparkFun Free Day is Today

January 11, 2012 Leave a comment

Who doesn’t like free stuff?

Basically, it’s just a server stress test for SparkFun Read more…

Categories: Other Tags: , , , , ,

for Loop

January 11, 2012 Leave a comment

I’m going to do a quick run though the “for” loop, using a part of this guide as a foundation.

Using the Arduino.cc explanation here you can get a basic understanding of how it works.

The for statement is used to repeat a block of statements enclosed in curly braces. An increment counter is usually used to increment and terminate the loop. The for statement is useful for any repetitive operation, and is often used in combination with arrays to operate on collections of data/pins.

There are three parts to the for loop header:

for (initialization; condition; increment) {
The initialization happens first and exactly once. Each time through the loop, the condition is tested; if it’s true, the statement block, and the increment is executed, then the condition is tested again. When the condition becomes false, the loop ends.

So now that you have a basic understanding of the for loop, lets see how we can apply it…

I will use my own version of the original example sketch from tronixstuff to illustrate how the for loop functions.

Click here for Pastebin with syntax highlighting for the following code.

void setup(){
    pinMode(9, OUTPUT);
void loop(){
    for (int x = 1; x <= 5; x++){
        digitalWrite(9, HIGH);
        digitalWrite(9, LOW);

Here’s whats actually happening in the for loop in human language:

    for (set x equal to 1; is x less than or equal to 5?; increase x by 1){
        set pin 9 high;
        wait one second;
        set pin 9 low;
        wait one second;
    wait 10 seconds;

The steps taken in our loop are;

  1. a variable “x” is set to equal to 1, this is done once
  2. a test is done to check if x is equal to or less than 5
  3. if the test is passed, the statements inside the braces are acted upon
  4. x is set to equal x + 1
  5. steps 2 through 4 are done until the test fails, in which case the loop is terminated

The physical result of the sketch – if you have an LED hooked up to pin 9 of the Arduino – will be that the LED will blink 5 times, wait 10 seconds, blink 5 times… and so on, for infinity.

That’s pretty much it. You can do more complicated things with the for loop, but the fundamentals can be summarized to: initialize, test, incrament/decrement.

if Test

January 9, 2012 Leave a comment

The “if” is a test you give to the Arduino. In this test you make the questions and you determine what happens when it passes or fails. If the test that you made is passed, as in something happens that you wanted to happen, then you want the Arduino to do a certain action. Optionally, if the test is failed or something unexpected happens and the test is failed, you can tell the Arduino to take a different action.

If you wanted to make a test on the Arduino that would turn on a light for 5 seconds every time someone walked through a door it would look something like this:

if (PersonWalkingThroughDoor == 1) {
 digitalWrite(LIGHT, HIGH);
 digitalWrite(LIGHT, LOW);

And a translated human readable version to understand whats going on:

if (a person walks through the door) {
 turn on the light;
 wait 5 seconds;
 turn off the light;

Now, let’s try to understand the Arduino version of the test and see why it works…

A person can only be walking through the door or not, we can represent this as 1 or 0, respectively. Then, we can assign this state to a certain variable – which can be thought of as a digital box that can hold thing – we’ll call this variable “PersonWalkingThroughDoor”.

If a person is walking though a door our variable PersonWalkingThroughDoor would equal 1, otherwise it would be 0. Given this basic setup we can run a test, and that is exactly what this part of our code does:

if (PersonWalkingThroughDoor == 1) {

If our variable PersonWalkingThroughDoor is equal to 1, then it would pass our test and go into the action phase, where it would preform a certain task.

digitalWrite(LIGHT, HIGH);
digitalWrite(LIGHT, LOW);

What happens in our “action phase” are just some basic commands, first we use digitalWrite() to set the pin attached to the LIGHT variable to HIGH (turning on the light), then we wait 5000 milliseconds, then we turn the light off.
Finally, if you want to expand the function of the test then you can include an “else” clause which would look something like this:

 if (PersonWalkingThroughDoor == 1) {
 digitalWrite(LIGHT, HIGH);
 digitalWrite(LIGHT, LOW);
 else {digitalWrite(LIGHT, LOW);}

In that case, if the initial test is failed you can tell the Arduino to take a different action. In our case it would simply keep the light off if there is nobody walking through the door, and it truly inessential as the light is turned off by default after 5 seconds.

So that’s it, that’s the basic overview of an if function. You might know it, but the best way to understand it is to try it out yourself.

Understanding LEDs

January 5, 2012 Leave a comment

Ah, LEDs. To me, they are to electronics what fire was to the early man.

Such a simple thing. Take any coin cell battery – making sure the plus side of the battery goes with the longer leg of the LED – slide the legs of the LED over the battery and you have LIGHT!

Here’s a simple explanation on how LEDs work from reddit user speedstix:

LED’s are semiconductor technologies that are really good at converting electricity to light. There are two types of semiconductors. N-type silicon and P-type silicon. N-type silicon has excess electrons in it. P-type silicon has extra holes that electrons are drawn to. This is important to remember. Now if you sandwich N-type silicon and P-type silicon together you get something called a diode. Hence the name Light Emitting Diode (LED). Now when these two materials are sandwiched together where they meet the holes from the P-type silicon attract the electrons from the N-type silicon. (think of north and south poles of magnets and how opposites attract) This attraction creates a barrier. Now if you connect a positive DC voltage to the P-type side and the negative part of this voltage to the N-type side this barrier gets smaller and smaller and eventually electrons will jump from the P-type side to the N-type side. Remember from before these jumping electrons are what creates light. Depending on how big or small this barrier is you will get different coloured light. Also if you reverse this voltage you make the barrier bigger and bigger and no electrons will pass. This is exactly what a diode is intended to do. Only allow current to flow in one direction.

For a more detailed explanation you can go here: HowStuffWorks.com

A nice macro image of an LED via wikipedia:

(Side Note: Remember that electrons flow Cathode (-) to Anode (+), and conventional electric current flows positive to negative.)