E-Bike system – Part 2 – CAN Interface

The first part of the E-bike system will be a computer interface to connect to all the nodes using the CAN-bus. This will be used for diagnostics, parametrization and software flashing. This makes it the logical starting point before developing the other parts of the system.

There are several commercial tools for this. Of course I could have used one of them, but it’s more fun, and cheaper to build my own. The tool I’m used to, is CANalyzer from Vector which is extremely competent but also extremely expensive. CANalyzer woll be a big inspiration for the windows application I build to communicate with the hardware interface.

Windows application

The windows application will have some basic features for setting up a signal database and debug generic messages, but also some specific features for communication with the different parts of the E-bike system. The application is created in C# .NET using Microsofts free Community edition of Visual Studio

Hardware Interface

The hardware will be built around the STM32F042C6 processor which have built in crystal-less USB and a CAN-interface which makes for a very low part-count device. Apart from some LEDs and passives, its only a processor, can transceiver and voltage regulator. For low volume production the BoM cost of the v0.1 prototype is around $10.

I’ve already designed and built the first prototype hardware and ordered the boards from OSH Park.

USB2CAN PCB v0.1 top
Top view of the USB2CAN board prototype v0.1
USB2CAN PCB v0.1 bottom
Bottom view of the USB2CAN board prototype v0.1


The embedded firmware is built on top of the ChibiOS RTOS using ChibiStudio which is a great tool for getting started with ARM Cortex-M development. Functions for communicating with the USB and CAN hardware and task synchronization is already done in the RTOS with the corersponding HAL.

The first version of the firmware works fine for sending and receiving CAN messages. There is still some work on the bootloader and the windows application. But I’m far enough to move on to the next part of the e-bike system, the BMS.

E-Bike system – Part 1 – Introduction

I’m currently working on replacing the complete electric system on my E-bike. The new system will be completely DIY and consist of several parts connected by a CAN-bus. I will post about the progress here continuously.

The system will be divided into several parts, all of them controlled by a STM32-series ARM Cortex-M processor running ChibiOS RTOS. The image below shows a system overview.

Overview of E-Bike system
Overview of E-Bike system

Handlebar Control Unit [HCU]

The main node controlling the rest of the bike will be mounted on the handlebars. Later on I’m thinking about something with a ~4.3″ TFT screen. To begin with it’ll probably be something much simpler. The main tasks would be to sample the acceleration and braking request and forward to the motor controller. It will also controll and display status from the BMS

Motor controller [DCAC]

I will probably start with a VESC with a redesigned PCB more suitable for e-bike use with heftier power transistors. Perhaps I’ll develop my own software to this later on since I find motor control software very interesting.

Battery Management System [BMS]

The battery will be built of 12 cells long series of 18650 cells. To monitor, balance and protect these I will build a BMS around the Maxim MAX14920 chip. This is an analog front end for voltage level conversion and balance discharge of 12 cell batteries. A STM32F373 processor will handle the sampling, control SoC estimation and communication with the rest of the system. The BMS will be able to continue running after the rest of the bike is shut down to handle charging and balancing after a ride.

Voltage Stepdeown [DCDC]

To generate 12 V from the main battery for driving the other nodes and things like lights. To begin with this will probably be designed around an off-the-shelf switching regulator controlled with a hard wire from the BMS which should have its own power supply.

CAN interface [CAN2USB]

This will be the first part I develop, the first prototype is already built up and somewhat tested. I will write a longer post about this later on. The CAN interface will be used to be able to download software, parameters and to read diagnostics using a computer. There are comersialy available alternatives but how fun is that. I manage to get the BoM down to the $10-$15 range as well. The can interface is based on a STM32F042 processor with built-in crystal-less USB and a CAN transceiver chip. This will talk with a Windows application over a virtual com port. The Windows application is written in C# using the community edition of Visual Studio.


A “dumb” standard Lithium battery charger like the one I have for my previous E-bike could be used as a start, but it would be nice if the charger was connected to the CAN-bus and let the BMS handle the charge and current control.


The mechanics will be the same ase described in for example [these] posts. The difference is that the bike will be touched up quite a lot with both paint and parts.

Dual pulse battery spot welder

I recently bought 20 Sanyo NCR18650GA Lithium batteries. 12 of them will be used for developing a BMS for a upcoming E-bike project. From the other cells I will put together 2 battery packs for my bicycle lights, 4 cells each in 2s2p configuration.

This type of cells are usually spot welded together using thin nickel strips. I’ve read that many have been successful soldering the cells together but how fun is that when you can build a DIY battery spot welder.

Most designs that I have found is based on a SSR (Solid State Relay) controlled MOT (Microwave Oven Transformer) with the secondary replaced by few turns of heavy wire which converts mains voltage to high welding current. This doesn’t seem like a good solution since the current comes in 100 Hz pulses which makes welding energy control very difficult. I know some SSR can only break the current at the zero-crossing-point of the mains voltage resulting in a pulse time resolution of 10 ms. I don’t think this is good enough to get a consistent result.

There are also variants which discharge a large capacitor bank through a MOSFET which seems like it would give a much higher degree of control. I also found a similar design using a car starter battery instead of capacitors which seemed even more interesting.

I don’t have any spare car batteries, instead I’ll use some high power LiPo batteries. I have a pack of 4 Turnigy 6S 20C 5Ah batteris that I could connect in parallell. This will result in a 6s4p 20C 20Ah pack capable of delivering  ~20 V 400 A continuous. It will not have any problem delivering enough current for battery tab welding in short ~10 ms pulses.

The main focus of the build was to use as much parts from my junk-bin as possible.


The electrodes are built om 10 mm copper rods sharpened in one end and threaded with an M10 thread in the other. On the threaded end a 25 mm² welding cable are connected. To set off the welding pulse I have placed a small button on the top of one electrode.

Left hand electrode, a sharpened 10 mm copper rod
Right hand electrode with trigger placed for thumb activation


To switch the current I found six FDP8440 MOSFETs rated at 40 V with a very low RDSon of 2.2 mΩ. If the welding current reach 1200 A they will handle 200 A each resulting in ~90W losses. This will easily be handled for a few hundredths of a second every 10 s or so. Especially since they are mounted on a thick copper busbar.

The control circuit semi-temporarily built on a Veroboard
The control circuit semi-temporarily built on a Veroboard

The mosfets will be controlled by a Microchip TC4421 from a 8-bit PIC microcontroller. Haven’t used a 8-bit PIC in ages, nowdays i prefer ARM Cortex-M processors, mainly the STM32 and LPC series. Since I’m building this on a veroboard I needed a DIP-casing so I decided to use an ancient PIC16F648A from the junkbin. This processor was perfect for a quick job like this, extremely simple keeping the datasheet reeding to a minimum

Spot welder schematic
Spot welder schematic

It has an internal 4 MHz 1% oscillator which will work fine for this application since there are no asynchronous communication, and 1% precision of the pulse timing is more than enough, no need for an external crystal oscillator. The reset-pin can be turned off with the config bits and all I/O-pins on Port B have internal pull-ups saving me a few resistors.

To program the processor I used Microchip MPLAB X IDE, XC8 compiler, and a PICkit 2 programmer. I’ve never used MPLAB X before, but it worked rather well. This is the application firmware, perhaps a little bit overdone, but why not?

The pulse length is currently hard coded in the application, it’s easy enough to update. If needed i will later add a switch with a few presets. The microcontroller has no A/D-converter, otherwise a potentiometer for setting this would have been nice.

A 6 S LiPo used as a power source discharging in a 3,3 ohm power resistor.
A 6 S LiPo used as a power source discharging in a 3,3 ohm power resistor.

There is a lot to read about pulse welding online, essentially the first pulse is to break any oxide layers and the second performs most of the welding. I have set pulse 2 to 10 ms, and pulse 1 to 1,25 ms which works fine for 8 x 0,18 mm Nickel strips.

I haven’t received the nickel strips I ordered yet, so my first test subject was some pieces of box cutter blades. It worked perfectly, but perhaps I need to shorten the pulses a little with the much thinner nickel strips.

Top side of welded knife blades
Top side of welded knife blades
Bottom side of welded knife blades
Bottom side of welded knife blades

Future plans

I will use this to build a few batteries, if the design works well, I’ll probably make a new PCB with a display, rotary encoder and a more modern processor.

Hacking Viasat branded version of Netgear WN602 v2 wireless bridge

Viasat WN602 v2
Viasat WN602 v2

The Viaplay package of my satellite receiver contained two Netgear WN602 v2 wireless network bridges to connect the satellite box to my network. These are a Viasat branded version with custom software for easy setup. To get the wireless bridge working I just had to connect the network cables, power them up and press the WPS setup button.

My house will soon get a fiber internet connection, to prepare for that I’m installing a wired network instead in the house and these boxes became superfluous. Instead I was thinking of using these as access points to extend my wireless network. This should be possible with the original Netgear firmware, but the Viasat firmware did not expose any interface for changing settings or update firmware.

Opening up the device i found a pin header with 4 pins, and i know these units often expose a TTL-serial port. Pin #1 is marked with a white dot and I could measure 3.3 V between pin 1 & 4, and made the assumption that pin 2 & 3 was the Rx & Tx pins. Using an FTDI-based serial adapter with 3,3V TTL levels I detected by trial and error that I could get a console bye connecting the adapter Rx to pin 2 (Tx) and the adapter Tx to pin 3 (Rx). The communication settings should be 115200 baud, 8 databits, no parity and one stop bit, otherwise you would just receive garbage.

Serial port on PCB
The serial port is accessible on the PCB. Pinout is:
1. 3.3 V
2. Tx
3. Rx
4. GND

The console access was not very helpful since i needed the root password to perform anything useful, this password was most likely kept secret by Viasat. Directly after power up the bootloader, U-Boot, instead called the router WRN612, which seemed very similar. Some googling verified that the WN602 essentially is a WNR612 with the WAN port removed. U-Boot also prompted for a key-press together with a down counter to abort the normal boot and enter a command prompt for the bootloader:


Luckily U-Boot is rather well documented, from here, loading a new firmware should be easy.

My first choice would have been to install dd-wrt instead which is an open source firmware for routers. Sadly dd-wrt had no support for neither WN602 nor WNR612, the D-Link DIR-601 A1 would perhaps work since it is based on the exact same chips, and these units are usually very similar to the chipset manufacturer reference design. The main processor in these are an Atheros AR7240 and the wireles network chip is an Atheros  AR9285. I decided to try OpenWRT instead, which is a similar project that had an excellent wiki entry for WNR612 on which most of this guide is based.

To flash the router with Open WRT I did like this:

  1. Connect a serial console to the hardware serial port described above. I used a FTDI based USB to Serial adapter from Sparkfun together with PuTTY as a terminal application. Be sure not to connect the 3,3 V pin, GND, Rx and Tx is enough. Connecting the power pin could damage the router, serial adapter and/or your USB-port
  2. Power up the router and be ready to press the “any key” when the bootloader asks for it.
  3. When the boot have been interrupted and you are in the boootloader console enter:

    to disable flash write protection, and then:

    to activate a TFTP server waiting for firmware upload
  4. Enter a static IP address from the series on your computer and connect it to one of the network ports on the device.
  5. Use a TFTP client on your computer to upload the firmware to address, i used TFTP Utility to upload the file named openwrt-ar71xx-generic-wnr612v2-squashfs-factory.img from the openwrt firmware repository.

    TFTP Util
    TFTP Util


Firmware flashed successfully
Firmware flashed successfully

Now I can use both wireless bridge devices as wireless access points instead of putting them on a shelf somewhere and forget about them.

Puffy mobile phone battery

A while ago I decided it was time to replace my old smartphone. The final choice was between a Samsung Galaxy S4 and a HTC One. I’ve had two HTC phones before and liked them both very much but this time I decided to go for the Samsung instead. The main reason was to be able to easily replace the battery in the future.

After about four months the battery time started to be really bad, and the phone sometimes decided to reboot without reason. A quick googeling showed that this problem is quite common and easily distinguished by the battery being a little puffy.

I couldn’t find only the battery in stock at any local store but one had a new battery together with a charger in stock for the price of 299 SEK which is quite reasonable.

Battery with charger
Battery with charger

You could easily see the difference between the old and the new battery.

Comparison of old an new battery
Comparison of old an new battery

Putting the old battery in the charger I couldn’t even close it.

Charger cannot be closed
Charger cannot be closed

In one way I’m glad that i got the phone with the easily changeable battery, but I think it’s really bad of Samsung to use such a low quality battery that it is worthless after four months. HTC probably have to use better quality batteries in their HTC One since it is much more expensive to replace the battery on warranty.

Long time no see

It’s been a long time since I posted something here. The main reason is that I became a father in February but I’ve also had a lot going on at work.

Hopefully the number of posts will increase again in the near future. In mid December we are moving from our apartment into a house. The house will have room for both a small workshop and an electronics lab. I’m also hoping to start brewing my own beer which certainly would be worth documenting here. But first there is a few things needed to be done on the house.

More pictures to come in the end of November when we get the keys.

Quadrocopter update

I used to have a tuning rig for my quadrocopter but I forgot it at my parents place before I stopped working on it for more than a year and now the rig is gone. Now that I want to continue working on the quad I had to build a new tuning rig.

Quadrocopter tuning rig
I made a new tuning rig for the quadrocopter.
Close up on tuning rig.
A close-up on one of the arms holding the quadrocopter. A small piece of aluminum tube with an inner diameter of 8 mm is striped to the MDF. Inside a M8 threaded rod is inserted into the square bar of the quadrocopter and is secured with a M8 nut and washer.

It is a little larger and better looking than the last one and there is less friction when rotating the quad. This hopefully leads to a better tuned software. I managed to tune the angular rate mode this weekend, I think it will be flyable right now but I haven’t tested it yet due to bad weather. I’ll upload a video as soon as I have tried it outdoors.

This is what I have left to do with controller software:

  • Filter yaw gyro signal, it’s to noisy right now to use as a feedback signal for yaw control
  • Activate and tune feedback control for yaw
  • Tune pitch/roll control in angle mode
  • Activate battery voltage measurement, blink LEDs with different speed depending on battery voltage. (and install LEDs on the quad)
I also started to work on a v. 2.0 of the controller with a faster processor, more sensors, more outputs and more inputs.

Upgrading Turnigy 9x with er9x firmware

A couple of weeks ago I ordered a Turnigy 9x RC radio transmitter. This is a cheap but capable transmitter sold by HobbyKing. I’m very impressed by the quality of the hardware and the thing have more buttons, switches and channels than I’ll ever use, but I’m not really satisfied with the firmware.

Luckily there are a lot of custom firmwares for this radio and from what I’ve read, the one named er9x is currently the preferred one. Guides on how this update is done can be found on many places (for example here), this is how I did it:

I used a USBasp AVR programmer, these can be bought off eBay for a couple of dollars. Since i generally prefer PIC processors, this was nothing I had so I ordered one. But I guess you could use any programmer supported by AVRDUDE.

When you have your programmer, there is a software called eepe that is a specially made AVRDUDE frontend for programming er9x firmware. I downloaded and installed that program.

Then you need to connect your programmer to the processor, this is the hardest part, but not really that hard. First unscrew the six screws on the back of the radio. Then disconnect the cable hold the two halves of the radio together.

Disconnect this cable

after that you need to locate the connection points where the programmer should be connected. These are all around the processor in the lower part of the radio. NOTE: I have the v2 version of this radio, the connection points differ on the older v1 of this radio!

Connection points
These are the six connection points that needs to be connected to the programmer. Note that I have the v2 version of this radio, these could differ on older versions.

To connect the USBasp programmer I made a custom cable like this

Programming cable
The programming cable made from a 10 pole flat cable and a 2×5 connector.
AVR connector
These are the different signals in the 2×5 AVR programming connector, with the connector in the same orientation as on the image above.

Note that the leads 3, 6, 8 & 10 are cut off and not used.

I soldered the cable to the connection points and followed the instructions for eepe. When I was done, I left the soldered cable inside the radio if I want to flash it again someday.


The RepRap is printing!

The RepRap build is moving along I haven’t installed the heated bed yet since I need a 24 V power supply, capable of delivering 10 A, first. I found a small pack of four 200 x 200 mm mirrors at IKEA named Sörli that will be perfect for a heated build surface. But for now I will print on a cold printbed. I measured the build volume without risk of hitting anything, it’s around 180x180x100 mm, so the mirrors will be perfect!

For the first real print after installing the hotend I put kapton tape directly on the aluminum sheet. The print wouldn’t stick at all, the printer just smeared a large blob of heated plastic around. I’ve read that blue painters tape is the preferred surface when printing PLA without heated bed so I bought two different brands of tape, Tesa and Kip at the local Hornbach store.

Blue painters tape
Blue painters tape from Kip and Tesa

So far I have only used the KIP-tape and I would say it works great. I’ll come back with a comparison when I’ve tried them both. Most people recommend Scotch blue 2090 but I haven’t found a supplier for that brand here.

I ordered a 0.35 mm J-Head MK V-B hotend from www.hotends.com. Most of it was already assembled, I just needed to insert the resistor and thermistor and put some kapton tape on it, then I could mount it to the extruder. I’ve also ordered some thermal grease that I will put on the resistor and thermistor when it arrives, but for now, the printer works fine without it.

J-head MK V-B
J-head MK V-B mounted on Wades geared extruder

Here are some pictures on my first print, maybe you could guess what it is?

First print
First print, here I used honeycomb infill, which probably results in the strongest parts, but to achieve any print speed, you need something more sturdy than a Prusa Mendel because of all the small print head accelerations needed to create a honeycomb pattern. The printer was shaking like crazy!
Finished print
Finished and assembled print, the outer part is mounted with two ball bearings and can be moved by a servo.
Finised print 2
The same part from above. Servo cable neatly hidden in the aluminum tube.

When generating the g-code for the printer you can choose how much infill you like to have. This particular print have the outer 3 layers solid and the inside have a 40% rectilinear infill which saves plastic filament, print time and weight but the parts are still very strong. This infill pattern is not as demanding on the printer as the honeycomb.

This image shows how the parts look on the inside, the top print was aborted half way through.

I will soon write another post about the thing I am printing together with the STL-files and a complete BoM.

The RepRap is alive!

I have previously written about my RepRap build, now its starting to come together. The last part I need before i can make my first print is the hotend that I’ve ordered from www.hotends.com

I use a RAMPS 1.4 board that, I ordered from 3D Gadgets, to control the stepper motors. RAMPS is an Arduino based RepRap controller that you can flash with several different firmwares. My first try was Marlin which seems to work fine. Since I dont have any hotend and temperature sensor I had to make some firmware hacks to get the thing moving. But I did!

And some picture of how it looks right now.

Picture of my RepRap
My RepRap including motors and RAMPS controller. It’s just put together to see if it works.

The steppers are pretty small but I got them very cheap. Let’s hope the have enough power not to loose steps while printing. A worst case solution could be to crank up the current and put heatsinks on them, or just buy bigger ones.

The RAMPS (RepRap Arduino Mega Pololu Shield) 1.4 controller loaded with Marlin firmware.

RAMPS 1.4 have 5 Pololu stepper drivers to be able to control X, Y, Z, E0 & E1.I only use one extruder and the Z axis have two motors connected in paralell on one of the driver. Maybe I’ll change the firmware so I can use one driver for each motor instead, but I don’t think that is necessary right now since the load on the Z-axis is very low.

One of the stepper motors controlling the Z-axis
The motor controlling the Y-axis
The carriage and extruder moving in the X-axis
Each axis have a mechanical microswitch for endstop.

Right now I only have 3 endstops which is enough for the controller to find the home position (coordinate 0, 0, 0). It’s probably safer to have 6 endstops, one for each end of each axis, maybe I’ll add 3 more in the future.

The extruder which push the plastic filament through the hotend
Top view of the extruder, you could see the hobbed bolt inside which grips the filament

I use Wade’s Geared Extruder I hope my small stepper motors are strong enough for this extruder.

A 1 kg roll of white 3 mm PLA filament

I will start out with PLA plastic for my first prints, from what I’ve read I think ABS is more likely to warp because of uneven cooling. I think PLA will work great until I have my heatbed working.