Saturday, June 15, 2013

Battery Powered LCD Desk Clock

Most of the clocks I have built have been mains or USB powered, but I had a request to build some battery powered ones, so I have been researching into the options. My basic LCD design was fairly simple, using an ATMega328P with a DS1307 Real Time Clock and an HD44780 based LCD display. I've built quite a few like this, here is one of the prototypes of this arrangement with an Arduino Uno and an adafruit RTC module:
My target was a pair of AA batteries. Of those, the micro will run happily run at 3V. The LCD should work at 3V althrough it is a 5V model, but the RTC requires 5V and a separate backup battery. To keep cost, size and power drain down, I have removed the RTC from the equation. It's a shame to lose the battery backed clock option. To save having to fully set the date and time each time the batteries are changed, I set it to write the date to EEPROM once a day at midnight. That at least means the date doesn't need to be set. The previous versions all used an ATMega328P running from a 16MHz crystal, but firstly that uses a lot more power, and secondly, it's only rated for that speed at 5V. I looked at running the micro from a 32768Hz crystal, but that didn't work out too well as the LCD updates were very slow. I finally went for the option of using the internal 8MHz oscillator in the micro, divided down to 1MHz, but retaining the 32768Hz watch crystal for an internal counter. This has to be set by programming the fuses in the ATMega328P microcontroller. I used avrdude and a USB Tiny ISP to set these using the following command line:
avrdude -cusbtiny -pm328p -Ulfuse:w:0x22:m -Uhfuse:w:0xd2:m -Uefuse:w:0xff:m
This sets the fuses to 22:D2:FF, selecting the internal 8MHz oscillator as the clock source, and enabling divide by 8 to get 1MHz. It also enables clock out - more on that later. This can be tricky territory as you can end up setting the clock speed to the point it isn't running fast enough to re-program. See my previous article on Reviving an unprogrammable ATMega328P. However, once set, this did reduce the current draw quite a bit from the original version running at 16MHz. The next challenge was the LCD. I went though a number of options here, I started with an MC34063 as a step up switch mode regulator. The idea was to generate 5V from the 3V input. This worked fine, but required quite a few components and took up a fair amount of space. It was also running at about 10mA, which wasn't really an option on batteries.
Having ruled that out, I looked into other options. Since the micro could run at 3V, it was only the LCD that required 5V. The next step was to look at using a voltage doubler to power the LCD. The idea is to take a square wave signal and via a couple of diodes and a couple of capacitors generate a voltage twice that of the  input. In this case, I was looking at getting up to 6V from doubling 3V. There is diode drop to consider, although with the 1N5819 schottky diodes, it's only around 100mV. To get the square wave, I had ruled out the ATMega328P PWM outputs as I'd changed the internal clocks to count the seconds (or rather 16th's of a second), so the extra fuse setting I programmed was clock out. This send the 1MHz internal clock out of PB0 (pin 14, Arduino Digital Output 8). This is a bit higher than necessary, but it's effectively free. A bit of experimenting with values gave around 5V under the load of the LCD and we were in business. There were few components to fit, so I went for mounting the circuit on a pair of small pad boards, the micro and the LCD on the front board. The crystal fits under the microcontroller to protect it. The LCD connectors via a 14 way header.
The batteries and buttons went on the second board.
I used a short ribbon cable to join the two boards, and bolted it all together.
This was using a few mA and seemed to be keeping good time, and sat nicely on the desk.
One of the tasks for this was to show the day of the week. The trouble was, it was only 8 characters, so I had to be a bit creative with Wednesday and fit 'esday' into 4 user defined characters.
I used the same 'esday' characters to allow Tuesday to be fit in the centre:
I ran this for a couple of weeks and all seemed well. As I was building another couple of these, I had another look at driving the LCD. The HD44780 chip should be able to run off 3V, the only reason the display was blank was because the contrast couldn't be turned low enough, it was already down to 0V. Various data sheets show a negative voltage as being required for this, so I made a slight modification and turned the voltage doubler into a negative voltage generator. This then generated approximately -3V from the clock signal. This was a bit high, so a switched to using standard 1N4148 diodes and 100nF capactiors (in place of 10uF) to make it less good. This ended up about -2V under load. The LCD could then be driven direct from the 3V battery supply, and the only things on the charge pump was the contrast reference of the LCD. This reduced the current consumption to less than 1mA, so should double the battery life, by just shuffling around a few components.
So there it is, a neat little desk clock / calendar that should run for many months on a pair of AA batteries.
Update:
Dave Jones over at the EEVBlog has just posted some videos on charge pumps and negative voltage generators:



Wednesday, June 5, 2013

Enough Leafs to fill an autumn

They're multiplying! The three charging bays at work were full again today, three black Nissan Leafs. Is it 'Leafs', I don't think 'Leaves' is right, although I'm not sure there are normally enough of them in the same place for anyone to have thought about a collective noun.
Not quite all charging away, as the middle one didn't have a Type 2 charging lead, and only the post on the left had a domestic three pin socket for the 'emergency' charger supplied with the Leaf, the others have the faster Type 2 Mennekes socket. Mine (on the right) is using a type 2 cable which goes direct from the post to the car. The one on the left is using the 'emergency' charger.
I'm not keen on the box that hangs down about a foot from the plug, I'm not convinced that's going to last or be waterproof. I was hoping they would have gotten around to supplying a suitable type 2 cable with the new cars, but it appears not.
1,300 miles in and I'm still enjoying mine. I'm living with the range without a problem. A couple of occasions I've had to plan in advance when I was going to charge up, in order to make longer journeys, but nothing insurmountable. I'd still recommend one to anyone who drives less than 30 miles a day and has a place to charge up.

Sunday, May 26, 2013

Arduino revival with EPROM programmer

One of the errors you get from time to time when working with Arduino's in general is

avrdude: initialization failed, rc=-1
Double check connections and try again, or use -F to override this check.

This error covers a multitude of sins. 99% of the time it is due to the chip not being connected to the programmer correctly for whatever reason. Either it is missing, badly inserted or there is a wiring problem between the chip and the 6 pin header. To fix those sort of problem, check the wiring, and don't bother with -F, that could just lead to more trouble.
In this case, I'm in the other 1%. I know the wiring is correct because I was using a USBtinyISP plugged into an Arduino UNO board, and it had been working correctly. The error in this case is because I had been setting the fuses via avrdude on the commandline and I had programmed a duff set of fuses into the chip, setting a stupidly low clock rate. In this state, the ATMega328P wasn't able to handle interfacing with the programmer (but it was probably running in a low power mode which is what I was trying to acheive).

I tried various solutions to this, apparently adding -B10 (up to -B250) is meant to help by slowing the interface down, and indeed it did, a bit. It got through the chip ID, but failed on the next step. One thing I didn't get around to trying was injecting an external clock into the chip, which might have worked. However, I was keep to get on with the battery powered LCD clock I was working on (more about that later...), so put that chip to one side.
Later I remembered I'd bought a cheap 'universal programmer' from ebay in order to program some EPROMs. After the fun I'd had with the VIC20 repair, I had concluded I needed a new one. The MiniPro programmer seemed quite widely available and had decent reviews. It has a massive list of supported chips, including some neat features like testing 74 series logic chips and SRAM chips. It also had the option to program various microcontrollers. I wasn't planning on using it to program ATMega328Ps, as I normally work in the Arduino IDE, which has no support for this, but easy support for programming using the standard bootloader on an Arduino board, or with the USBtinyISP.
Firing up the software, it read the chip correctly and showed the duff fuse settings. I was able to set them back to something more reasonable and reprogram the chip. To make sure it was OK, I changed to a kown working ATMega328P and read that in, saving a copy for future reference. I then wrote the code and fuse settings from the working chip back to the dodgy one. That seemed to go alright, so I replaced the chip in the Arduino Uno board, loaded the IDE and programmed it with the blink example (what else).

The moral of the tale, when it says 'be careful when setting fuses', it means it!

Wednesday, May 1, 2013

ZX Spectrum USB Keyboard

Following on from the ZX81 USB keyboards I've been building, next comes the Spectrum. The Spectrum has a lot in common with it's predecessor, but there are a few differences to the keyboard. The main difference from a software point of view is that there are now two shift keys. What was the ZX81's shift key is now caps shift, and what was the . / , key is now symbol shift, and there are various symbols dotted around the keyboard in red.
From a hardware point of view, it uses the same 5x8 keyboard matrix giving 40 keys, but the two membrane tails are now at opposite ends of the keyboard, meaning it's not as neat as the ZX81 worked out. I've done a few of these in the past in a similar way to the original ZX81 keyboard, just with a long, thin board. Here I went for a thin strip to take the connections to the Arduino Leonardo.
Other than that, it was plain sailing, and I soon had a Sinclair ZX Spectrum USB Keyboard to add to the collection. Note that due to the internal layout, it doesn't look like there would be sufficient space for a Raspberry Pi, the Pi would be too tall. One option would be to remove the USB connector and the RJ45 Magjack, or alternatively, use a model A - even with 256MB, it's still 5,461 times more than the Spectrum originally had.
The ZX Spectrum USB keyboard (and the others) are now available to buy in the Tynemouth Software Etsy store, look out for more of these coming soon.


Saturday, April 27, 2013

Arduino Leonardo based ZX81 USB keyboard

Last year, I had a ZX81 case and wanted to turn it into a ZX81 USB keyboard. At the time the option I chose was initially developed on an Arduino Uno, and then built up onto a board using just the ATMega328P. I've build several versions of this now, many of which sat alongside Raspberry PI's inside their ZX81's and ZX Spectrum's, and I'd refined the circuit and the presentation a bit since the orignal.
A couple of days ago, I had a request for more information on the keyboard, and I mentioned that if I were doing it now, I would have looked at using an Arduino Leonardo. These are Ardunios based on the ATMega32U4, which has built in USB support, and there are libraries to turn the Leonardo into a USB Keyboard / Mouse etc. as part of the Arduino IDE.
So I got my self a Leonardo and tried out some of the samples and it seemed to work fine, so I adapted my original code to use the Leonardo USB Keyboard object and tried that out, and it worked remarkably well. One of the things I'd noticed about the Leonardo was that it was available to buy without the headers, and the IO pins were arranged in a way that I could get the 8 and 5 pin headers for the keyboard directly over IO pins 0-7 and 9-13.
So it was simply a case of soldering the keyboard connectors in place where the headers would have been.
Then installing it in the ZX81, on the underside of the top of the case.
And there it is, an incredibly simple way of making a ZX81 USB keyboard. There are 20 available IO pins on the Leonardo, so a similar approach could be used on most matrix keyboards, up to 100 keys. It should also work with any of the other ATMega32U4 based Arduino boards (such as the Micro).
The code is available below, in two versions. The first is the standard keyboard mapping, all the letters and numbers work as usual, and with shift held, the letters are upper-cased and the top row gives escape, ", #, arrow keys, !, and delete. The alternate version has the top acting like that by default and requires shift to get the numbers. This is the version I use to drive my myth tv frontend box, so I can navigate the menus without having to hold shift.

Creative Commons Licence

Wednesday, April 24, 2013

Nissan Leaf Review - Part 4: One Month On


I've now had the leaf for a month, and driven 500 miles in it. I really like it, yes there have been a few minor issues, but solving those has been quite interesting, and it's now just an automatic part of the daily ritual to plug in and charge.
I've only had to charge up at home a couple of times to make a longer journey, so that's the only time I've had to pay. All in, the 500 miles has cost me about £2. (plus £250 for the charging cable, and the inital cost of the car).

As you're driving the dashboard shows via a series of dots how much power is coming out of or going back into the batteries. Given such a clear indication of the way your driving affects how much energy you use makes it easier to make minor adjustments to the way you drive to save energy, and extend the range. Things such as thinking about when you accelerate and when you actually need to, and braking sooner to slow down more gradually, making more use of the regenerative breaking.

The main annoyance for me at the moment is a minor one, with all the instrumentation in the car and the carwings website and app, nowhere does it give the one figure that would be most useful, the actual percentage charge available. The estimates are always going to be off as they can only be based on assumptions the the type of road you're currently driving will remain the same for the next 80 miles. It won't, I know it won't, I know exactly what the roads will be like, so given the vital missing information, I would be able to get a more accurate estimate. As it is, the best I can get is either the 12 bar gague or the misleading 'percent' on the carwings website which jumps is steps rather than giving the actual figure. Hopefully they'll be able to do something about it in future.

In summary:

  • There were a few initial problems getting insurance and the correct charging cable, but those are soon forgotten about
  • It's a very nice car, roomy, well built, fast, responsive, quiet, comfortable and easy to drive
  • The range is comfortable around 60 miles driving normally (less that quoted, but perfectly workable)
  • There are probably an additional 10 or 20 miles available past where I take it to, so 80 miles tops
  • You can get a better range if you drive slower and more carefully
  • It should be fine for anyone who drives up to about 40 miles a day, and has the option to charge at home or at work
  • Once you're using it, although they are getting better, it's still wise to ignore the dashboard, app and website estimates, and just count the bars on the battery gauge
  • The battery is 24kWh, so in the UK, that's about £3 to charge from empty, so at 60-80 miles, that works out around 4p per mile if you charge at home, although many charging posts are currently free. My old diesel would do 10p per mile at best.

Here's to the next 35 months of my 3 year contract!

Saturday, April 6, 2013

Nissan Leaf Review - Part 3: Living with a Leaf

I've had the Leaf for a few weeks now, and I'm starting to get the hang of working with the range. The first thing to do it seems is ignore the estimates and the quoted ranged and just see what you get from normal driving. I'm getting about 60 - 80 miles, about half of what the estimates tell me. The range of the car is estimated differently in various places, but most are over 100, so what I am getting is a little disappointing. However, it's still perfectly usable.

It's a balance, if you want a car which is nice and easy to drive, fast and responsive, you will get a lower range. If you want higher range, you need to drive in ECO mode with the aircon off and don't accelerate too often or too hard, you may be able to get into the 100 miles + range. I've been enjoying the car and getting less range, but I know if I'm getting a bit low, I can just change my driving style a little and extend the range. So you get the best of both worlds, you can have the fast, responsive, enjoyable to drive car when you want it, and drop to the economic one if and when you need to. You don't get that choice on a normal car, it's one or the other.

The range estimates on the dashboard are getting better, it is currently showing I have 100% charge and a 75 mile range (although carwings - see photo lower down - still shows a generic average of 120-149 miles).
The one piece information I've been relying on is the charge gauge. This has twelve bars, and I'm working to the following rules of thumb for these:
  • 1 bar = 5 miles of my driving on my usual roads
  • 1 bar = 10 miles at a push in ECO mode with the aircon off and driving carefully
  • 1 bar = 30 minutes to recharge from a type 2 charging post (32A rated, limited to 18A by the onboard charger)
  • 1 bar = 45 minutes to recharge from 13A standard mains socket
  • 1 bar = 2kWh = 25p (if you're at home and paying) or free at most charging posts
So from that, it works out at 5p per mile (if you're paying), and it charges at 10 miles per hour. That's what I'm getting at the moment, I'll keep an eye on those readings over the coming weeks.
The carwings website shows a percentage of charge, which I think can be a little misleading as I think it only show percentages converted from the number of bars. I've been monitoring charging and I've only ever seen it change in 7-8% jumps, as if it can only show 1/12, 2/12, 3/12 etc. It's estimate is also a generic one, and not biased by your driving style, so shows the rather optimistic range of 149 miles with the air conditioning off. The car itself, as shown above, is now showing a more realistically attainable 75 mile range, although this varies a little to frequently when you're driving. If you're in a short 30 mph zone, the range shoots up just as fast as it falls when you leave the 30 zone, so it's difficult to keep track of. Hence, I'm using the bars.

At one point, I left the car with a charge shown on carwings as 50%. The next day, without having moved the car, it was showing 42%. I initially thought it had lost 8% somewhere! However, what was seen as a jump from 50 to 42 may have been a lot less as it dropped from 6 bars (50%) to 4 bars (42%). It may have just dropped slightly, just enough to drop down to the next bar. A little misleading initially, but now I think I know how it works, less of a concern.