Category Archives: AVR

LUFA & Printf on XMEGA

LUFA, my favorite XMEGA USB driver also supports controling the serial ports. And connecting streams to printf, which makes for a happy, if lazy, embedded programmer. I just couldn’t figure out how to wire it up.

Turns out I was calling fdev_set_udata too soon, it must be called after the stream is created. Which makes sense. Perhaps I need to check the caffiene levels in my coffee.

Example:

Serial_Init(&USARTC0, 9600, false);

FILE stream;
Serial_CreateStream(&stream);
fdev_set_udata(&stream, &USARTC0);

fprintf(&stream, "Hello");

Power Meter 0.1.1

Built rev 0.1.1 of the power meter, at this point the initial round of firmware is complete and some hardware bugs have been fixed. The buzzer seems to work, but is not yet used as an indicator. All 3 INA219 sensors are communicating though the I2C isolator, and the Xmega is performing the math to send current and voltages to the host over usb (CDC Device Class).

This marks the midway point of this project. The goal was to get build a device that could measure current and voltage, with select-able settings for high range or high precision measurements of small currents, with electrical isolation from the USB interface. This board meets the requirements, at the expense of being a bit overdesigned. A lower pin count xmega could have been used, as well as potentially a single INA219 and a rotary selector. However… I am a fan of over-design, and the prototype as built can measure 3 different loads (at different max current/precision, all must share common ground) at the same time.

I have been pretty happy with the XMega so far, I should have switched off of the megas sooner. Next project should probably involve a STMicro arm, they are a lot faster and cheaper, and seem to have comparable peripherals…

IMG_20141109_231448

IMG_20141109_231439

Some additions need to be made to the firmware to finish this off. There is no way to send commands to start or stop a measurement, and the 2nd I2C bus that is intended for integration with other uCs is not used. I also need to be a able to notify the device which resistor the small current INA219 is using.

A Power Meter

Sent a new board off a week ago, a power meter based on the INA219, a high side current meter from TI. I have been wanting a way to log power consumption to my computer for a while, and wanted a more complete solution (and single board) than a dev board + current sensor. It seemed the usual hobby suspects sold analog and I2C current sensors, but not a good USB datalogger to go with it.

board

I put 3 on a board with 4 current set resistors (.01, .1, 1, and 10 ohm). The 1 and 10 ohm resistors are switched using a jumper. This gives the board range from tens of amps down to microamps (but not at same time). It can log on all three channels. It would be nice to get some kind of mux and use 1 meter and autorange the current, but I didn’t think of an easy way to do that, and just wanted a board. Might look at how a Fluke meter does it for a future revision.

Interesting features of the meter are USB isolation and a speaker. The measurement side is isolated from the micro-controller and USB, which allows some protection if the device under test has a massive ground spike, or is reverse connected. The uC is bus powered with an LDO, the measurement side requires an external power supply.

The speaker will allow a tone to be plated proportional to the current, which might be a nice hands off debugging feature. Even though the main purpose of the meter is power logging, having some feedback when a load is changing or cut out might be nice.

I’m using an atxmega32a4u for the controller. I think I will use this chip series from now on, the advantages are just too much. Fewer parts, more peripherals, twice as fast. I started writing the code for the DAC, it looks like I can get DMA working such that no CPU intervention will be required to drive the DAC, everything can be event driven.

I probably will just have the board stream ascii data at first, I don’t feel like building a command interface at the moment. Adding jumpers to mute the speaker and boot to boot-loader should be good enough.

DHT22, an easy to use humidity sensor

Got a DHT22 humidity sensor recently, was pretty easy to get up and running.

Since moving to the new place i’d been having issues with sinus headaches, probably due to low humidity. I got a cheapo humidifier, but it did not do so well.

I finally got an industrial strength humidifier, a CM-6009, and wanted to both see what the humidity is, and how well the humidifier is working.

Busted out my logic analyser for pretty much the first time ever, the timing was pretty close to as indicated in the datasheet.

dht22_cap

Zooming in, the short high pulses are ~25us and the long high pulses are ~72.4us. The data sheet claims that the short pulses should be 26us-28us and the long pulses should be 80us, so these are a bit fast actually.
dht22_cap_zoom

I used some really simple busy wait code to time the duration of the high periods (using loop_until_bit_is_set / loop_until_bit_clear), which works well enough. There is already publicly available code to use interrupt based methods that would be better.

For a first test I turned off my humidifier, and you can see the slow drop in RH over the next hour, with some sharper drops roughly tied to when the AC turns on. I also quickly learned my humidifier is set way higher than I thought it was… 70% is a bit high.

rh

t

Looks like AC is my main problem, it is continuing to get drier. Once I see how low it drops I’ll turn the humidifier back on to get the recovery rate.

New Project: Blind Opener

As if i wasn’t busy enough….

But I have been wanting this for a while. Something to close the blinds at night and open them around 8am so I actually will wake up and get into work before noon. maybe. (i’m pretty light sensitive)

Current thoughts are taking one of the l298 boards, adding some code to keep a timer / set the current time, and just let it free run after that. (presumably would need to reset it after a while or when the power dies, I don’t see the need to add a RTC, although integrating a SPI one should not be too hard.

Might sketch this out. Main concern is where to get a long flex joint from the motor to the blind control stick thing, and the lack of any encoders or stop feedback. Was planning to just do it timer based for now since /some/ overrun on either end won’t break anything, otherwise would need to buy a stepper or encoders. In theory the l298 can drive steppers by running the 2 bridges out of phase.

L298 rev .6.2

Another update, despite the small increment this board has a lot of changes. Most noticeable is the switch to a digital isolator by Silicon Laboratories, which reduces part count and cost by about 3$/unit. Moved to micro usb too, which is nice since it seems to be more common. Was able to get some samples of both the isolator and usb connector from SI and FCI respectively, thanks!

Also worked out some software issues and got the LUFA HID bootloader working. I wanted to use the CDC bootloader so vanila avr dude could be used, but I was not able to get the code down below 4096 bytes without much sacrifice – despite removing about 400 bytes from the program the last hundred or so needed was going to be a lot more work. The HID bootloader is much smaller, and fits well in a 2k bootloader section, giving me 6k to work with for the user program, which is more than enough. I should have about another 1500 bytes to add more functionality.

  • Digital isolators replacing optical isolators
  • Somewhat better routing, pads
    • larger pads for mcu & oscilator should help asm
  • Support for LUFA’s HID bootloader
    • the reset command ‘r’ now restarts to the bootloader
    • currently no HW bootloader jumper – only software bootloader start
  • micro-usb header vs mini
    • matches ras pi
    • seems more common anyway
    • easier to solder – has guide holes
  • 3 mount holes at corners

Still much to do, haven’t tested the isolator yet. I think i need to pick new caps too, pretty sure I have been getting 10v max ones when i intend for the high side to work up to ~28V.

Here is a shot of the PCB and the in progress board, partly populated as i was working on mcu code:

IMG_20130223_195116IMG_20130225_001533