Converting Cheap Chinese USBHID Fake USBasps Into Real USBasps

I recently ordered some USBasps from Amazon, which looked interesting, because unlike the typical USBasps, which are just bare PCBs, these had metal cases:

They are also common on AliExpress.

Unfortunately, when I plugged one into my computer, it detected as a USBHID device with VID=03EB and PID=C8B4, rather than as a USBasp. I tried overriding the USBHID driver on my Windows 10 machine, but that didn’t work.

Thankfully, after doing a bit of searching on the Internet, I found that others had encountered the same problem, and had found a solution. It seems that the firmware loaded into these things from the factory is proprietary, and require that you use the manufacturer’s janky software … it’s not AVRdude compatible!

Thankfully, the hardware is actually compatible w/ USBasp firmware with a minor tweak, and you just have to flash it with modified USBasp firmware.

I have a bunch of real USBasps, so I used a USBasp to convert the fakes into real USBasps! In order to program it, slide off the metal case. Next need to connect a jumper across the two holes labeled –> UP <–. The jumper enables programming of the onboard ATmega88V. Then plug it into your other USBasp or other ISP programmer, using the 10-pin ICSP cable:

So where do you get the special firmware? GreenPhotons has graciously compiled a modified firmware for us. Next use AVRdude to program the USBasp firmware into our target:

avrdude -cusbasp -pm88 -Uflash:w:20161227_mega88_usbasp.hex

You can use any ISP you already have, if you don’t have another USBasp. Just substitute the programmer in the -c parameter (e.g. -cusbtiny for a USBtiny). If you don’t have another ISP programmer, you can use an Arduino. This guy shows you how, as well as another way to get firmware.

If you get the following error, then your USBHID ISP has an ATmega88P instead of an ATmega88V

D:\hacking\arduino\USBasp\convert_usbhid>avrdude -cusbasp -pm88  -Uflash:w:20161227_mega88_usbasp.hex

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.02s

avrdude: Device signature = 0x1e930f
avrdude: Expected signature for ATMEGA88 is 1E 93 0A
         Double check chip, or use -F to override this check.

Just substitute -pm88p for -pm88 in the avrdude command line:

avrdude -cusbasp -pm88p -Uflash:w:20161227_mega88_usbasp.hex

If you have an old copy of avdude that doesn’t like -pm88p, you can instead use -F to force avrdude to ignore the device signature:

avrdude -cusbasp -F -pm88 -Uflash:w:20161227_mega88_usbasp.hex

Downloads:

modified ATmega88 firmware for USBHID USBasp

References:

Making USBasp Chinese Clones Usable

Hacking An AVR Programmer

Hacking An AVR Programmer II

I found this great resource after writing this article:

USBasp on a Clone

Korg N1 Synth Repair

I have a Korg N1 synth, which I’ve had for many years. Recently, it started having strange symptoms: after warming up for several minutes, the audio would get slightly distorted, but only on certain notes. The effect was rather subtle, but enough to drive my wife absolutely bonkers.

First, I tried to isolate the problem. I tried headphones, connected to the jack on the front left of the keyboard, to rule out my amp. The distortion didn’t go away. Next, I compared the output from the rear audio jack to the headphone, and the distortion sounded exactly the same.

I first tried google, to find out if this was a common problem. It turns out that the N1 wasn’t particularly popular, so there wasn’t much info on problems/repairs, not even teardown photos.

OK, time to take it apart. I first took the screws off the wood side panels, and removed the metal cover on the front, under the keys. Totally the wrong approach. Upon looking at it more closely, I noticed that the bottom is hinged. All you have to do is take out the big philips screws, and then it flips open.

Once I had access to the inside, I looked/smelled for any burned/damaged parts, loose wires, and bad solder joints. Nothing looked amiss. Another thing that’s a common failure point in aged electronics is aged electrolytic capacitors. They often go bad. The most obvious clue is that the tops will bulge up or burst open. I couldn’t see any that looked obviously bad, so I started touching the tops of the SMT electrolytics to feel if any of them were bulging. I found one that had an almost imperceptible bulge on top. It’s in the photo below:

The bulge is so slight, you can’t even see it in the photo above, but I could just barely feel it. Surely, this wasn’t the bad component? Since I couldn’t find anything else wrong, I decided to try replacing it.

At first, I was going to try to remove it w/ a hot air gun, but it’s so big, and close to other components, that I decided that was too risky. I searched the Internet again, and someone suggested just cutting the can in half w/ some cutters, and then yanking out the rest. It turned out to be an easy and safe method. And, it left two short protruding wires attached to the circuit board, which made it easier to solder in a new cap. (Sorry, I should have taken photos, but forgot).

Anyway, here’s what it looks like w/ the replacement soldered in place:

I was extremely skeptical that replacing this capacitor would fix the distortion, but amazingly, when I turned it on, and left it for an hour, the problem was gone! It’s been over a month now, and there is no more distortion, no matter how long it’s turned on. Amazing.

Below is a full photo gallery of my teardown:

Direct link to Google Photos: https://photos.app.goo.gl/VwxPwYw8nxvgwx8Q9

Weller WESD51/WES51 Repair – No Heat

Over the past couple of years,my Weller WESD51 soldering station had been getting progressively flaky. Sometimes, I would have to fiddle with it for a while to get it to heat up. Finally, one day, it just stopped heating altogether. I had trouble finding info on how to fix it, mainly because I couldn’t even find the pinouts for the soldering pencil attachment jack.

Finally, I hit the jackpot last week. I found this thread in AllAboutCircuits discussing a similar model, the WES51. The main difference between the WESD51 and the WES51 is that the WESD51 has a digital temperature display, while the WES51 only has a status LED. While the thread didn’t tell me how to fix it, I found the user/troubleshooting manuals attached! I have linked the manuals at the bottom of this post. Unfortunately, following the troubleshooting guide didn’t help me find the problem, because everything checked out OK.

Then I found this guy’s YouTube video on fixing a WES51 that wouldn’t heat up. As I suspected, the PCB’s in the two different models is very similar. In the guy’s video, he fixes it by replacing a 2.2uF capacitor that’s connected to the heater’s power transistor. I checked the corresponding capacitor in my WESD51, and sure enough it was bad. I found a in my junk parts bin to swap in and bingo, my WESD51 is working again! The step by step procedure is below.

Step 1: Open the case

First, you need to open up the case. Pop out the rubber feet at the bottom of the controller case. Underneath are philips screws.

After you remove all four of them, the blue upper body easily separates. Here’s what’s inside:

Step 2: Remove PCB

Separate the green PCB from the casing. You don’t have to remove the temperature adjustment knob from the faceplate. Locate the 2.2uF/50V capacitor. It’s circled in red in the photo below:

Step 3: Remove existing 2.2uF capacitor

Carefully desolder the capacitor circled in red above. If you don’t have a capacitance meter, just try swapping in a new part, and see if it fixes the problem.

Step 4: Solder in a replacement capacitor

Make sure to pay attention to the polarity of the electrolytic capacitor… the negative terminal faces the line of SMT resistors on the right of the photo above. I didn’t have an exact replacement in my parts bin, so I just used a 4.7uF/25V part… the circuit voltage is 24V, so 25V isn’t much headroom, but it’s easy to replace it again if it fails in the future. Before putting everything back together, I fired it up, and it was heating again!

Downloads:

WESD51 spec sheet

WESD51 User/Troubleshooting Manual

WES51 User/Troubleshooting Manual

Build an Audio Player Based Mind Machine Part 1: Photic Goggles [REPOST FROM GEEKMATICA]

NOTE: This is just a repost of an article that originally appeared in my geekmatica blog back in 2007. I’ve re-posted here, because it’s referred to by my Build Ganzfeld-style Photic Goggles (AVS Lightframes) article.

Commercial mine machines are quite expensive, and often inflexible, containing only a few canned programs. In this series, I will show how to build an infinitely reconfigurable photic-stim mind machine on the cheap. Rather than build an oscillator circuit, we will drive our mind machine with computer-generated waveforms via an audio player. You can use your favorite audio playing program on your computer, or a portable MP3 player.

UNDER CONSTRUCTION: I still have a ways to go w/ filling in details, but I thought I would at least throw up the article first, since it might take me forever. Please pardon the omissions.

Keywords: DIY, build, mind machine, brain, AVS, photic goggles, audio visual stimulation, lightframesPart 1A: Photic-stim Goggles (aka Lightframes)

Photic-stim goggles cost typically more than $50, but they’re typically built with only <$1 worth of LED’s, and some cheapo ski-type sunglasses.

Below are the lightframes which came with my Sirius AVS machine, which I purchased from MindModulations.com.

Note how they are just as I described above, a cheap pair of ski-type sunglasses, with 8 LED’s wired into a sheet of black plastic. The plastic is attached to the sunglasses with double stick foam tape.

Below are the materials we need for our project:

Bill of Materials (BOM)

  1. Ski goggle style sunglasses. I purchased this pair for under $10
  2. Stereo wiring with 1/8 in phone plug. A cheap pair of earbuds from a local 99 Cents Only Store is perfect for cannibalizing.
  3. Thin, flexible black plastic sheeting. A spare DVD case courtesy of junk mail from AOL will do fine.
  4. LED’s. The type and number of LED’s is up to your own taste. Since my design uses the black plastic sheet insert, I picked 3mm LED’s… they need to be small enough that they won’t bump into your eyes when you’re wearing the goggles. If you want to drill directly into the goggle lenses and eschew the plastic sheet, you can use bigger LED’s. I bought my LED’s from UniqueLEDS.com. They have an extensive selection, and list detailed specs for the LED’s, including brightness. SUPERBRIGHT
  5. Not pictured is a few inches worth of hookup wire. I had some kynar-insulated 40AWG wire wrap wire laying around, which was perfect, due to its extremly thin diameter.

How many LED’s you use is up to you, but the minimum is two, one for each eye. I decided to use 8 … 4 for each eye. Also, I used two different colors, green and blue, on alternating diagonals. Note that my LED’s have 3 leads instead of the usual two. This is because I had a wild idea of using bicolor LED’s so that I could get different color combinations, but I’m not very sensitive to the colors, so I decided to hook them up like one-color LED’s. There are various recommendations on the web for color selection. Personally, I like the bright white LED’s that my Sirius frames used, but I decided to experiment with colors on this set.

First, cut the black plastic which will support your LED’s. Ask I said above, you can skip this step, and drill directly into the lenses, but it will look a lot uglier, since the wiring will be exposed in the front. Also, the drilling method doesn’t let you easily reconfigure the frames if you change your mind about the layout. I used graph paper to make a paper template for my LED layout:

You should lay out the LED’s so that they’re fairly well-centered over your eyes. If you space them out a bit, it will allow for inaccuracy, and eye movement. Note that my layout is rotated 45 degrees with respect to the layout used by the Sirius. My DVD case was soft enough that I could just use scissors to cut out the template, but you may need to use an X-acto knife.You can use a drill for the holes, but I spun an X-acto knife around, instead, and slowly increased the size of the holes until I had a tight fit. (BTW, for the curious, those are not my hands in the photo).

I misaligned one of the holes a bit, as will be apparent in later photos, but it works fine, even though it’s a bit ugly.

Since we want to keep the LED’s far enough away that they don’t hit your eyes, we need to bend the leads as tightly as possible. I used a pair of needle-nosed pliers, but if you’re careful, you can just use your fingers on the edge of your desk:

Here’s what they look like when they’re done. Since my layout uses alternating colors on the diagonals, I first bent two of the green LED’s. Then, I bent another pair of green LED’s in the opposite direction. The opposite polarity of the bending is important, because the LED’s are hooked up in parallel. Here’s what they look like before I cut the leads:

Now, the wiring and layout of the leads will vary considerably, depending on how you want your LED’s to behave. The lightframes I built behave very differently from the Sirius lightframes, which utilize the industry standard wiring. In the Sirius setup, all 4 LED’s on a particular side light up synchronously. The left side responds to the left channel, and the right side responds to the right channel, so the two sides can be controlled independently. This is an example of how they can blink:

My design mimics the IC/D setting of TC-Softworks lightframes, hooks up each pair of diagonals together. The greens respond to the right channel, and the blues respond to the left channel. Therefore, both eyes are always lit, but the intensity and color can vary.

You can use your imagination to come up with your own unique design.

If you are building your goggles to work with a particular mind machine, be aware that there is another variation in the wiring, which affects the compatibility. All of the schematics I have shown up to now have used Common Ground (CG) wiring, where the cathodes (grounds) are all tied together. Some machines, such as my Sirius, use Common Power (CP) wiring, where the anodes (power) are tied together. Before wiring up your goggles, make sure you know if you need CG or CP wiring. As it stands, the goggles I built are incompatible w/ my Sirius.

You must also be aware that which polarity you choose will affect the polarity of the signals that you use to light up your goggles. CG goggles light up when the signal is positive, and CP goggles light up when the signal is negative. Thus, if we use the following signal, CG goggles would light up in the first half of the cycle, and turn off in the second half, where the signal goes negative.

On the other hand, CP goggles will do the opposite, and be dark in the 1st half cycle, then light up in the 2nd half cycle.

Next, I fit them into the plastic, and soldered the wires:

Be careful to solder as quickly as possible, and not to use a high-wattage iron, because LED’s are semiconductors, and can easily be destroyed by overheating.

The inner pairs of LED’s have to be bent a bit differently, so that the leads don’t touch and short out w/ the outer pair of LED’s.

In my case, they were the blue LED’s. Note that the LED’s w/ the extra bend actually go on the inside, so I had to take the first pairs out first. Here’s what they look like up close. Although the inner and outer wires look like they’re touching, there’s a tiny clearance. Here’s what it looks from the other side:

I know, I know, the holes are a bit off, but it’s accurate enough.

Next, I connected up the wiring. Since my goggles are wired up for CG, the center leads (the cathodes) of all of the LED’s are wired together. The anodes of each color are all wired together. NEEDS PIC OF TRS WIRING HERE

Testing …

Left channel only

Programming OpenEVSE with a Serial Cable instead of Hardware Programmer

Typically, OpenEVSE firmwares are flashed into the board using a hardware programmer, such as a USBasp. In the past, this was required, because the firmware had grown so large that there was no space left in the ATMega328P‘s flash to fit in a bootloader. However, the latest versions of the AVR tools that come with Arduino have shrunken down the binaries to the point that we now have space for a bootloader. Once the bootloader is installed, OpenEVSE can be programmed in exactly the same fashion as an Arduino Pro Mini, via a USB->TTL UART adapter, such as a FTDI cable, using the stk500 (arduino) protocol.

Before we can program the chip with a bootloader, we need to make a minor hardware mod. After a reset, the bootloader waits to see if a new firmware wants to be flashed before proceeding with booting the installed firmware. It is only during this very small time window that the ATMega328P‘s MCU is ready to accept a firmware. In order to trigger a reset via software, we need to connect the DTR pin of the FTDI cable to the RESET pin of the MCU via a .1uF capacitor.

Below is a photo of the mod, done on a Wattzilla C3 board, which is an OpenEVSE variant:

The DTR pin is on the far left of the 6-pin serial connector. The RESET pin can be accessed at either the left side of R10, as pictured above, or at Pin 5 of the ISP connector (red circle).

Once the hardware mod is in place, we must set the fuses to use a bootloader, and flash in the bootloader, using a hardware programmer. In this example, we will use OptiBoot, because it’s smaller and faster (115200 baud) than the standard Arduino bootloader.

avrdude -c USBasp -B0.5 -p m328p -Uflash:w:optiboot_atmega328.hex -Ulfuse:w:0xFF:m -Uhfuse:w:0xDE:m -Uefuse:w:0x05:m -Ulock:w:0x3F:m

After the bootloader is flashed in, we can thereafter flash in firmwares using just the FTDI cable:

avrdude -carduino -PCOM5 -b115200 -p m328p -Uflash:w:open_evse.hex

Substitute your FTDI cable’s virtual serial port for COM5 above.

For those who are not comfortable with command lines, it’s also possible to use the Arduino IDE to burn the bootloader, and flash in firmwares.

  1. Set your board to Arduino UNO by using the menu to navigate to Tools->Board->Arduino UNO
  2. Select your hardware programmer via Tools->Programmer
  3. Install the bootloader via Tools->Burn Bootloader
  4. Disconnect the hardware programmer, and use Tools->Port to select your FTDI cable’s virtual serial port.

Thereafter, you may flash in your sketches with the upload button. The above procedure will also work with any DIY or other Arduino clone which is not wired for a bootloader. Note that the bootloader takes up 512 bytes, so your maximum sketch size drops from 32768 to 32256 bytes.

 

 

Precision PWM Frequency for Arduino / ATmega 328P

I’m working on an application where I need fine adjustment of PWM frequency. The existing PWM code that I found, such as the Arduino PWM Frequency Library, only allows integral frequencies to be selected. On pins supported by 16-bit timers, the Arduino PWM Frequency Library allows fine adjustment of duty cycle, but not frequency. After searching for a while, I found an interesting article in Udo Klein’s Blinkenlight blog: Flexible Sweep. In the article, Klein has an Arduino sketch which sweeps the LEDs of a Blinkenlight board from 0-999.9999 Hz in increments of .0001 Hz.

I hacked his sketch into PrecisionPWM, which outputs PWM to any arbitrary digital pin in increments of .0001 Hz. What’s nice is that since it doesn’t use the ATmega’s internal PWM generator, you can use it on any arbitrary digital pin, whether or not it supports hardware PWM.

Download from github: PrecisionPWM

How to Dramatically Speed Up AVRDUDE with USBasp or USBtinyISP Programmers

AVRDUDE has a little-known command line parameter, -B, which sets the bitclock, and can dramatically speed up writing/reading firmware to/from an AVR MCU when using a USBasp or USBtinyISP. For a USBasp, simply add -B0.5 to your command line parameters. Example:

avrdude -cusbasp -B0.5 -pm3280 -U flash:w:firmware.hex

In my tests, adding -B0.5 reduces the time to write & verify a hex file by about 2/3! For the USBtinyISP, add -B1 to your command line parameters. Example:

avrdude -cusbtiny -B1 -pm3280 -U flash:w:firmware.hex

The speedup is even more dramatic with the USBtinyISP. In a specific test, I found that write/verify time dropped from 59 sec to 17 sec!

You can also speed up programming from the Arduino GUI. Simply edit your programmers.txt file. In older versions of Arduino, it can be found in <ArduinoFolder>/hardware/arduino/avr/boards.txt. For Arduino 1.8.x, it’s located in C:\Users\<YourUserName>\AppData\Local\Arduino15\packages\arduino\hardware\avr\<version>\programmers.txt.

For the USBasp, add the -B0.5 parameter to the usbasp.program.extra_params line:

usbasp.program.extra_params=-Pusb -B0.5

In order to realize the speed gain in programming, the USBasp must have firmware which supports the setting of SCK. If AVRDUDE gives you this warning:

avrdude: warning: cannot set sck period. please check for usbasp firmware update.

then you must update your USBasp’s firmware. Follow the instructions in my article:
How to Update the Firmware on a USBasp V2.0

For the USBtinyISP, add the -B1 parameter to the usbtinyisp.program.extra_params line:

usbtinyisp.program.extra_params=-B1

If the Arduino GUI is already running, you must restart it in order to load the new settings.

WS2812B LED (NeoPixel) Control: Part 2 – WiFi Control via Art-Net on ESP8266

INTRODUCTION

For wireless control of WS2812B (NeoPixel) LEDs, I initially played with Bluetooth SPP (Serial Port Profile), due to the simplicity of setting up the host software… from the host’s software’s point of view, the connection just looks like a physical serial port. Unfortunately, the flakiness of my Windows 8.1 PCs’ Bluetooth SPP support caused me to abandon that solution.

WIFI CONTROLLER HARDWARE

ESP8266 modules provide a very low cost method of interfacing WS2812Bs to WiFi. Adafruit’s Huzzah module costs $9.95, but on eBay, NodeMCU clones, such as the LoLin NodeMCU board can be had for ~$3 shipped from China. This makes it even cheaper than the Arduino/Bluetooth combination!

What’s more, the LoLin board has a CH340G onboard, so it doesn’t require a FTDI cable to connect it to your host computer for programming. I ordered a few of the LoLin boards, but in the meantime, I started playing with the Adafruit Huzzah boards I had on hand.

With the addition of ESP8266 support via the Board Manager, Arduino becomes an easy to use platform for code development. Also, there are easily obtainable libraries for both WiFi configuration and control of the WS2812Bs.

One extra complexity of using an ESP8266 to control WS2812Bs is that the ESP8266 is a 3.3V device, while the WS2812B is a 5V device, (usually) necessitating level shifting. The WS2812B datasheet shows a threshold of >= 0.7VDD for logic HIGH, and <= 0.3VDD for logic LOW. The allowed VDD ranges from +3.5-5.3V. Interestingly, some WS2812Bs can actually work when powered by 3.3V, and driven by 3.3V logic, even though it’s out of spec, but many cannot. On the other hand, it’s totally within spec to be powered by 3.7V and driven by 3.3V logic. So, if you use a 3.7V LiPo battery to power the WS2812B strand, the WS2812B data line can be connected directly to the ESP8266 without any level shifting! If you choose to go this route, power the Huzzah from its VBat terminal, so that the 3.7V will be regulated down to 3.3V to power the ESP8266. More details are available in Adafruit’s NeoPixel Uber Guide.

Since I want to be able to drive long strands of LEDs, I elected to go the 5V power with level shifter route. Also, I have lots of 5V power supplies laying around. There are many different ways to do level shifting, either passive or active. The WS2812B has tight timing requirements, and runs at 800KHz, so care has to be taken in order to avoid signal distortion. One of the most reliable methods is to use a 74AHCT125 level shifter IC. I decided to first try a simple diode and pullup resistor circuit (credit: RPi GPIO Interface Circuits):

The circuit is currently working flawlessly for me, driving my 5m long strand of 150 LEDs.

WIFI COMMUNICATION PROTOCOL

In order to send data to our WS2812Bs over WiFi, we need some sort of IP protocol. Art-Net is a royalty-free protocol, which sends DMX data embedded in UDP packets. I decided to go with Art-Net because it is an industry standard that is supported by a variety of Pro software, and Jinx! and Glediator can talk to it.

ARDUINO FIRMWARE

I will not go into how to set up Arduino to compile sketches for the ESP8266, as that is discussed elsewhere. To compile for the Huzzah, select it as the compile target from the Tools pulldown menu:

Tools -> Board -> Adafruit HUZZAH ESP8266

I created a sketch, which is a mashup of a few different projects from github. The code is in my github repo: WS2812ArtNet. I stripped the Adafruit NeoPixel library down to the bare metal, and added a captive portal for configuring the WiFi connection. Also, it supports a hardware pin to erase the WiFi settings. Configuration is done via a few defines in WS2812ArtNet.ino. See the #defines for PIXEL_CNT, PIN_DATA, PIN_LED, and PIN_FACTORY_RESET. At a minimum, PIXEL_CNT must be set to the number of LEDs in your strand.

PIN_DATA is used to select the pin that’s used to drive the data to the LED strand.

PIN_LED is used to select the a pin which blinks an LED every time an Art-Net packet is received. This makes it easy to tell if the board is receiving data. In addition, the LED is initially off at boot-up, and turns solid red when the ESP8266 connects successfully to a WiFi AP. By default, PIN_LED = 0, which makes it control the onboard red LED on the Huzzah.

PIN_FACTORY_RESET wipes out any saved settings and clears the EEPROM when it’s grounded for 2 sec.

To load the WS2812ArtNet sketch into the ESP8266, first press the GPIO0 and Reset buttons simultaneously, and then let go of the Reset button. The red LED will then glow dimly, indicating that the bootloader is active. Once the sketch is loaded, when the ESP8266 initially boots up, it will create a WiFi AP with SSID WS2812ArtNet_hh-h. Use a computer, phone, etc to connect to the AP. Upon connection, it should automatically present a captive portal for configuration:

If the captive portal doesn’t automatically launch, open a web browser, and point it to http://192.168.4.1. Tap on Configure WiFi, and the ESP8266 will automatically scan for available APs:

Tap the desired AP’s SSID, and type in the passphrase. Additionally, you can also choose a starting Art-Net universe, and configure a static IP. After you tap save, the ESP8266 will reboot. If it connects successfully to your AP, the onboard red LED will light. Then, the LED strand will go into the startup test sequence of lighting up red, green, and blue, and then turning off. Once Art-Net data is received, the LED still start blinking with every packet it receives.  If you have trouble during setup, you can see debug messages by opening the ESP8266’s serial port in a terminal set to 115200,N,8,1.

 

When configuring Jinx!/Glediator, select GRB as the pixel data format.

 

Prev: WS2812B LED (NeoPixel) Control: Part 1 – Serial Control via 8-bit ATmega (Arduino)

WS2812B LED (NeoPixel) Control: Part 1 – Serial Control via 8-bit ATmega (Arduino)

INTRODUCTION

I’ve been laying the groundwork for doing some projects using remote controlled RGB LEDs. My first attempt was Lampduino, which used discrete RGB LEDs, and an ITEAD Colorduino as a controller. In that project, I ran into several pitfalls:

  • though inexpensive, assembly of the LED matrix was very labor intensive
  • the LED’s were rather dim, due to the limited drive capability of the Colorduino
  • the frame rate was slow, due to limited baud rate and RAM
  • while it was scalable, I didn’t like the idea of having to use a separate Colorduino for every 64 LEDs

A few years have passed, and WS2812B LEDs have dropped enough in price enough to get into the range that I feel is affordable. They can be found on eBay and AliExpress very cheaply. Also, they can be controlled without any specialized hardware – all that is needed is one GPIO pin. There are libraries available for many of the popular microcontrollers. Some examples are Arduino, ESP8266, Teensy 3.x, and Raspberry Pi.

HOST SOFTWARE

The other piece of the puzzle is control software. For Lampduino, I hacked uRaNGaTaNG’s mtXcontrol Processing sketch into rgbMtx, but I found Processing to be a very limiting platform, which was hard to debug. This time around, I found a couple of interesting free LED control programs, which are both quite powerful. The first one is Jinx! LED Matrix Control, which runs on Windows only, and the second is Glediator, which is a Java app. Both programs, while free, are not open source. However, they are both powerful enough to do some interesting things.

HARDWARE

I decided to start my experiments with an Arduino Pro Mini clone, because they have a tiny footprint, are cheap (clones are <$2 shipped from China on eBay), and I happened to have some laying around. Also, the Arduino Pro Mini’s ATmega 328P MCU runs at 5V, so no level shifting is required when interfacing to WS2812Bs. Glediator’s creator, Solderlab, has barebones, fast serial client Arduino sketch which can be downloaded at: WS2812-Glediator-Interface. It can run on any 8-bit ATmega-based Arduino such as the Mega, UNO, Deumilanove, etc. The code that outputs the data to the LEDs is written in assembly language, and is thus, very fast & compact. Also, rather than using Arduino’s Serial library, it contains its own very compact serial code. At the expense of a little bit of speed, I decided to generalize it a bit, and add my own packet protocol. My code is on github at: WS2812Remote. The main changes that I made in my version of the code are:

  • Since I didn’t understand the Glediator example’s serial code, I reverted to using Arduino’s built-in Serial library. I’ve tested it with baud rates up to 1000000 and an FTDI cable on a Windows PC, and it works fine
  • I added support for my own packet protocol. Glediator’s serial protocol is extremely simple. Each frame starts with 0x01, followed by the pixel data stream. My simple packet protocol adds an XOR check byte, as well as a few simple commands such as color fill and blanking of the LEDs.

I also wrote a C++ program, called pkt_test, which demonstrates usage of my packet protocol.

Hookup of the WS2812B LED strand to the Arduino is quite simple. You can use any 8-bit Arduino. First, you must select a data pin to drive the strand. I arbitrarily decided to use pin PD2. For speed and compactness, instead of using Arduino functions to access the data pin, the code refers to the ATmega port and pin numbers, rather than Arduino’s rather arbitrary digital pin numbers. On the Arduino Pro Mini, digital pin 2 = PD2, as can be seen from the following pin mapping diagram:

FIRMWARE CONFIGURATION

So WS2812Remote.h is configured as follows:

#define DATA_PORT PORTD
#define DATA_DDR DDRD
#define DATA_PIN 2

Next, PIXEL_CNT needs to be set to the number of LEDs in your strand. I tested with an Adafruit NeoPixel ring containing 16 LEDs:

#define PIXEL_CNT   16

Connect your the data input pin of the first LED of your WS2812B strand to your selected data pin. Adafruit recommends a 300-500 ohm inline resistor to protect from voltage spikes. The NeoPixel ring I used already has a resistor onboard, so I didn’t need it. I connected the +5V and GND pins directly to the corresponding pins on the Arduino. To protect against current inrush when powering it up, Adafruit also recommends connecting a 100uf capacitor between the +5V and GND pins. However, it’s not necessary if you’re just going to power it from USB, which is what I did, since I was only powering 16 LEDs. For large strands, you will need an external power supply to supply sufficient current, as each LED can draw up to 60mA at full brightness. If using an external power supply, make sure to always apply power to the WS2812B strand before the data pin!

HOST SOFTWARE CONFIGURATION

I used the same FTDI cable that I used for programming the Arduino as a virtual com port for sending data to it. When configuring Jinx! or Glediator, select Glediator protocol. For speed, the sketch just receives raw pixel data, and dumps it out to the LED strand, so the data format is in native GRB order.

When configuring Jinx! or Glediator, select Glediator as the device type (Jinx!) or output mode (Glediator). Make sure that the baud rate of the corresponding com port matches BAUD_RATE as defined in your sketch. I tested 115200 and 1000000 bps with my FTDI cable, and both worked fine with both programs. It failed at 1250000 bps.

If you want to play around with my packet protocol, the pkt_test code is self explanatory. I tested it with Visual Studio 2015 in Windows 8.1, and g++ in Debian linux 8.2.0. Prior to compilation, set COMM_PORT to correspond to your Arduino’s serial port. Also, confirm that BAUD_RATE in ../WS2812Remote.h matches the value that was used when loading the Arduino sketch. To compile and run pkt_test in linux, use:

g++ pkt_test.cpp serialib.cpp -o pkt_test
sudo ./pkt_test

WIRELESS CONTROL

My first inclination for wireless control was to use Bluetooth, due to its simplicity. The Bluetooth SPP (Serial Port Profile) makes it easy to construct a wireless virtual serial interface between a host computer and the Arduino. This allows you to use exactly the same host software configuration that you would for a direct serial connection to the host. I had an Elechouse EHB Serial Bluetooth Module in my parts bin (very similar to the ubiquitous HC-05), so I decided to try it out.

Before using the EHB module can be used, it must be configured with a series of simple AT commands. I hooked it up to my Windows PC with my FTDI cable, and used PuTTY as a serial terminal to configure it. Connecting the EHB module to the Arduino is quite straightforward:

Arduino -> EHB

5v -> VCC
GND -> GND
RXD -> TXD
TXD -> RXD

Pairing the EHB to a host computer creates a virtual serial port for the host software to access. Unfortunately, I was using Windows 8.1 as my host computer, and its handling of Bluetooth SPP clients is rather flaky. Every time I powered down the LED controller, I had to unpair/pair the Bluetooth in order to get the virtual serial port to work properly. While it worked flawlessly when the virtual serial port was functional, ultimately, I abandoned Bluetooth due to the flakiness of Windows’ Bluetooth SPP support. Perhaps Linux can handle it better.

Next: WS2812B LED (NeoPixel) Control: Part 2 – WiFi Control via ARTnet on ESP8266

HowTo: Fix AVRDUDE 6.3/Arduino 1.6.10+ Compatibility Issues with USBasp Clones

I recently upgraded to Arduino 1.6.13, and found that I could no longer program my boards with my Chinese USBasp clone programmer. When the Arduino IDE tried to load the firmware with my USBasp, AVRDUDE couldn’t find my USBasp, and gave this error:

avrdude: error: could not find USB device with vid=0x16c0 pid=0x5dc vendor=’www.fischl.de’ product=’USBasp’

It turns out that the that AVRDUDE 6.3, which is bundled with Arduino 1.6.10+, has timing issues with USBasps. The fix is to replace your libUSB-win32 driver with libusbK v3.0.7.0. An easy way to install libusbK v3.0.7.0 is to use zadig. Download the zadig from

http://zadig.akeo.ie/

Plug your USBasp into your PC.

Launch zadig, and from the menubar, select Options->List All Devices

Next, from the top listbox, select USBasp.

From the Driver selector box, click the up or down arrow key until libusbK (v3.0.7.0) appears.

Finally, click the Replace Driver button.

The screen should look like this:

zadig

You do not have to reboot or disconnect/reconnect your USBasp. After Zadig finishes installing libusbK, AVRDUDE 6.3 will start working correctly with your USBasp.

NOTE: the version of AVRDUDE that Arduino 1.6.x uses is actually controlled by the Boards Manager (Tools->Board->Boards Manager). Even if you have a version of Arduino 1.6.x prior to 1.6.10, if your Arduino AVR Boards by Arduino is version 1.6.10+, it will use AVRDUDE 6.3.