Arduino reading digital scale
I am the proud owner of Stamps.com Model 510 5lb digital scale. It is a nice little scale which works very well (much better than Stamps.com service itself) while attached to my workstation. The scale doesn’t have a display making any kind of standalone use difficult. However, since the scale is a USB HID device reading data from it should be as easy as from a joystick and Arduino board should be adequate to provide a display function for it. To test this theory I made a simple setup consisting of Arduino UNO, USB Host shield and HD44780-compatible LCD display. I also wrote a small sketch which polls the scale and outputs the weight. The secondary objective of this project was to demonstrate LCD support in USB Host shield library.
For this project I used the following:
- An Arduino board. Standard size board, such as UNO, Duemilanove or Leonardo, will work
- USB Host Shield
- Toshiba HD44780-compatible LCD display, in 16×1 or 16×2 configuration. If you’re planning to use this sketch for something else, like data logging, the display is optional – all output from the scale is repeated to the serial port
- Stamps.com 5lb digital scale. Scales are standard HID devices with usage table 0x8d, therefore, scales from other brands may work as well with no or minimal modifications to the code
- USB Host library
The example code is also hosted at github, as well as in ‘examples’ section of the library under ‘HID’. It has been tested with Arduino IDE version 1.0.5.
In this project, the LCD is connected to the shield’s GPOUT pins, as documented in max_LCD.h header file. In addition to data lines, 5V and ground must also be connected to the shield’s 5V and GND terminals; the RW pin must be grounded – I do it on the LCD itself. In order to see the characters, the display must be biased – a 5K-10K pot with wiper on Vo and other two pins on 5V and ground will provide contrast adjustment.
Continue reading Adding a display to a digital scale using Arduino and USB Host shield
Power switch populated
About a month ago I started shipping USB host shields built on PCB bearing revision number 2.0.1. On this PCB I added a new feature, suggested by Andrew Kroll – a VBUS power switch. The board comes with power switch unpopulated and if you don’t care about this feature it can simply be ignored. However, if you do care about power control, read on.
The ability to turn VBUS on and off at will can be very beneficial. In battery-powered projects the run time can be significantly increased by powering on USB device only when needed. Some other devices can’t even be initialized reliably without a powercycle. Also, many power switches incorporate current limiting circuitry allowing VBUS overload detection and prevention.
An example of populated power switch is shown in the title picture (click on it to make it larger). A is a power switch IC (in this case, Micrel MIC2004). B is 0.1uF ceramic capacitor in 0603 package. C is a wire from MAX3421E GPX pin to the ENABLE pin of the power switch. Finally, D is VBUS Power jumper which needs to be opened, as pictured. Current revision of USB Host 2.0 library is needed to support power control.
Next picture will be used to explain the details of the power control circuitry.
Arrow A points to the jumper which needs to be cut open
Arrow B shows the position where 0603 0.1uF ceramic capacitor needs to be placed
C and D show the places for the power switches (only one switch is needed). Many switches packaged in SOT23-5 and SOT23-6 use this footprint, use On Semiconductor NCP380 as a reference. Also, some other 5 pin switches, such as Maxim MAX4793 and Micrel MIC20xx, will work while placed on SOT23-6 footprint, as shown on the title picture.
Certain switches, such as 6 pin NCP380, allow for adjustable current limit. The position for current setting 0603 resistor is marked ILIM – for the value of this resistor consult the datasheet for the part you’re planning on using
Many switches provide FAULT pin to signal various fault conditions, like output overload, reverse votage, or over-temperature. The pin is typically active low open drain type. It is broken out to a pad labelled VBUS OVL. The signal can be used in several different ways. A LED with a series resistor can be connected across VBUS OVL and a power rail. Also, it can be connected to a MCU input. In this case, a position labelled 10K should be populated with 0603 resistor, typical value is 10K. The other (upper) end of the resistor is connected to 5V rail with a trace which is placed under the letter K; if 3.3V level signal is desired, cut the trace and solder the upper end of the resistor to the 3.3V rail.
The power control signal is labelled VBUS EN. The library uses GPX pin for
Init() functions. There is also a variant of
Init() function which will hold the VBUS off for the number of milliseconds passed to it as a parameter. See usbhost.h file for details. Also, testusbhostFAT.ino demonstrates usage of powercycling
Continue reading VBUS power control on USB Host shield
The code supporting USB Mass Storage Class of devices has been added to USB Host Shield 2.0 library and is available to download on GitHub. Mass storage devices include USB Flash drives, memory card readers, external hard drives/CD-ROMs, smartphones/tablets, and some others – almost anything that shows as a drive while connected to a PC (exceptions are digital cameras as well as some phones pretending to be digital cameras). Andrew Kroll (who made this release possible) – thank you very much!
At present, the code example, also featuring Andrew’s FAT and extended memory implementation, can only be run on “big” Arduinos such as Mega and Mega 2560. Another FAT implementation, developed by Alex Glushchenko, is being tested – there is a slight possibility that at least some functionality can be demonstrated on a regular UNO board. On the other hand, the mass storage component can be used without a file system by simply reading/writing physical sectors; this approach can save a lot of memory. The documentation for the mass storage class code is available here.
Many hours has been spent testing the code; it should work with any device which claims to support “mass storage bulk only” transport. While newer (less than 5 years old) won’t cause any problems, older ones could be finicky. If your device shows odd behaviour with this code, please let me know – I will trade it for the good working one.
Bit reversal code
This is Part 3 of 3-part series of articles. Part 1 talks about ways of tweaking SPI code while Part 2 talks about hardware modifications.
After finishing hardware modifications for my three-SPI-device setup I started coding and hit a snag. Any device would happily work by its own, WiFi and SD were also happy together, however, adding NFC Shield to the mix would disable other two. If I moved NFC initialization to the beginnig, other two devices would work but NFC would not. At the same time, if a device was just present on a bus and not initialized, other two devices were not affected. It became clear that initialization itself was the source of error.
Seeedstudio NFC Shield uses NXP PN532 transmission module. This module supports several communication interfaces, namely SPI, I2C, and HSU. In SPI mode the data format is ‘LSB first’, i.e., transmission starts from bit 0. Such format is uncommon; all other SPI shields I’m aware of – Ethernet, WiFi, USB Host, etc., are using ‘MSB first’ format – the transmission starts with bit 7.
A quick look into NFC library source code revealed the following line in
This line sets the data format. In Atmega microcontrollers the default SPI data format is ‘MSB first’ – all other SPI devices don’t have to set it during initalization. Initial revision of PN532 code ( written by Adafruit ) was using software SPI and awkward bit order was dealt with in
read() functions. When Seeedstudio modified the code to work with hardware SPI, they just switched SPI to ‘LSB first’ format without much thinking, breaking compatibility with the rest of the world. Surely, when I commented out this line, NFC initialization stopped breaking WiFi. Predictably, NFC also stopped working.
Luckily for me, the SPI is pretty basic protocol and bit order setting in SPI controller doesn’t mean much. It really doesn’t matter how the bit order is set; if we need ‘MSB first’ for the majority of the devices we can initialize SPI in a normal way and then modify
read() functions for ‘LSB first’ device to serve it reversed bytes. This is exactly what I did. Below is modified version of Seeedstudio PN532 library (also presented on a title screenshot) – lines 6 and 16 perform bit reversing:
/************** low level SPI ********/
/*Function:Transmit a byte to PN532 through the SPI interface. */
inline void PN532::write(uint8_t _data)
/* bit reversing code copied vetbatim from http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious */
_data = ((_data * 0x0802LU & 0x22110LU) | (_data * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16;
/*Function:Receive a byte from PN532 through the SPI interface */
inline uint8_t PN532::read(void)
uint8_t data_ = pn532_SPI.transfer(0);
/* bit reversing code copied vetbatim from http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious */
data_ = ((data_ * 0x0802LU & 0x22110LU) | (data_ * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16;
After making these modifications, commenting out the ‘LSB first’ setting in the
PN532:begin() and also modifying WiFi code to stay away from pin 9 (see this article for discussion) all three devices are happily working together without conflicts. The library mod for PN532 can be left there permanently – the chip will never know that it is communicating with “misconfigured” SPI host. I’m hoping Seeedstudio will fix their code eventually; in the mean time, if you have SPI compatibility issues, simply make code modifications presented in this article.
WiFi and NFC shield stack
This is Part 2 of 3-part series of articles. Part 1 talks about ways of tweaking SPI code while Part 3 explains how to deal with incompatible data formats.
Add-on boards AKA “shields” greatly expand the capabilities of standard Arduino platform. Typically, a shield adds a specific peripheral interface; if you need several peripherals you may need to use several shields. The biggest problem in this approach is shield compatibility. Shield developers don’t care much about interoperability and sometimes making several shields work together is difficult. In the previous article on the topic I described a project where USB Host and WiFi shields were made to share the same SPI bus. I have been recently tasked with similar project which required rather heavy hardware modifications. This article shows the necessary steps to build a system consisting of Arduino board, official WiFi Shield and Seeedstudio NFC Shield.
Here is the hardware configuration. Both shields come with stackable headers so they can be plugged into each other. Both shields use ICSP for SPI but connectors are non-stackable so at least one of the shields must have its ICSP connector replaced with the stackable variant ( I sell them). Also, both shields use pin 10 for CS so this pin needs to be re-assigned on one of the shields.
NFC shield has offset female connectors therefore if another shield is placed on top of it ICSP connector won’t mate. It means that stackable ICSP connector shall be placed on WiFi shield. At the same time, since NFC Shield does have a second row of contacts re-assigning pin 10 on this board is easy.
Before we start cutting traces and desoldering connectors, let’s establish a baseline or “a known good state”. I simply grabbed an example from NFC shield library distro called
readMifareMemory, compiled, loaded and ran it. I was able to read the card and now I know that my NFC shield is functional.
One other note – both shields are assembled using lead-free solder which has higher melting point. Don’t forget to set the temperature of your soldering iron accordingly – between 700F and 750F.
Continue reading Running multiple slave devices on Arduino SPI bus – hardware modifications.
Allan Caplan sent me a write-up and pictures of putting together headers on USB Host Shield 2.0. The post can be used as an assembly guide for the shield. The following text and pictures were taken straight from Allan’s Evernote which he kindly shared with me and gave me permission to repost. I just wanted to add that after assembly is is a good idea to load and run the quality control routine to make sure the shield works.
Assembling the USB Host Shield (v2.0) is pretty well straight forward, even without instructions. Some people have complained about the lack of instructions, so here goes. This guide assumes you are familiar with basic soldering techniques, and basic Arduino know-how.
1. Unpack the shield. You should see something similar to this: two 8 pin headers; two 6 pin headers; one 2X3 pin header
2. Insert the pins. It should be explanatory where they go – along the outer rows, one side has two banks of 8 and the other has 2 banks of 6. Now, if you have a third hand this may get easy. When I do soldering like this I like to insert all the headers and turn the card upside down, like this:
6x and 8x headers from the bottom
3. Before you start soldering, make sure the header you are working on is straight!
4. Did you check that your header is straight?
5. OK, go ahead and solder. I like to work on each header at a time, making sure it’s straight before starting. Once done, you should have something that looks like this (from the top)
6x, 8x headers from the top
6. Time for the 2×3 header. Insert it like so. Again if you have a third hand this will be easier, if you don’t make sure your header is straight before soldering!
7. That’s it! If you did things correctly you should have a functioning USB Host Shield! Go ahead and fit it on your Arduino board (make sure it’s straight, I didn’t realize mine was crooked in the picture)
Final alignment check
8. You just soldered, clean up your workspace and go wash your hands.
9. Find the instructions for uploading software and running the QC program.
USB Host Library Documentation
This is short but very important announcement. Kristian from TKJ Electronics started a documentation project – here is the link to the post on his blog. Kristian configured Doxygen and set up the framework to build the documentation from the source code of the library. In addition to all that, Kristian documented all the code he has contributed to the USB Host library 2.0. Results can be seen here.
Documenting a function is now as easy as adding a specially-formatted comment at the beginning of the function. In the near future, I will start adding comments to the core USB functions as well to help people understand the internals of the library. It is going to take a bit of time to document everything but even what is available now is a very good beginning – thank you, Kristian!
Discovering Arduino Bluetooth
This is a second article about Bluetooth connectivity using USB Host library for Arduino. Previous article described Android connectivity
; today, I will show how to connect an Arduino to a Windows machine using USB Host Shield ( available at the store
) and USB Bluetooth dongle.
The biggest issue with Bluetooth on a Windows machine is third-party Bluetooth stacks. They all work well but behave slightly differently and it is not possible to write a step-by-step walkthrough valid for any Windows PC. My setup is 64-bit Windows 7 with Broadcomm Bluetooth stack; other Windows versions and Bluetooth stacks will be slightly different.If you have any difficulties following the text, leave a comment and I’ll try to help. Also, Bluetooth protocols time out very quickly – 5-10 minutes, sometimes less. If you get a timeout at any step simply start over again; sometimes, resetting Arduino may be necessary.
First thing that needs to be done is to make a working Arduino setup. Previous article on the topic gives plenty of information about necessary gear, compatible Bluetooth dongles and expected terminal output. Once this is done, install Bluetooth on Windows. On Win7, the installation consists of plugging in a Bluetooth dongle to USB port and watching the progress of installation process which will start automatically. At the end of the installation system will give you the warning that your PC is not discoverable – ignore it. If installation is successful a Bluetooth icon will appear in the system tray.
The next step is to pair Arduino to a PC. At this point, an Arduino-USB Host Shield-Bluetooth dongle combo shall be put together as described earlier, tested, and set up as follows:
- Powered from external supply – optional but highly recommended, at least for the first attempt. If you decide to use external supply, connect it before USB cable
- Connected to a PC with USB cable. It could be the same PC to which you are going to pair your Arduino via Bluetooth
- Terminal to Arduino opened. It can be Serial Monitor in Arduino IDE or third-party terminal program such as Putty or Teraterm. If everything is wired correctly and Bluetooth dongle is good you should see the following output in the terminal window:
Continue reading Using USB Bluetooth dongle on Arduino to connect to Windows PC
Wireless Xbox360 code
[EDIT] Four simultaneous controllers are now confirmed to work [/EDIT]
This is a short announcement of another nice contribution to the USB Host 2.0 library code. Kristian Lauszus from TKJ Electronics announced release of wireless Xbox360 controller support for USB Host 2.0 library; up until now only wired Xboxen were supported.
As announced, up to 4 controllers are supported, however, only one has been tested so far. If you have (or can borrow) multiple Xbox controllers, please help Kristian test his code.
Arduino connected to Nexus S
Since Kristian’s release of SPP service for USB Host 2.0 library
I got many e-mails with questions about Bluetooth serial communications in general and using the SPP class in particular. While establishing a Bluetooth connection is quite easy when using PCs/smartphones/tablets, the Arduino implementation is less user-friendly (for a good reason) and somewhat confusing. This article is intended to be a short tutorial of Arduino USB Bluetooth connectivity using an Android device as a peer – in my opinion, the simplest one to set up and communicate.
To follow this tutorial, you will need the following:
- An Arduino board ( I sell them too )
- full-size USB Host Shield 2.0
- USB Host Shield 2.0 library downloaded and installed in your Arduino library directory
- An SPP example sketch
- Bluetooth dongle – see below for detailed discussion
- Android device – a phone or tablet. The accuracy of this tutorial has been checked on Nexus S phone and Nexus 7 tablet, both running Jelly Bean 4.1.1 OS.
- A Bluetooth terminal emulator app for Android – I use Blueterm
The hardware setup is shown on the title photo (click on it to make it larger). It is a good idea to use external power supply at least in the beginning – some Bluetooth dongles are not happy when powered together with Arduino board/USB Host shield from USB cable. On a side note, it is always a good idea to use external power supply while experimenting with USB devices, especially when using current Arduino boards with their weak regulators. Also, Bluetooth code requires plenty of resources. The example used in this article compiles in ~24K and even though it would work on standard Arduino, any meaningful project will likely require a bigger board, like a Mega.
Continue reading Using USB Bluetooth dongle on Arduino to connect to Android phone