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.

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.

Propeller driven Lego car.

Lego Technic is perfect for creating quick prototypes of pretty much anything, combine this with RC stuff and you can create really fun stuff. One simple example is the robot i describe in this post:
Sunday afternoon robot

My old Lego is usually stuffed away in a box in the basement, but when we have kids visiting we usually bring it up to the apartment. I suspect I’m the one enjoying it most and this time I built a RC lego car

Lego Propcar 1
From the side
Lego Propcar 2
The motor and propeller us used as a pusher
Lego Propcar 3
The battery, ESC and receiver is secured with a rubber band
Lego Propcar 4
I use a HXT900 servo to steer

After a few testruns I added a propeller protection bar to minimize the risk of running into something with the propeller.

Lego Propcar 5
Propeller protection

Runs great, I’ll probably update this post with a video when I’ve recorded one.

Battery bag for my E-Bike

Last weekend we borrowed a sewing machine to make new curtains for the apartment. When I was sewing the curtains I realized that It wouldn’t be very difficult to make a custom battery bag for my e-bike. I would like a bag that I could mount in the frame triangle and have room for 4 Turnigy 6S 5Ah batteries. Doing some measurements, calculations and drawing I came up with this design

Battery bag drawing
An outline of the battery bag, the red triangle is the frame, the blue rectangles are batteries and the gray rectangle is the controller mounted on the frame.

I went to the fabric store and found a black nylon fabric that had a PVC layer on one side. This should be fairly water resistant and I plan to spray it with some textile waterproofing spray as well. I was recommended to use a thread for furniture which is much stronger than ordinary thread as well. A couple of hours thinking, cutting, and sewing later:

Battery bag
I use some Depron in the bottom, and some foam in the two corners to protect the batteries and fill out the bag.
The zipper has a bit of fabric folding over it for water protection
Hole for cable
The hole for the cable is also waterproof since the side of the bag overlaps ~10 cm where the cable comes out.
I'm using the cable I made for the E-Puch. This cable is for two turnigy batteries in series and two in paralell with a 60A fuse. The LEGO part is just for size reference.
Everything fits nicely inside the bag

The bag fits perfect on the bike!

Battery bag on bike
Battery bag on bike
Battery bag in frame
Battery bag in frame

Pleas write a comment if you think I should make some kind of drawing and description on how to make a bag like this.

First testrun of the E-Puch

Today I’ve made the first test-run of the E-Puch. As I’ve written about before it is an old ICE moped that I’ve removed the engine and replaced with an electric BLDC outrunner.

Today, me and my brother threw everything together for a quick testrun just to see that everything works, and try out the performance.

E-Puch ready for testrun
E-Puch ready for testrun

I will soon write a post about the motor controller which I ordered from a guy named Lyen on the Endless Sphere forum. It is currently set to limit battery current around 40 A, but I think I could increase the current limit by 50% without any modifications. Which would give 50% more torque.

Controller temporarily mounted to the frame
Controller temporarily mounted to the frame

The motor is mounted on two 5 mm aluminum sheets that are bolted in the original motor holes in the frame.

Motor and motor mount
Motor and motor mount, I think I'll remove the axle on this side.

Except for the motor and motor sprocket the drivetrain is original.

Original chain with 10 tooth front sprocket and 43 tooth rear sprocket
Original chain with 10 tooth front sprocket and 43 tooth rear sprocket

I use 4 bricks of 6S 5 Ah Turnigy 20C batteries mounted in 12S2P configuration. For now these are in a plastic box on the rear end of the moped.

Temporary battery box
Temporary battery box

And at last a video of my father trying the moped.

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.

Sunday afternoon robot

I had nothing to do today so I copied hubbens creation from the Swedish electronics forum. It is a two wheeled radio controled vehicle based on just two servos.

For wheels I used two wheels from an old Lego Technics truck I got for christmas almost 20 years ago (The best christmas gift I’ve ever got BTW). The wheels were bolted to servo horns from two HXT900 servos.

Lego wheel
Lego Wheel
Servo Horn
Servo horn bolted on lego wheel
The servos are glued together and self adhesive velcro is glued to both sides

These servos are modified for continous rotation according to this guide.

Pretty simple:

  1. Open servo and remove the gears and potentiometer.
  2. Connect to your receiver and set your output to center. (or use another way to generate a 1.5 ms pulse every 20 ms)
  3. Adjust the potentiometer to the center until the motor stops and solder it so it can’t rotate any more.
  4. Remove the stop pins on the output gear with a sharp knife
  5. Sand the top of the potentiometer axis so the top gear spin easy on the axis.
  6. Re-assemble the servo

The servos are then glued back to back and some self adhesive velcro are mounted on both sides and the wheels are mounted.

The ideal would be to run both the receiver and the servos of a 2S LiPo but I only have 3S at home so I needed to use a Turnigy 8 V – 10 V 5A SBEC to power the servos and the receiver. Together with the DC/DC converter the über cheap Hobby King 2.4Ghz 6Ch receiver was mounted on the top of the servos.

Top of the vehicle, the Turnigy SBEC to the left and Hobbyking receiver to the right.

On the bottom I have a Turnigy 1000mAh 3S 20C Lipo which is a little to large for this project but it was the smallest one I had.

Turnigy 3S 1000 mAh LiPo

I used channel 1 and 2 and mixed them 100% with each other. Since the servos are mirrored they need to rotate in the same direction for a turn and in oposite directions to move forwards och backwards.

This is what it looked like when it was ready.


My next order from HobbyKing will include a smaller 2S battery. And maybe I’ll put together a small controller board for this to make it autonomous.

Electric MTB

My electric MTB. I forgot to mount the battery pack for the photoshoot.

I’ve got a 6 km bike ride to work, the route is mostly flat but there is a high bridge I need to pass and the wind is not your friend when biking westward in Gothenburg. Despite this it is quicker for me to go by bike than public transport. The problem is that I do not want to be all sweaty when i arrive to work.

The solution: Put a motor on the bike

My bike is an Crescent Balder 24 speed mountain bike with front suspension and aluminum frame. Here in Sweden there are some regulations limiting an E-Bike to 250 W of power and a maximum speed of 25 km/h. That is not enough for me but I need a drive train that is possible to limit when driving on public roads.

After several hours of reading at the Endless Sphere forum I settled for a Nine Continents conversion kit including a 2809 rear wheel motor, 48 V 27 A motor controller, twist throttle, regeneration break handles and some other parts needed for the conversion. This kit has what is called an Infineon controller (based on the Infineon XC846 chip) and is programmable and possible to limit within legal limits.

E-Bike controller
E-Bike controller mounted behind the saddle

This is a 12 MOSFET version of the controller with a somewhat splash proof aluminum case that i mounted on a luggage rack from Biltema. One of the reasons that i choose the Nine Continents kit was that someone wrote on the web that it was one least bad china made E-bike kits regarding weather resistance.

E-Bike kit delivered
E-Bike kit delivered

The motor comes already laced in a 26″ rim and has mounts for disc brakes and a 7-speed freewheel, the only thing missing is a tire.

The 2809 means that the stator and magnets are 28 mm wide and each stator tooth have 9 turns of wire wound around it. There are different configurations of this motor, for example 2807 which have 7 turns wind resulting in a faster motor. The 2809 has a top speed of 35 km/h, on flat ground with 48 V battery voltage, which is enough for me. The slower speed means that the motor will be more efficient than the faster one at slow speed, for example uphill or with strong headwinds.

To power the motor I use Lithium Polymer (LiPo) batteries made for electric RC airplanes. At the moment I have 4 Turnigy 5000 mAh 6S 20C LiPo Pack batteries with 6 cells @ 5.0 Ah each. These are connected together as two 12s1p (12 cells in series and one paralell) packs like the one below.

Battery Module
One of my two 12S1P battery packs. These can be connected in parallel as well for a 12S2P pack. The serial wiring contains a 30A automotive fuse and a Anderson SB50 connector.

But the two packs can also be paralleled for a 12S2P pack with twice the range. The serial/paralell harness is made out of HXT 4mm Gold Connector for the battery, 10 AWG (~5,27 mm²) wire, a 30 A automotive fuse and a Andersson SB50 connector.

12 cells, at 3,7 V nominal voltage, in series results in a battery voltage of 44,4 V. 2 cells, of 5,0 Ah in parallel results in a total 44,4 V 10 Ah battery or 444 Wh of energy. My plan is to, at a later stage, extend this to 3 packs in parallel to 666 Wh of energy. Commuting to and from work with the bike has shown a energy usage of ~12-14 Wh/km. Mostly dependent on the amount of pedaling from me and wind speed/direction.

The battery specs say that they will deliver 20C continusly which means 20 times the capacity. My current battery with 10 Ah capacity will hence deliver a maximum of 200 A @ 44.4 V which is almost 9 kW of power.

There are a couple of different types of batteries used for electric bikes LiPo (which I use) has the highest power- and energy-density. The downside with LiPo is that they can be quite dangerous if you mistreat them, for example overcharge or puncture them in which case they will explode in a ball of fire. There are other lithium chemistrys for example LiFePo4 which are more stable but have lower energy density and much lower power density. There exists LiPo batteries that can deliver up to 90C while I haven’t seen LiFePo4 batteries capable of more than 10C. There are nickel and lead based batteries as well but they belong to the last century IMHO. Maybe I’ll write a more in-depth post about batteries, battery management and chargers in the future.

If you have any sort of experience repairing bicycles the install is easily made in a couple of hours, the most difficult thing was that the dropout has to be filed a little to suite the large axle on the hub motor. Since all torque from the motor is transfered through the dropouts the axle is 14 mm instead of the ordinary 10 mm bike wheel axle. The hub motor axle is flat on two sides making it fit in the 10 mm dropouts. Since the bend radius on the non-flat sides is 2 mm larger than on a regular bike wheel the frame dropouts has to be filed to a bend radius of 7 mm for a good fit.

Original dropout and axle to the left, hub motor dropout and axle to the right.

Since an aluminum frame is not as strong as a steel fram I decided to use what is called a torque arm to strengthen the dropouts. When using a hub motor that delivers a considerable amount of torque to the small flat sides of the wheel axle this is a must if the dropouts should survive. For example a powerful hub motor on an aluminum front fork without torque arm could end bad if the motor manage to twist itself out of the dropouts and the wheel comes loose. The rear wheel is probably less dangerous but I still don’t want the bike to break.

Torque arm
Torque arm. Note that the rear derailleur isn't used.

In the picture above you can see that there is no wire to the rear derailleur. The bike is originally a 24-speed bike but the hub motor only has room for 7 sprockets. Since the gear lever was mounted on the handbrake lever on this bike the rear 7 gears had to leave room for the regenerative brake lever supplied with the conversion kit making this a 3-speed bike. The left gear and handbrake lever is still intact controlling the front derailleur and brake.

I have used the bike to commute to work for a couple of months now and it works perfect. I have some future upgrades I want to make:

  • Move controller to water bottle mount
  • Replace phase wires to 12 gauge
  • Create fiberglass battery box
  • Tidy up cabling

This time of the year the weather in Gothenburg usually not invite to biking so I’ll have the winter to perform these upgrades on the bike.

Output voltage adjustment on 240W Kingpower charger

Kingpower 240 W charger
Kingpower Charger

I week ago i bought a bulk charger for my E-Bike, the idea is to use this to charge the battery fast and then once a week or so use a slower balancing charger to assure that the cells are in balance. I bought the charger from www.bmsbattery.com and they set it up for your requirements.


This is a pretty common china made charger sold at various places under different names. I have seen names link Ping-charger and King Pan charger as well as Kingpower.

The charger uses a CC/CV (Constant Current/Constant Voltage) charging algorithm and i requested the charger to have the current set to 4 A and the voltage 49V with an Anderson SB50 connector for the battery and an European wall outlet contact. They got the current and the high voltage connector right but the voltage were set to 48.8 V and the battery connector was an Andersson Power Pole instead. I had a spare SB50 connector so that one was easy to fix

48.8 V for a 12 cell battery will result in 4.07 V per cell. Since this charger doesn´t monitor each cell independently a safety margin is required but that is a bit to much. I was aiming at somewhere between 4,10 V and 4,15 V per cell. I needed to set the voltage to somewhere above 49 V.

Four screws on the sides held the lid on and after exposing the PCB i was faced with 3 potentiometers.

Charger voltage adjustment
Three potentiometers where the voltage setting is identified.

I would have guessed that there should be one for voltage and one for current. Two of the potentiometers were located next to the current shunt so I guessed that they were for setting the current. Maybe one is for coarse and one for fine adjustment, if anyone knows please leave a comment. The third potentiometer were located below the output fuse holder and that one was my first guess for output voltage. With the charger powered up and a voltmeter connected to the output I tried the last potentiometer and I was right. I adjusted output voltage to 49,5 V resulting in 4,125 V per cell.

Be extremely careful while touching the insides of the powered up charger with a metal screwdriver!!!