LeafCAN v2 Firmware in Alpha Test

I have been working on v2 of the LeafCAN firmware, which adds a whole slew of new screens, selectable via a rotary encoder.  The rotary encoder is connected to the AD0/1/2 pins on the expansion header of the LeafCAN V2 hardware.  The code is currently in alpha testing, and is available in the development branch of the LeafCAN github repository.  Be aware that the development branch is for my bleeding edge code, and at any time, the code there may be broken, as I continually checkpoint my code.  I will move it to the master branch when it’s ready to be released.

While I was developing the LeafCAN v2 firmware, I received a pleasant surprise in the mail from Barbouri (GregC) of the MyNissanLeaf forum.  He designed a PCB with 16×2 OLED display + RGB Led Rotary Encoder support,


and sent me a completely assembled and tested rig.  I immediately added support for this new hardware variant into my working LeafCAN v2 firmware code.  The RGB knob is cool:


but I am still pondering how best to use it. Currently, I have it blue when the car is idle, red when it’s consuming power, and green during regen.
Below is an overview of LeafCAN v2Alpha3. The various screens are selected by rotating the encoder knob. Some of the screens have different modes, selected via a press of the encoder knob. The first screen is the familiar info screen from LeafCAN v1.3:


The top line from left to right is: kWh remaining/gids/fixed fuel bars, and the bottom line is: pack voltage/SOC%/instantaneous kW. The next screen is an idea lifted from Turbo3’s WattsLeft, the DTE (distance to event) screen:


The top line shows various miles/kWh values, 2.0/3.0/4.0/5.0/6.0, and bottom line shows the distance in miles to the event, in this case, Low Battery.  Pressing the encoder button switches it to miles until Very Low Battery:


and pressing the button a third time shows miles until Turtle:


Thanks to a breakthrough in active can sampling, spearheaded by GregH and TickTock, I was able to implement the following new screens. The first one has on the top line, High Precision State of Charge (SOC)%.  The bottom line shows State of Charge (Ah), and possibly a Battery Health %.


The next screen shows the 4 battery pack temperature sensors:


The units are selectable between Celcius and Fahrenheit with a press of the button. Finally, the last screen shows the minimum and maximum cell-pair voltages in mV, as well as their difference:


When an OLED is installed, the display now blanks after 5 sec of inactivity on the CAN bus. Pressing and holding the knob for a second wakes the display up for 5 sec.  When an LCD is installed, the press/hold turns on the backlight for 5 sec, instead.

I will be working towards finishing LeafCAN v2.0 in the coming weeks, and will announce its release here.

Barbouri and I are also collaborating on a dual-CAN bus version of the LeafCAN hardware, which will be able to monitor the Car-CAN as well as the EV-CAN on the Nissan Leaf. This will open up access to various information which is accessible only via the Car-CAN, such as friction brake actuation, steering angle, etc.

I would also like to point out that GregH has yet another cool Leaf CAN bus dash display in the works (only $80) that is worth checking out. Also, TickTock and garygid are working on the very fancy dual-touchscreen open-source CANary Project. Turbo3 has also figured out how to extract data from the Leaf Car-CAN using a cheap ELM-327 clone dongle and an Android phone. There is currently a flurry of CAN bus hacking on the Leaf.

AT90CAN Support for Arduino 1.0+

I have adapted the Arduino AT90CANxx support to work with Arduino 1.0+. Also, I have moved the code to github, so that it will be easier to update.  Note that the Arduino 1.0+ support is currently only alpha quality.  I have compiled a few sketches, but I also have found some which currently can’t compile.

You can always download the latest version at https://github.com/lincomatic/AT90CAN

It is easiest to download the whole repository as a zip file: https://github.com/lincomatic/AT90CAN/zipball/master

Follow the instructions in the README file.  For more information, see the post linked below.

Related Post: AVR CAN Bus Project – Step 1: Programming AT90CAN128 with Arduino

LeafCAN Hardware V2

Chris H built and sent me a revised board for LeafCAN. The V2 board puts all of the components on the back of the board, instead of facing the LCD.  By also eliminating all of the header pins (except for the ones used to connect the LCD), the assembly becomes considerably thinner than V1:

Below are my V2 and V1 meters side-by-side.  The V2 meter is on the left:

The new circuit allows software control of the backlight, so I changed the firmware to turn off the backlight 5 seconds after CAN bus activity stops, and turn it back on when the activity resumes.

I also added a new feature, which I call “fixed fuel bars.” Recently, I noticed that the 12-segment fuel bar display in the Leaf’s dash is misleading and inconsistent. Even as your battery degrades, it always displays 12 bars at 100% charge, regardless of how much energy your battery can store. Over the course of 10K miles and 1 year of driving, my range has been dropping. Lately, I’ve been caught off guard, almost running down to turtle on 2 occasions, on a 66 mile drive that used to be easy. My SOC at 100% has dropped from 270 to 255 (I never got 281, even when the car was new). Another thing is, the car often only has the range with 3 bars remaining that I used to get with 2 bars remaining. So, I decided to make a new fuel display, which directly links the SOC and “fuel.”  On my fuel scale, 13 = 281 gids, and the transition from 0 -> 1 is at 24 gids, which corresponds to Very Low Battery.  The number is displayed with 1 decimal point.  I put the zero point at Very Low Battery, because when the charge gets this low, we really need more precision, and should be using raw SOC values.  Unlike the fuel bar display in the dash, the first bar contains as much energy as the rest.

I find it more convenient to use this new fuel gauge than SOC %, because I can easily do the calculations in my head while driving.  When driving, I’ll count the number of miles for it to drop by 1, and then multiply it by the remaining count.  For example, saw the car goes 8.3 miles when the “fuel” drops from 11.1->10.1.” Then I estimate that I can comfortably drive 80 miles given the current driving conditions.

Worst case for me is about 5 miles per fuel point, so when I want a conservative estimate of range, and I know I’m not going to be climbing any long, steep hills, I can just multiply the fuel number * 5 to estimate range.

In the photo below, you can see I’ve rounded the corners on the new case, and the slimmer design looks nicer.  Also, I’ve switched to a 4-conductor white telephone cable, which is slimmer, and thin enough to squeeze in the gap between the left dash & the body.  I’m able to use only 4 conductors, because this build is hardcoded to use only the EV bus, so the only wires I need are power/ground/CANH/CANL.

The top line displays:  remaining pack KWh/SOC in gids/remaining fuel

The bottom line displays: pack voltage/instantaneous amps/instantaneous power draw KW (pack voltage * amps / 1000).


As usual, the latest code is available on github, and you are welcome to modify the display code to suit your own tastes.

Cheap 16×2 LCD Comparison

HD44780-compatible 16×2 LCD’s are cheap (<$4), and easy to use with Arduino via the LiquidCrystal library. To reduce the pin count considerably, the Adafruit LCD backpack is a nice little add-on that converts them to work via a two-wire I2C or SPI interface.

I bought two types to compare for use with OpenEVSE and LeafCAN. One has white lettering over a blue background, and the other one has black lettering over a yellow background. Here is an indoor comparison:

My camera can’t take good photos of the blue LCD indoors. The blue background is actually a good bit darker than in the photo, and the contrast is much better than it appears. Factoring that in, I guess I would still give the yellow LCD a slight edge indoors.

Now, here they are outdoors, in direct sunlight:

Again, the blue LCD didn’t photograph well. It looked slightly better than depicted in the photo. However, it’s a clear win for the yellow LCD. It works as a reflective LCD in direct sunlight, with very high contrast.

Case Protoype for LeafCAN

I’ve been having a lot of issues tweaking my latest Reprap design, so I haven’t been able print a case for my LeafCAN SOC meter.  Late last week, the printer was stabilizing, so I took a break to whip up something in OpenSCAD.  It’s just a basic box, which I printed in natural ABS.  I don’t like the sharp corners, so I’ve already redesigned it with rounded corners for the next rev.  This print was just to test out initial fitment.  This case is the absolutely smallest you can go, and still fit in all the electronics.
As you can see from the photo below, the header pins add considerable depth.

Also, in this build, I used female header pins to make the LCD detachable from the PCB, which added another centimeter to the depth of the case. The back has slots to engage various mounts:

Below, LeafCAN is clipped onto a suction cup mount, using the attachment slots:
One idea I have is to make vent clips, so allow it to be attached to either the center or left HVAC vents.  But so far, my favorite position is inside the left corner window:

In the location above, it’s up high enough to see easily, without obstructing the view out the windowshield, and the A-pillar helps to shield it from the sun.  The only problem is figuring out the mount.  I’m going to try a print a holder to allow me to suction cup it to the corner window next.

The natural ABS gives it a cool glow at night.  The photo below only shows the glow on the right, but it actually glows all around.

Although my vision is good enough that I can read the LCD comfortably, even in bright sunlight, I can understand how some people would find it hard to read.  I’m going to look into other displays, but at about $8, these 16×2 LCDs are hard to beat.

AVR CAN Bus Project: LeafCAN v1.2 Released

Another tweak today, I added instantaneous battery power consumption in KWh to the second line of the display. In order to make room, I had to do away with the V and A characters after the voltage and current draw. Here is the new display:

The top line, from left to right, contains Battery Pack Remaining KWh, SOC (“GIDs”), SOC % (GIDs/281*100), and Remaining Fuel Bars.
The bottom line now contains Battery Pack Voltage, Battery Pack Current Draw (Amps), and Battery Power Consumption (Pack Voltage * Pack Amps).

For users who prefer the old display, I added:

#define SHOW_KWH
#define SHOW_KW

If you comment out both lines, the display will be the same as v1.0, but with the smoothing that was added in v1.1.

As usual can download the latest release from github: https://github.com/lincomatic/LeafCAN/downloads.

Previous: AVR CAN Bus Project: LeafCAN v1.1 Released

AVR CAN Bus Project: LeafCAN v1.1 Released

I made some minor modifications to LeafCAN firmware today. First, I added display of remaining KWh in the battery pack, based on Phil Sadow’s formula of GIDs * 80 = Wh. Also, the display was a bit blurry when the values were updating rapidly, so I slowed down the refresh rate of the LCD to 250ms.

In order to make space for the KWh remaining value, I had to take out the % character in the SOC % display. Here is the new display:

The top line, from left to right, contains Battery Pack Remaining KWh, SOC (“GIDs”), SOC % (GIDs/281*100), and Remaining Fuel Bars.
The bottom line is unchanged, and contains Battery Pack Voltage, Battery Pack Current Draw (Amps).

You can download the latest release from github: https://github.com/lincomatic/LeafCAN/downloads. Note that I have added LeafCAN.hex, a precompiled file, for those who don’t have a working Arduino setup for compiling the code.

Previous: AVR CAN Bus Project: Step 4 – LeafCAN: Nissan Leaf SOC Meter
Next: AVR CAN Bus Project: LeafCAN v1.2 Released

OpenEVSE – Open Source J1772 EVSE (“Charging Station”) for Electric Vehicles

Last year, I purchased a Nissan Leaf, which is an all electric vehicle (EV). Although the Leaf comes with an included L1 EVSE (“trickle charger”), which connects to a regular 120V wall outlet, it takes way too long to charge the car. The more practical solution is to purchase and install a J1772 L2 EVSE which is equivalent to the commercial charging stations you see in public parking lots. The problem is the cost. J1772 L2 EVSE’s cost a couple of thousand dollars; the quote I got from Aerovironment, Nissan’s chosen installer was about $4000 including installation. Considering that modern EV’s using the J1772 standard have the charger built-in to the car, and that a J1772 EVSE is no more than a smart safety interlock plug for connecting the car to the wall, I thought the cost was pretty outrageous.

I obtained a copy of the SAE J1772-2010 standard, and after a few hours of perusing it, realized that a J1772 EVSE could be implemented with an Arduino, a few parts, and a relay at the fraction of the cost of the commercial products.  Furthermore, that the circuitry is so simple and compact that it could be built into a small case that fits into your hand, and can easily be thrown into your trunk, rather than the giant monstrosities like GE’s WattStation.

I started to design my own EVSE around an AVR MCU, but soon discovered that Chris Howell had had exactly the same idea, and posted about his Weekend EVSE project on the MyNissanLeaf forums.  Chris had already made major progress on the project, so I thought, why reinvent the wheel?  I contacted Chris, and we decided to team up to develop an inexpensive, portable L1/L2 J1772 EVSE. Chris is a wizard with PCB layout, and has shrunken the board down to smaller than an Arduino over several revisions. It’s truly a sight to behold how tiny and simple it is after seeing the giant messy hackorama that’s being funded by the DOE, the $5000 Blink EVSE, which is just an amalgam of OFF THE SHELF DEVICES IN THEIR ORIGINAL CASES: http://www.mynissanleaf.com/viewtopic.php?f=26&t=5664 . One thing that I find annoying about commercial EVSE’s is that they often waste too much power in standby mode. Nissan’s included EVSE draws about 3W in standby, which doesn’t sound like much, but the question is WHY? Vampire power wastes about 10% of the energy used by a typical residential household. I’ve heard that the Blink wastes more than 50W when it’s doing nothing. Not only that, many of the commercial offerings (including Nissan’s included L1 EVSE) aren’t even 100% J1772 compliant!

After many hours of tweaking and testing, we designed a 100% J1772 compliant dual mode L1/L2 EVSE.
This DIY EVSE has worked flawlessly charging my Leaf ever since last summer:

The actual EVSE logic board is the tiny PCB at the top with the 3 LED’s attached to it.  The switching power supply is on the bottom right, and a 30A relay is on the bottom left.  A CT in the upper right is used to detect ground faults.

For a portable unit to throw in my trunk, I need adjustable current, so I’ve implemented a 1-button menu interface using a common HD44780-compatible 16×2 LCD, connected to an Adafruit LCD backpack, which greatly reduces the pin count required for interfacing to it. I’m currently using it in i2c mode.

Note how small the controller PCB is now … Chris is a PCB layout ninja … yes, that’s a complete implementation of a J1772 EVSE on that tiny board.  Now, go back to the Blink EVSE link I listed above, and see how huge and complex the circuits are in the commercial offerings THAT AREN’T EVEN 100% compliant to the standard!

This time, instead of going into the expensive box I bought for the first EVSE, it’s going to live in a $5 NEMA wiring box from my local hardware store. I think this box will be a lot more durable against getting dropped and thrown around than my fancy $18 NEMA box.

Hardware hackers, what are you waiting for?  Join in on the fun.  We’ve open sourced the design as OpenEVSE:  http://code.google.com/p/open-evse/. Now, you can build your own fully hackable, customizable EVSE!

DISCLAIMER:  Don’t jump into this project unless you have a thorough understanding of the precautions which must be taken when dealing with high voltages and currents.  It’s quite easy to electrocute yourself and DIE if you don’t know what you’re doing.

AVR CAN Bus Project: Step 4 – LeafCAN: Nissan Leaf SOC Meter

I have implemented a SOC (State of Charge) meter for the Nissan Leaf. Many thanks to garygid and others from the MyNissanLeaf forums, for their help in decoding the Leaf CAN bus messages, and figuring out the pinouts.

The top line shows the SOC%, raw SOC value, and number of charge bars displayed in the dash.
The second line shows battery pack voltage and current in amps.

You can download the Eagle CAD schematic and AVR (Arduino) code from github:  lincomatic / LeafCAN
The Eagle schematic uses the Sparkfun library.

The schematic shows how to implement the entire circuit, without using the Olimex AT90CAN128 breakout board.

My original intent was to make a small PCB that directly attached to the LCD, and put it into a small case, but I never got it past the breadboard stage.  Unfortunately, I have gotten busy with other projects, so I am probably not going to do further development on this device nor finish the PCB layout, unless there is a large amount of interest. However, feel free to adapt it as you wish for your own CAN bus projects. Do bear in mind, however, that the design is licensed via the GPL, so if you use it for a commercial project, you must openly share your design.

Previous: AVR CAN Bus Project – Step 3: CANspy CAN Bus Monitor
Next: LeafCAN v1.1 Released

AVR CAN Bus Project – Step 3: CANspy CAN Bus Monitor

Sorry for the delay in posting the circuit and schematics from my AVR CAN Bus Project – Status Update 1.  The circuit for interfacing the Olimex AT90CAN128 Header Board is incredibly simple, and only requires 3 components.

Parts List
(1) .1uF ceramic capacitor
(1) 10K resistor
(1) Microchip MCP2551 CAN transceiver


If you’re going to connect it to a Nissan Leaf, the car has 3 different CAN buses accessible via the OBD-II connector. The pinouts can be found on MyNissanLeaf.com in this thread: Leaf CANbus Decoding (Open Discussion)

To communicate with the AT90CAN128 header board from my PC, I connected a USB to serial converter to USART0: TXD0 (pin 3) and RXD0 (pin 2).

Arduino Sketch

Below is my CANSpy sketch for monitoring the CAN bus via the serial port, as depicted in my Status Update 1.

Download: CANspy.zip

To compile the sketch, follow the instructions in AVR CAN Bus Project – Step 1: Programming AT90CAN128 with Arduino.

In my next update, I’ll show how to implement a SOC (State of Charge) meter for the Leaf using a LCD display.
Previous: AVR CAN Bus Project – Status Update 1
Next: AVR CAN Bus Project: Step 4 – Nissan Leaf SOC Meter