Displaying on Paper – Thermal Printer + Arduino

Tuesday, August 30 th , 2011

The following code and library are compatible with arduino software 1.0+ ONLY. You can download the newest version of the arduino software here.

Outputting data can be extremely useful, and typically when we look at doing this, it is either to the Arduino’s serial terminal, or to some sort of display. But what about making physical copies of the data? So a few months back, SparkFun started selling a thermal printer that you could connect to a microcontroller (or via adafruit ). That same day my brain filled with crazy thing you could do with it, like a giant fortune cookie that could print your fortune on the fly.

Anyways… If you dont know about thermal printers, they are most often the printers your store’s receipts are printed on. The reason for this is that they dont use ink, or use a cartridge of any sort. The paper it prints on turns black when heated. So this printer simply applies heat where another printer would apply ink. Eventually the printers head will wear out, but this after several miles of printing. Yes, miles – about 30 of them. So given that the rolls of paper are 34ft long, you can print about 4,600 rolls before the print head dies, meaning for most of us, you will never see that happen.

Hooking it up

So this printer can be powered off of 5V, but NOT the 5v pin on your arduino, and no via USB. It uses very little power when it is is just sitting by, but uses around a full amp when it is printing! And because the most USB can handle is half of that, we need an external power supply. Something between 5 and 9V, and a minimum of 1400ma output (larger is fine). I found that powering the printer with 9v, it printed much faster, and slightly darker than 5v, and if you have it available, I recommend using 9v.

There are two ways of connecting the power supply as shown in the illustrations. You can either power your arduino and the printer separately, or you can power the arduino, and power the printer off of the arduino.

To do the latter, the printer’s power wire (red) needs to connect to the VIN pin on the arduino, not the 5v pin. The reason is that the 5v pin goes through a regulator and is not meant to supply this kind of power, but the VIN pin is directly connected to the power input on the arduino. So it can supply much more.

Now just connect the rest of the wires as shown.

Code

The printer needs a serial connection to the arduino. And normally that would mean we would connect to digital pins 0,1 but, if we did this, anything you printed to the serial terminal for debugging or otherwise the printer would see and possibly print out. We don’t want that, so we will be using a libraty so that you can keep those pins free.

This printer has a huge amount of features. 2 different barcode, text output, size settings etc, and each one needs a new configuration to get it working. So we created a library for this so that you could keep your code clean, and simple.

To make this code work, before you load the code, or even open the Arduino program, we need to place the “Thermal” folder into your Arduino Library. If you don’t know where that is by default, Look to the right.

If you click the download button to the right of “Arduino” you can download the whole thing as a zip, so you dont need to copy all the files.

Default Library Folder Location

On your Mac:: In (home directory)/Documents/Arduino/libraries
On your PC:: My Documents -> Arduino -> libraries
On your Linux box:: (home directory)/sketchbook/libraries

darkness And Speed Calibration

If you want to get a little more speed, or darkness out of the printer, you can change some settings in the thermal.cpp file of the Thermal library.

heatTime = 120; //80 is default from page 23 of datasheet. Controls speed of printing and darkness
heatInterval = 50; //2 is default from page 23 of datasheet. Controls speed of printing and darkness
Unless otherwise stated, this code is released under the MIT License – Please use, change and share it.

Changing heatTime and heatInterval to lower values, will yield a faster, but lighter printing, and raising them will yield a slower, but darker printing. If you are running off of 5V, and find the printing too light, try changing both values to 255.

You can make changes to the file with the arduino sketch open. Just save the Thermal.cpp file and re-upload the sketch to see the changes.

Help expand the library

We would love help expanding the library if you are interested. We would like to add to support for bitmap printing and other text setting. Also there may be a better way to set many of the settings than we are now. If you are interested in helping, let us know in the forum.

Polar Heart Rate Monitor Interface + Arduino

Wednesday, August 24 th , 2011

The following code and library are compatible with arduino software 1.0+ ONLY. You can download the newest version of the arduino software here.

When you start to talk about biometrics in electronics, heart-rate is usually the first thing to come up. And why not? I think it is often the thing we are most aware of changing when we get excited, nervous, are being active, or very calm. There are several ways of sensing heart rate that dont require lots of heavy equipment and electrodes taped to you. A lot of work has been done on wrist worn IR based sensing, but it tends to be error prone, especially with movement. Currently, the best available method at a reasonable price seems to come from the company Polar. And, what makes it better is that Sparkfun in conjunction with danjuliodesigns, created a microcontroller compatible receiver for the polar monitors. So let’s take a look at using the Polar Heart Rate Monitor Interface and getting it running with your arduino.

The annoying thing about all of this is that you do need to wear a sensor on your body to get this going. I picked up this guy off amazon and had pretty good results most of the time.

This transmitter band is a reusable electrode that wirelessly transmits to a receiver. A note when you are testing, the band will only transmit when it is on person, or you have semi-wet thumbs pressed into the sensor plates. Also because it is an electrode, it wants a good connection with your skin… meaning sweat. They sell electrode gel you can use with it for a better connection when the skin is dry, but I never tried it. In the humid summer nights I was testing this in, I never really had an issue.

Hooking It Up

Ok… so this board is capable of a ton of stuff. But we are just looking today at using this with the Arduino, and the simplest way is to get this guy running in I2C mode because it will allow us to get the heart rate from it easily into our Arduino.

I2C is a 2-wire serial connection, so you just need to connect the SDA (Data) and SCL (Clock) lines to your Arduino for communication. On your Arduino (everything but the mega) SDA is on analog pin 4, and SCL is on analog pin 5. On an Arduino Mega, SDA is digital 20, and SCL is digital 21.

The board doesn’t come setup for I2C by default, so we need to make a few changes. There are two solder-jumpers on the board that need to be changed to make this happen. The first is SJ1 just to the right of the 5V pin. Remove the solder from there using solder wick or a solder sucker so that the two parts are not connected. (like in the illustration). Next look at the row of pads at the top of the board labeled OP0 – OP7. We need to solder OP0 so the two pads are connected (See the illustration).

Ok, so once you have the board modified and ready for I2C communication, we can actually just power it up with 5v to check if it is able to communicate with the band. With the board on, moisten your thumbs and press them firmly into the back of the band where the electrodes are and hold it less than a food from the monitor board. You should see a green LED on the board start to blink. If it does… Awesome, the band and the board are communicating! If not, try it a bit more, and see if you can get it going.

So, now that we know they communicate with each other, connect the board to the arduino as shown, and load up the code below.

When it is actually running, you will need to wear the band on your chest to get a heart rate signal.

code

This code is pretty simple. It will take the info from the sensor band (average of 20 of the samples) and report that back in the Arduino’s serial terminal as BPM (beats per minute). If it dosnt have a connection, or the band does not have a good connection to the skin, it will report a BPM of 0.

You should know better than I, but your BPM reading should be between 55 and 140. If you are just sitting around, and you see it around 200, it probably means it has a bad connection. Make sure it has a good connection with your skin, and restart the receiver board/ Arduino. – A note… I did have an hour reading that was very off, but all the other time, it was very accurate.

//Arduino 1.0+ Compatible only
//Arduino 1.0+ Compatible only
//Arduino 1.0+ Compatible only

// Code to retrieve heartrate information from the Polar Heart Rate Monitor Interface via I2C
// Part: http://www.sparkfun.com/products/8661
// Article:  http://bildr.org/2011/08/heartrate-arduino/

#include "Wire.h"

#define HRMI_I2C_ADDR      127
#define HRMI_HR_ALG        1   // 1= average sample, 0 = raw sample

void setup(){
  setupHeartMonitor(HRMI_HR_ALG);
  Serial.begin(9600);
}

void loop(){

  int heartRate = getHeartRate();
  Serial.println(heartRate);

  delay(1000); //just here to slow down the checking to once a second
}

void setupHeartMonitor(int type){
  //setup the heartrate monitor
  Wire.begin();
  writeRegister(HRMI_I2C_ADDR, 0x53, type); // Configure the HRMI with the requested algorithm mode
}

int getHeartRate(){
  //get and return heart rate
  //returns 0 if we couldnt get the heart rate
  byte i2cRspArray[3]; // I2C response array
  i2cRspArray[2] = 0;

  writeRegister(HRMI_I2C_ADDR,  0x47, 0x1); // Request a set of heart rate values 

  if (hrmiGetData(127, 3, i2cRspArray)) {
    return i2cRspArray[2];
  }
  else{
    return 0;
  }
}

void writeRegister(int deviceAddress, byte address, byte val) {
  //I2C command to send data to a specific address on the device
  Wire.beginTransmission(deviceAddress); // start transmission to device 
  Wire.write(address);       // send register address
  Wire.write(val);         // send value to write
  Wire.endTransmission();     // end transmission
}

boolean hrmiGetData(byte addr, byte numBytes, byte* dataArray){
  //Get data from heart rate monitor and fill dataArray byte with responce
  //Returns true if it was able to get it, false if not
  Wire.requestFrom(addr, numBytes);
  if (Wire.available()) {

    for (int i=0; i<numBytes; i++){
      dataArray[i] = Wire.read();
    }

    return true;
  }
  else{
    return false;
  }
}
Unless otherwise stated, this code is released under the MIT License – Please use, change and share it.

But it’s not working!

If it is not working, 99% of the time, it is simply a sensor-band placement issue. Make sure you have a blinking light on the board. If you don’t have that light, the band is not communicating with the board. It wont communicate unless it thinks it is on your body, so your skin could be too dry or the placement is off.

If you see the communication light, but nothing in the arduino, make sure you soldered/ unsoldered the solder-jumpers as described above.

Arduino: Individually Control Shift Register Pins

Tuesday, August 23 rd , 2011

If you want an article on general 74hc595 shift register usage with detailed explanition on how it works/what it’s doing, you want this article here: http://bildr.org/2011/02/74hc595/

The following code and library are compatible with arduino software 1.0+ ONLY. You can download the newest version of the arduino software here.

Earlier this year we did an article on using the 74hc595 shift register with your arduino. It has been, by far, our most popular article. It seems that a lot of people want to control a lot more LEDs (or other) than the arduino can do without help. So we actually went to the guys over at sparkfun and said… “Hey! Let’s make this even easier”. And so we came up with the 74hc595 breakout board.

So now you find your self asking “Why?” Well… chainability! Using right-angle headers you can chain a ton of these together. AND! With the code we have for you, you get an additional 8 digital outputs per board you have connected. That means if you have 1000 of these chained together, you will be able to say “setPin 7,432 HIGH”, and BAM! Just like that pin 7,432 goes HIGH. It will literally be like you have more digital outputs on your arduino, no funny business.

Hooking it up

At first this board may look confusing to hookup… But just click on the illustration to get a better view of how it is hooked up. We won’t be using Reset, so just connect that to power, and connect /OE to ground (enables the board). For VCC you can connect this to 3.3 or 5V on your arduino, and the outputs will just reflect this. So if you power the board with 3.3v, the outputs on the board will output 3.3v as well. We just need 3 digital pins to control the board, and after that we are done.

You can chain as many of these as you would like. I used right-angle header pins to make it modular and clean. I picked up some 7 pin right-angle headers from digikey to make it even easier. But you dont have to use header pins for this if you don’t want. You can easily just wire them together too. Either way, see the illustration below to see how you do this.

Code

Because it is so much easier to use a lot of these now, we wanted the code to be the same. So we made an Arduino library that is capable of controlling near 2000 of these chained together and still enable you to call each pin individually. (this library can be used with the plain 74hc959 chip as well)

The library by default will only support a chain of 25 of these. But that can easily be changed by changing one number in the file “Shifter.h” . If you find your self with a larger chain, hit us up in the discussion area, and we can help you out.

To use the library, you just need to create an instance of the Shifter library, then you have a few functions to do everything.

shifter.clear(); – This sets all of the pins on the register chain to LOW
shifter.setPin(20, HIGH) – Turn pin 20 (or whatever) HIGH/LOW
shifter.write() – Displays the changes made.

Best practice is to only call shifter.write only when you need to display the changes. This is because shifter.write() can be slow if you have a ton of these chained together (about 1ms per 10 registers (80 pins))

Make sure to check out the discussion area. We came up with a few cool sequences to test out/ play around with – including the one in the video

To make this code work, before you load the code, or even open the Arduino program, we need to place the “Shifter” folder into your Arduino Library. If you don’t know where that is by default, Look to the right.

If you click the download button to the right of “Arduino” you can download the whole thing as a zip, so you dont need to copy all the files.

Default Library Folder Location

On your Mac:: In (home directory)/Documents/Arduino/libraries
On your PC:: My Documents -> Arduino -> libraries
On your Linux box:: (home directory)/sketchbook/libraries

Video

« Older Entries