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.

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.


Prusa Mendel printbed

I put together a heated printbed for my Reprap using three parallel 10 Ω 100 W power resistors. The resistors are screwed onto a 220 mm x 200 mm x 5 mm aluminum sheet. This will make the whole printbed a 3.3 Ω 300 W heating element.

Printbed power resistors
Three 100 W power resistors connected in parallel will act as a heating element.

On top of the sheet I will place a 220 mm x 220 mm sheet of glass to use as a print surface.

Top of printbed plate
Top of printbed plate

When printing the printbed should be between 60° C and 110° C depending on which plastic is used.

Since the main voltage used on the reprap is 12 V, maximum power that can be achieved is

\frac{U^2}{R} = \frac{12^2}{3.3} = 44 W

My guess is that it will take at least two eternities before it gets hot enough, but for a start I’ll use the printbed temperature logic built into the RAMPS controller card.

Later on I think I’ll build my own temperature controller with either a step-up or a separate power source with a higher voltage to get more power out of the printbed.

Modifying the Turnigy 80-100 Brushless Outrunner – Part 3

Continuing on: Modifying the Turnigy 80-100 Brushless Outrunner – Part 2

As I’ve written before I had problems getting the Turnigy motor to run in sensored mode using the modified cheap-o eBay controller. Just to se if It’s the controller that is the problem I’ve tried the motor with my e-bike controller.


It works perfect! Turns slow and have lots of starting torque. I didn’t try more than 15%-20% throttle and the battery was empty in my current meter so I could’t measure the current. I will try full throttle and measure the current as soon as I’ve got a new battery.

The motor looks much neater with the internal sensors than the external I think I’ll paint it black to match the black moped when I’ve epoxied the stator and not going to take it apart again.

Turnigy 80-100 with internal sensors
The motor looks much better with the sensors mounted internally

Compared to the externally mounted sensors

Sensors mounted in bracket
Sensors mounted in bracket

Modifying the Turnigy 80-100 Brushless Outrunner – Part 2

Continuing on: Modifying the Turnigy 80-100 Brushless Outrunner – Part 1

This is a picture of the rewound stator. As described un the previous post the stator is now wound for ~90 rpm/V using double 1.5 mm copper wire. To hold the windings in place I use some dabs of low temperature heat glue. This will most certainly melt if I would put 3 kW of power through the motor, but I will replace the heat glue with high-temp epoxy when I know that everything works.

Rewound stator
The stator has been rewound with double strands of 1.5 mm copper . The heat glue is just to hold the windings in place while testing. Before using the motor under heavy load, the heat glue will be replaced by epoxy.

The motor is wound as an Distributed LRK (DLRK) and terminated in Y-mode. Using the notation from the picture below, S1, S2 and S3 are connected to the motor controller and E1, E2 and E3 are soldered together inside the motor.

The DLRK winding scheme. I've connected E1, E2 and E3 together and use S1, S2 and S3 as phase wires for a Y-termination. The sensors are mounted between 1 & 2, 5 & 6 and 9 & 10.

In the previous post I used ATS177 sensord mounted in a bracket outside the motor. This didn’t work very well with the modified controller. This itme I will try SS441A hall sensors, which are more expensive but thats the sensor that is usually recommended on the Endless Sphere Forum. I will also try to mount the sensors inside the motor which I hope will  be more robust and better looking. The sensors are mounted between slot 1 & 2, 5 & 6 and 9 & 10. Which gives them 120° spacing. To hold them in place before applying epoxy I use the same low-temp heat glue and a little kapton tape.

Sensor in stator slot
The sensors is mounted between two teeth of the same phase on the motor, all three sensors are 120° apart. To hold them in place I applied a small dab of hot glue and some kapton tape (the brownish tape in the image)

When I tried this setup with the modified controller it still didn’t run in sensored mode which leads me to suspect that there may be something wrong with this controller. I didn’t have time to do any thorough investigations why but my next step will be to inspect the sensor signals on the oscilloscope and try the motor on my other controller which doesn’t run at all without sensors.

Continue to: Modifying the Turnigy 80-100 Brushless Outrunner – Part 3

Fried connector

Fried Connector
The contacts inside this Anderson SB50 connector vaporized the first time I connected it to the controller

When I connected the modified e-bike controller for the first time a little accident happened. The Anderson SB50 connectors I use for my bike are great since they are polar and you cannot connect the battery in reverse polarity. Of course you have to put the positive and negative terminal in on the right side of the connector housing first. I missed that with the result that i connected the controller in reverse.

Since the MOSFETs conduct from drain to source through the body diode this was practically short circuiting the battery and resulted in a big spark. It was a good thing that I had a 30 A fuse on the battery lead otherwise the connector would probably lock much worse than on the picture above. The controller survived as well, probably thanks to the fuse, otherwise i think $50 worth of MOSFETs would have released the magic smoke.