Sunday, March 8, 2009

Windows XP Build Script for Arduino

If you use an Arduino board with Windows XP...

If you like to code in C/C++ and have total control of compilation...

If you dislike GUI IDEs and prefer coding in <your favorite text editor> with a makefile...

A Windows XP Build Script is all you need!!!


Arduino (Show/Hide)

If you want to read more about Arduino, unhide this section.


The Arduino IDE and build process

The Arduino IDE seems to be tailored to smaller programs in which all codes can fit into one file. In my case, I'd like to have multiple files to keep things organized. I'd also want to have codes shared by two or more projects, so I have files in separate folders. The IDE seems to get confused by the file paths and it was not long until I found it too bothersome to get the projects to compile and link properly. I love the legendary makefile and command line tools after all.

The entire Arduino platform is actually a frontend of the WinAVR tools, which is essentially an ATmega128 extension of the GNU collection. So all I needed was to write a batch file that executes the proper commands to compile, link and download the program. Of course, this could be a difficult task, but it turned out that I wasn't the first one, as I found exactly what I wanted at the Arduino Playground.

The Windows command line build was written by Don Cross and is a build script that works seamlessly. Though, this tool comes in three batch file and was a little bit difficult to digest/customize at the first glance. Also, the tool assumes that all your code comes in one single file. These batch files weren't exactly what I wanted, but they did provide a good starting point!

THE Arduino build script

After some poking around, I wrote up a makefile-like build script. The script, along with an example project, can be downloaded here:

ArduinoBuildScript.zip

Below is a description of the build.bat build script:

1 Configs (Show/Hide)

2 Parse command line option (Show/Hide)

3 Prepare environment variables (Show/Hide)

4 Clean output files (Show/Hide)

5 Compile source codes (Show/Hide)

6 Link to elf file (Show/Hide)

7 Download to microcontroller (Show/Hide)


The Example project and Further customization

To run the example, simply hook up your Arduino board, edit the COM port in the script and execute buildExample.bat. The example demonstrates compiling and loading a multi-file project. Note that I made it include a 3rd party library called SoftwareSerial. The project doesn't really need it but I included it to demonstrate how you can simply provide a space delimited list to tell the build script to compile and link the 3rd party libraries.

NOTE: It is important that you execute the init() function at the beginning of your main() function. The init() function initializes the hardware of the board etc. Also, remember to include the WProgram.h header file whenever you use an Arduino function call (like Serial and digitalWrite).

This build script hopefully provides a good basis of a customized makefile for everyone. My original intention was to also code other features into the script, but I decided that there are too many possible features you can add and it's down to your choice of functionality. Without modifying any codes this script supports compiling and loading multiple source files in multiple folders.

With a little modification, you can also customize this script to do the following:

  • support more than 1 config with the same script (like a DEBUG preprocessor flag): You can add a command line option, then use an if statement to set the variables to respective values.
  • support more than 1 project: Similar to above, but use a command line option and an if statement to switch between sets of source files and output name. The project outputs will locate in its own folder so you can compile multiple projects without affecting each other.
  • save the build process console output to a file: change the BUILDLOG variable

... and lots of other features! Once you get familiar with the script, you should be able to tweak it according to your own need. So enjoy!

Wednesday, March 4, 2009

Mod an HP50G calculator to use rechargable lithium batteries

First let's start out with a short rant:

HP50G is an awesome calculator
; RPN is the greatest thing ever. After using RPN, I'm trying to enter stacks into a normal calculator. If you're an engineer and you haven't used an HP calculator, there's probably something wrong with you.

AAA Batteries are terrible;
they are usually more expensive than AA's in retail stores which makes no sense, when they have a third of the capacity vs AA's. I hate it when devices are powered by AAA's especially when they're higher drain devices. The only devices that should use AAA's are things like clocks, radios, and remote controls. Which motivated me to do this hack. The HP50G chews through AAA's really fast. I've been in more than one exams where it ran out of batteries half way. Therefore here's the goals of this hack:
  1. Replace AAA battery with Rechargeable lithium battery
  2. Improve battery life significantly
  3. Enable charging over USB
The HP50G already has a Mini-USB port which it can power itself off of, so it just makes sense to do this mod.

Materials:
I COULD design my own USB charging circuit and output regulation circuits and buy the optimal lithium cells. BUT that would take time and effort. A real smart person buys a product with all the features, and then cannabalize it. The perfect candidate for this is an external battery pack for the PSP available from DealExtreme. It includes all the components I want, all for less than $10 shipped! A keen observer will notice that DX advertises this as a "12000mAh" battery pack. This is partially BS ofcourse. What they mean is it has approximately 12000mWh of energy storage. When I opened it up, I found 2 Li-Po cells marked "103447R". A quick search on Google revealed that this is a 3.7V 1400mAh cell. The picture below shows the disassembled battery pack.
Now let's open up that HP50G:
There's an awesome tutorial on Youtube for this, so I won't write a whole thing on it. The Youtube video shows the guy using a suction cup to pull the LCD cover, but I just pried it carefully with a screw driver since I don't have suction cups laying around. Next, we will gut the HP50G case to make room for our new battery:
Lucky for us, the Li-Po cells that come with the external battery pack from deal extreme is the same thickness and height as AAA cells! however it is wider than 4 AAA cells, which means we need to gut some of the case away to fit it. There will be no exterior aesthetic damage since we will only be removing ribs and the battery holder; the area which needs to be gutted out is shown in the picture below.
We will also need a space for the PCB which has our charge regulation and output step up circuits, but lucky for us, there's a perfect spot near the bottom of the case (marked in the picture below) The PCB has an LED on it that indicates charging status, and it could be a good idea to drill a hole so that the LED can peek outside. The LED is actually really tall, so you will either need to re-solder it to have a shorter profile, or bend the legs or else the whole thing won't fit together in the end.



You will notice that I completely destroyed the battery holder and pulled out the battery contacts to avoid short circuiting anything. When you are doing this, you will also notice that the power wires from the battery holder is soldered on to a large capacitor. Since our PCB already have capacitors on them, the removal of this capacitor is optional; I left it there since its legs made it a lot easier to solder on the subsequent wires.

Putting it all together:
Put everything in their place. Wrap things in electrical or packaging tape to avoid shorting stuff. Pretty self explanatory so far. I used double sided tape to hold things in place but that's optional.
You will need to take the 5V output wires from the PCB and cut off the connector and strip off the black jacket. Solder the output wires onto the capacitor that's glued on to the case. Obviously black goes to where all the black wires go, but you'll need to solder the red wire on the leg that has the blue wire on it.

Getting USB power to charge your battery:
The USB ground is already connected through the common ground, so the only thing you have to do to get power from USB to charge your battery is to connect pin 5 of mini USB to the external power input of the PCB. Pin 5 of the mini USB is the pin that's closest to the IR emitters on the HP50G motherboard, and it is VERY hard to get to. There is a metal casing that covers the mini USB connector that does really nothing at all, so I used a plier to snap off one of the walls (be very careful!) thus exposing the mini USB pins. I tinned pin 5 with some solder, and tinned the tip of the wire, and soldered them together very carefully. I guess an alternative is to probe around for an easier to solder pad that connects to pin 5, but since I was comfortable with my soldering iron, I chose to solder it directly on the pin.

When everything's in its place, it should look like this:

If you did everything correct like me, you can simply close the case back up and everything will fit. Remember to solder the two red wires on the speaker(buzzer) back on to the motherboard when you close it back up!.

Results:
The calculator weighs a bit more but otherwise looks exactly the same. The LED sticks out the bottom for me which makes the calculator wobble. I will need to get a taller rubber feet to compensate for this.
When I plugged it into the USB port of my laptop, it immediately started charging. Great success! And ofcourse, I can use the calculator while it's charging as well. After charging for 6 hours, the LED finally turned green. I unplugged it from the USB, turned it on, and everything works the way it should :) The day is mine...

I haven't tested the battery life improvements, but theoretically it should be about 2X longer.
4XAAA Alkaline = 1.5V x 1000mAh x 4 = 6.0Wh
2X 1400mAh Li-Po = 3.7V x 1400mAh x 2 = 10.36Wh

Thursday, February 19, 2009

I2C EEPROM for the Arduino

The Arduino is a nifty little board as most of you probably know already. I was doing a school project on it and found that I needed some external storage. Now, there are a lot of storage options available. SD cards and EEPROM on the SPI bus seem to be a popular choice, but I couldn't sacrifice 4 I/O pins just for storage.

I found code and tutorial for using I2C EEPROMs on the Arduino which was perfect since it would only take up 2 pins (pin 4 and 5) and I could put up to 8 EEPROMs on a single bus. The problem was that the tutorial was in Portuguese, (and I found another one in Spanish) so I ended up just fumbling through it myself. For the rest of you, here's the English guide.

The EEPROM Chip that i used was AT24C256B which I bought from Digi-key for $1.43, but any 24XX series EEPROM should be the same. Sparkfun carries the 24LC256 for $1.95 if you don't want to pay for Digi-key's ridiculous shipping charges (after shipping, Sparkfun will be much cheaper). The ones I listed above are in DIP-8 package and provide you with 256Kbits of storage (32KBytes) but you can purchase these in different packages and capacities. Since the 24LC is cheaper, the rest of the tutorial will assume using 24LC.

Wiring it together is super easy. To save everyone the trouble, I'll lay it out simple and straight forward:

Pin 1 2 and 3 determine the address of the chip; I2C uses 7 bit addresses + 1 control bit. With the code available on the Arduino Playground link above, you don't have to worry about the control bit. The EEPROM chip's address in binary will be 1010XXX where XXX is Address 2, Address 1, and Address 0 in that order. For example, if Pin 1 2 3 are Low Low High in that order, the binary address will be 1010001, or Hex 0x50.

For simplicity, I connected them all to Ground, thus giving me address of 1010000, or hex 0x50. It is important to remember to pull SDA and SDL up to 5V using a 1K resistor since those pins aren't pulled up internally at either end. Failing to pull them up will result in garbage when communicating. The picture below shows how I wired up my EEPROM:



The last part is actually using the code in your software. Since the creator for the functions linked above hasn't created a .h file for his stuff, you will simply have to copy and paste the entire snippit to the beginning of your code. Click here to get the code from Arduino Playground.

The code includes four functions i2c_eeprom_write_byte, i2c_eeprom_write_page, i2c_eeprom_read_byte, and i2c_eeprom_read_buffer. They are self explanatory if you read the code; for example, to write the data byte "0xAB" to the 1st block (remember a 256kbit EEPROM has 32708 blocks available) of the EEPROM whose address is 0x50, you would use:

i2c_eeprom_write_byte( 0x50, 1, 0xAB );

And to read the data you would write:

temp = i2c_eeprom_read_byte( 0x50, 1 );

That's it for this simple tutorial, those who want more information can read the EEPROM's datasheet and the details of the code provided.

Update: I found another guy who has a tutorial (Although it is in Spanish) of this up in his blog. He even has a picture of it all wired up on the breadboard. Click the picture below to jump to his tutorial

Add an external wifi antenna Panasonic CF-R1 (Or any other laptop)

On my recent trip to Japan, I picked up a Panasonic Toughbook (or Let's note) CF-R1 for about 10,000 Yen, equivalent to $110 or so at the time. It is a beautiful 10" sub-note that sports a Pentium 3-M 800Mhz ULV Processsor and 256MB of RAM. The CPU is the new Tualatin core, so the performance should better than EEEPC's 600Mhz Celeron. The RAM is a bit shy, which means I'll be running a stripped down version of XP, or a light Linux distro.


Panasonic CF-R1

When I bought it, the laptop had very little wear on battery life, and only a bit of discolouration on its plastic palm rest. The biggest downer though, was the lack of a Wifi card. Understandable considering this is from 2002. The laptop did however, come with a mini-PCI slot, so I got a spare wifi card and decided to add this capability.

For those who do not have spare wifi cards laying around, they're very cheap. I found one on ebay for less than $6 shipped Here. I went with an old Intel 2100 since it had very good driver support on linux.

The next problem was, that the laptop had no built in antennas like most recent laptops. You can buy panasonic OEM ones that go on the LCD, but that just wouldn't be very ghetto. So just to be hardcore and ghetto, I decided to add an external antenna and an internal auxilary antenna.

A side lesson on antenna connectors: If you already know this, skip this section. Most antennas for wifi will have one of two connectors: RP-SMA, and u.fl. all internal laptop wifi cards I've seen use the u.fl connector, while most desktop cards and routers use the RP-SMA connector.



u.fl internal antenna

For the internal antenna, you can get one reasonably cheap on ebay Here but if you have a broken laptop, you can salvage it from there; the antenna itself is usually somewhere behind/around the LCD, and there is a pig tail cable that leads to your wifi card. it doesn't matter what shape it is, it should be small enough to just jam into free space available on your target laptop.


RP-SMA Antenna and u.fl to RP-SMA cable

Now the real fun, the external antenna. You can get these everywhere, ebay, old routers, desktip wifi cards, etc. As long as they're good for 2.4 Ghz, you're ok. I ripped one out of an old router, which luckily also had the RP-SMA connector I needed. Since most external antennas use RP-SMA, I will need a u.fl to RP-SMA cable, and make an RP-SMA port on my laptop. For those of you who can't salvage these parts, they are also very cheaply available. Get an RP-SMA antenna here, and a u.fl to RP-SMA cable here. Another option is to just get an external antenna that has an u.fl cable comming out of it, but if you do this, you can't take the antenna off.

Next, I had to find a place to mount the RP-SMA connector. Lucky for me, the modem port (RJ-11 port) was the perfect size, located right next to the mini-PCI, and I don't feel bad for ripping it out. Also, since it's recessed, the connector doesn't even jut out too much (some guys who modded their EEEPC with the RP-SMA had it comming out of a drilled hole, and it stuck out of the case a lot). So unless you have an odd attachment to your modem port, it is the perfect place for your new RP-SMA connector.

Putting the connector in place was simple as ripping out the old RJ-11 jack and epoxying the connector in to place. Be careful not to get epoxy on sensitive places since its dielectric properties may affect sensitive electronics. I used 5 minute epoxy, and it worked out perfectly. Just file/sand the plastic around the connector as needed to accommodate your antenna.

The result was very good. With only the internal antenna, I was picking up about 6 AP's at my university, but with the external antenna, I was able to pick up 31! The signal strength also increased from around -60dB to -30dB.


The RP-SMA connector in where the modem port used to be


The black wire goes to the internal antenna, the light brown wire loops and goes to the RP-SMA connector


CF-R1 with the external antenna attached

Update: Some people have questioned the need for such mods when internal antennas have pretty good gains. True for most laptops, but Toughbooks uses some magnesium alloy instead of plastic for the LCD back bezel (The front bezel and side chrome parts are plastic. So when I installed the internal antennas behind the LCD, the signal was quite poor. Most laptops have plastic bezels and alloy frame, which makes RF waves propagate easier through the bezel. The OEM antennas sold by Panasonic is designed to mount where the chrome plastic side piece is, since the plastic piece won't block the signal as much as the alloy back bezel.

Tuesday, February 17, 2009

Hello World!

Spent the past 2 days in attempt to switch my mobo from SATA to AHCI mode while avoiding a Win XP reinstall. Given that tons of online resources are readily availble, it seemed to be very promising and straight forward. Of course, I ended up killing the OS because driver problems, and in the end did a clean install anyway. Note to self: NEVER ever install XP under SATA mode...