In July 2018, after 6 years of careful observation while my disease rested in an indolent state, the lymphoma kicked into gear and started growing rapidly. I experienced an abnormal increase in generalized lymphadenopathy (beyond the level I had come to get used to as normal), and a CT revealed thoracic and axial lymph nodes which were significantly larger than typical. A PET CT revealed high metabolic activity in many chains of lymph nodes, indicating the disease had shifted in its behavior from indolent to aggressive.
On one hand it’s disappointing that the disease began progressing. On the other, it’s now in a state where it is more likely to respond to treatment. In August 2018 the decision was made to begin interventional treatment: chemotherapy to attack the actively-growing lymphoma, followed by an autologous stem cell transplant (treatment which includes very high doses of chemotherapy, total-body radiation, and a type of bone marrow transplant where I am both the donor and recipient) which may help prevent its recurrence in the future.
My first realization that something was changing in the status of my disease came while filming a YouTube video about FTDI microchips. I noticed the cervical lymph nodes on my left side were swollen, took a selfie to assess what they looked like, then kept shooting the video with my body turned in such a way as to minimize the notability of my left neck. For the record here’s the picture, and here’s the video. That picture was taken May 29 2018, and at my next doctor visit was 8 days later. The oncologist was concerned about the progression and ordered additional imaging (a CT) to get a better idea of what may be changing.
It’s been a few years since I’ve written about this, so here’s the summary of where I’ve been and where I am now: In April 2012 I had some alarming symptoms (lymphadenopathy, weight loss, night sweats) and visited an oncologist for the first time. This picture was taken just before my first doctor visit, and I’m in scrubs because I was a dental student at the time (I hadn’t started the DMD/PhD program yet). After several blood tests and two surgical biopsies I was eventually diagnosed with non-Hodgkin’s lymphoma (NHL). More specifically, Lennert’s Lymphoma, a rare lymphoepithelioid variant of peripheral T-cell lymphoma in the not otherwise specified category T-cell lymphomas.
Of interest is one of K. Lennert’s early publications from 1986 (there are earlier ones, but this one is open-access). Its title describes condition as “a monoclonal proliferation of helper T cells“, and in its text further characterizes it as “special variant of Hodgkin’s disease characterized by a high percentage of epithelioid cells and rarely containing the Reed-Sternberg cells characteristic of classical Hodgkin’s disease.”
The FT232 USB-to-serial converter is one of the most commonly-used methods of adding USB functionality to small projects, but recently I found that these chips are capable of sending more than just serial signals. With some creative programming, individual output pins can be big-banged to emulate a clock, data, and chip select line to control SPI devices. This post shares some of the techniques I use to bit-bang SPI with FTDI devices, and some of the perks (and quirks) of using FTDI chips to bit-bang data from a USB port. Code examples are available on GitHub, and links to additional resources are at the bottom of this post. After the final build I created a slightly more polished “ftdiDDS.exe” program to control an AD9850 frequency synthesizer from the command line by bit-banging a FT-232, and code (and binaries) are also available on GitHub.
The main reason I like using FTDI devices is because when you plug them in to a modern computer, they just start working! You don’t have to worry about drivers, driver versions, driver signing, third party drivers - most of the time it just does what it’s supposed to do with no complexity. If I’m going to build a prototype device for a client, a FT-232 USB to serial converter is the way to go because I can be confident that when they plug in, their device will start working right away. Yeah, there are third party drivers to get extra open-sourcey functionality from FTDI devices (libFTDI), but I don’t want to ask a client (with unknown tech-savviness) to install third-party unsigned drivers before plugging my device in (and heaven forbid the product doesn’t work in their hands and I have to ask them to verify the device is actually using the third-party drivers and not defaulting back to the official ones). In this project I seek to use only the generic, default, officially-supported FTDI driver and API access will be provided by libftd2xx. Don’t forget that USB ports supply 5V and GND, so in most cases you can power your project just from the USB port! All-in-all, the FT-232 is a great way to give a small device USB functionality. This post explores how to use it for more than just sending and receiving serial data…
The goal of this post is not to describe every detail about how to control FTDI chips. Instead, the key points of the software are described here (and in the video) so you can get the gist of the main concepts. If you’re interested in additional detail, full code examples are provided on the GitHub folder for this project. All code examples were tested with Visual Studio Community 2017, are written in C#, and uses the FTD2XX_NET library installed with NuGet. Also, see the list of resources (including official FTDI datasheets and application notes) at the bottom of this post.
This block of code attaches to FTDI device 0 (the first FTDI device it sees) and sends the letter “a” using a traditional serial protocol. Since this code connects to the first FTDI device it finds, this could be a problem if you have more than 1 FTDI device attached. Alternatively you could have your program connect to a specific FTDI device (e.g., by its serial number). To see what FTDI devices are attached to your computer (and see or set their serial numbers), use the FT_Prog application provided by FTDI. Also, see the code I use to list FTDI devices from inside a C# program ftdiDDS program.
Full code is on GitHub
public static FTDI ftdi = new FTDI();
public static FTDI.FT_STATUS ft_status = FTDI.FT_STATUS.FT_OK;
public static UInt32 bytesWritten = 0;
static void Main(string[] args)
{
ft_status = ftdi.OpenByIndex(0);
ft_status = ftdi.SetBaudRate(9600);
string data = "a";
ft_status = ftdi.Write(data, data.Length, ref bytesWritten);
}
__Here is a minimal complexity LED blink example. This code block alternates between writing 0 (all pins off) and 1 (TX pin high) over and over forever. __Note that ftdi.SetBitMode
is what frees the FTDI chip from sending serial data when ftdi.Write()
gets called. The 255 is a byte mask which tells all 8 pins to be outputs (by setting all 8 bits in the byte to 1, hence 255). Setting bit mode to 1 means we are using asynchronous bit bang bode (sufficient if we don’t intend to read any pin states). For full details about these (and other) bit-bang settings, check out the Bit Bang Modes for the FT232R application note.
Full code is on GitHub
ft_status = ftdi.OpenByIndex(0);
ft_status = ftdi.SetBitMode(255, 1);
ft_status = ftdi.SetBaudRate(9600);
int count = 0;
while (true)
{
byte[] data = { (byte)(count++%2) };
ft_status = ftdi.Write(data, data.Length, ref bytesWritten);
System.Threading.Thread.Sleep(100);
}
In reality all we want to send to SPI devices are a series of numbers which we can place in a byte array. These numbers are transmitted by pulling-low a clip select/enable line, setting a data line (high or low, one bit at a time) and sliding the clock line from low to high. At a high level we want a function to just take a byte array and bit-bang all the necessary SPI signals. At a low level, we need to set the state for every clock cycle, bit by bit, in every byte of the array. For simplify, I use a List<byte>
object to collect all my pin states. Then I convert it to an array right before sending it with ftdi.Write()
.
Full code is on GitHub
List<byte> bytesToSend = new List<byte>();
bytesToSend.Add(123); // just
bytesToSend.Add(111); // some
bytesToSend.Add(222); // test
bytesToSend.Add(012); // data
BitBangBytes(bytesToSend.ToArray());
// given a byte, return a List<byte> of pin states
public static List<byte> StatesFromByte(byte b)
{
List<byte> states = new List<byte>();
for (int i=0; i<8; i++)
{
byte dataState = (byte)((b >> (7-i)) & 1); // 1 if this bit is high
states.Add((byte)(pin_data * dataState)); // set data pin with clock low
states.Add((byte)(pin_data * dataState | pin_clock)); // pull clock high
}
return states;
}
// given a byte array, return a List<byte> of pin states
public static List<byte> StatesFromByte(byte[] b)
{
List<byte> states = new List<byte>();
foreach (byte singleByte in b)
states.AddRange(StatesFromByte(singleByte));
return states;
}
// bit-bang a byte array of pin states to the opened FTDI device
public static void BitBangBytes(byte[] bytesToSend)
{
List<byte> states = StatesFromByte(bytesToSend);
// pulse enable to clear what was there before
states.Insert(0, pin_enable);
states.Insert(0, 0);
// pulse enable to apply configuration
states.Add(pin_enable);
states.Add(0);
ft_status = ftdi.Write(states.ToArray(), states.Count, ref bytesWritten);
}
The AD9850 is a SPI-controlled DDS (Direct Digital Synthesizer) capable of generating sine waves up to 65 MHz and is available on breakout boards for around $20 on eBay and Amazon. It can be programmed with SPI by sending 40 bits (5 bytes), with the first 4 bytes being a frequency code (LSB first) and the last byte controls phase.
To calculate the code required for a specific frequency, multiply your frequency by 4,294,967,296 (2^32 - 1) then divide that number by the clock frequency (125,000,000). Using this formula, the code for 10 MHz is the integer 343,597,383. In binary it’s 10100011110101110000101000111, and since it has to be shifted in LSB first (with a total of 40 bits) that means we would send 11100010100001110101111000101000 followed by the control byte which can be all zeros. In C# using the functions we made above, this looks like the following.
Full code is on GitHub
int freqTarget = 12_345_678; // 12.345678 MHz
ulong freqCode = (ulong)freqTarget * (ulong)4_294_967_296;
ulong freqCrystal = 125_000_000;
freqCode = freqCode / freqCrystal;
bytesToSend.Add(ReverseBits((byte)((freqCode >> 00) & 0xFF))); // 1 LSB
bytesToSend.Add(ReverseBits((byte)((freqCode >> 08) & 0xFF))); // 2
bytesToSend.Add(ReverseBits((byte)((freqCode >> 16) & 0xFF))); // 3
bytesToSend.Add(ReverseBits((byte)((freqCode >> 24) & 0xFF))); // 4 MSB
bytesToSend.Add(0); // control byte
BitBangBytes(bytesToSend.ToArray());
If somebody wants to get fancy and create a quadrature sine wave synthesizer, one could do so with two AD9850 boards if they shared the same 125 MHz clock. The two crystals could be programmed to the same frequency, but separated in phase by 90º. This could be used for quadrature encoding/decoding of single sideband (SSB) radio signals. This method may be used to build a direct conversion radio receiver ideal for receiving CW signals while eliminating the undesired sideband. This technique is described here, here, and here.
Rather than hard-coding a frequency into the code, I allowed it to accept this information from command line arguments. I did the same for FTDI devices, allowing the program to scan/list all devices connected to the system. Now you can command a particular frequency right from the command line. I didn’t add additional arguments to control frequency sweep or phase control functionality, but it would be very straightforward if I ever decided to. I called this program “ftdiDDS.exe” and it is tested/working with the FT-232R and FT-232H, and likely supports other FTDI chips as well.
ftdiDDS -list
lists all available FTDI devices
ftdiDDS -mhz 12.34
sets frequency to 12.34 MHz
ftdiDDS -device 2 -mhz 12.34
specifically control device 2
ftdiDDS -sweep
sweep 0-50 MHz over 5 seconds
ftdiDDS -help
shows all options including a wiring diagram
Although my initial goal for this project was simply to figure out how to bit-bang FTDI pins (the AD9850 was a SPI device I just wanted to test the concept on), now that I have a command-line-controlled RF synthesizer I feel like it’s worth keeping! I threw it into an enclosure using my standard methods. I have to admit, the final build looks really nice. I’m still amused how simple it is.
There is a serious problem with the FT-232R that affects its bit-bang functionality, and it isn’t mentioned in the datasheet. I didn’t know about this problem, and it set me back years! I tried bit-banging a FT-232R several years ago and concluded it just didn’t work because the signal looked so bad. This week I learned it’s just a bug (present in every FT-232R) that almost nobody talks about!
Consider trying to blink a LED with { 0, 1, 0, 1, 0 } sent using ftdi.Write()
to the FT-232R. You would expect to see two pulses with a 50% duty. Bit-banging two pins like this { 0, 1, 2, 1, 2, 0 } one would expect the output to look like two square waves at 50% duty with opposite phase. This just… isn’t what we see on the FT-232R. The shifts are technically correct, but the timing is all over the place. The identical code, when run on a FT-232H, presents no timing problems - the output is a beautiful
The best way to demonstrate how “bad” the phase problem is when bit-banging the FT232R is seen when trying to send 50% duty square waves. In the photograph of my oscilloscope below, the yellow trace is supposed to be a “square wave with 50% duty” (ha!) and the lower trace is supposed to be a 50% duty square wave with half the frequency of the top (essentially what the output of the top trace would be if it were run through a flip-flop). The variability in pulse width is so crazy that initially I mistook this as 9600 baud serial data! Although the timing looks wacky, the actual shifts are technically correct, and the FT-232R can still be used to bit-bang SPI.
Unfortunately this unexpected behavior is not documented in the datasheet, but it is referenced in section 3.1.2 of the TN_120 FT232R Errate Technical Note where it says “The output may be clocked out at different speeds … and can result in the pulse widths varying unexpectedly on the output.” Their suggested solution (I’ll let you read it yourself) is a bit comical. It’s essentially says “to get a 50% duty square wave, send a 0 a bunch of times then a 1 the same number of times”. I actually tried this, and it is only square-like when you send each state about 1000 times. The data gets shifted out 1000 times slower, but if you’re in a pinch (demanding squarer waves and don’t mind the decreased speed) I guess it could work. Alternatively, just use an FT-232H.
Update (2018-10-05): YouTube user Frederic Torres said this issue goes away when externally clocking the FT232R chip. It’s not easy to do on the breakout boards, but if you’re spinning your own PCB it’s an option to try!
Bit-banging pin states on FTDI chips is a cool hack, but it isn’t necessarily the best solution for every problem. This section lists some alternative methods which may achieve similar goals, and touches on some of their pros and cons.
-
LibFTDI - an alternative, open-source, third party driver for FTDI devices. Using this driver instead of the default FTDI driver gives you options to more powerful commands to interact with FTDI chips. One interesting option is the simple ability to interact with the chip from Python with pyLibFTDI.While this is a good took for hackers and makers, if I want to build a device to send to a lay client I won’t want to expect them to fumble with installing custom drivers or ensure they are being used over the default ones FTDI supplies. I chose not to pursue utilizing this project because I value the “plug it in and it just works” functionality that comes from simply using FTDI’s API and drivers (which are automatically supplied by Windows)
-
Raspberry PI can bit-bang SPI - While perhaps not ideal for making small USB devices to send to clients, if your primary goal is just to control a SPI device from a computer then definitely consider using a Raspberry Pi! A few of the pins on its header are capable of SPI and can even be driven directly from the bash console. I’ve used this technique to generate analog voltages from a command line using a Raspberry PI to send SPI commands to a MCP4921 12-bit DAC.
-
Multi-Protocol Synchronous Serial Engine (MPSSE) - Some FTDI chips support MPSSE, which can send SPI (or I2C or other) protocols without you having to worry about bit-banging pins. I chose not to pursue this option because I wanted to use my FT232R (one of the most common and inexpensive FTDI chips), which doesn’t support MPSSE. ALthough I do have a FT232H which does support MPSSE (example project), I chose not to use that feature for this project, favoring a single code/program to control all FTDI devices.
-
Bus Pirate - If you don’t have a Bus Pirate already, get one! It’s one of the most convenient ways to get a new peripheral up and running. It’s a USB device you can interact with through a serial terminal (supplied using a FTDI usb-to-serial converter, go fig) and you can tell it to send/receive commands to SPI or I2C devices. It does a lot more, and is worth checking out.
-
FtdiSharp - Several years after writing this blog post, I created a C# library for interacting with FTDI USB devices
-
Saleae logic analyzers - The official Saleae hardware (not what was shown in my video, which was a cheap eBay knock-off) can do a lot of great things. Their free software is _really _simple to use, and they haven’t gone out of their way to block the use of third-party logic analyzers with their free software. If you are in a place where you can afford to support this company financially, I suggest browsing their products and purchasing their official hardware.
-
DYMO Letra-Tag LT100-H label maker and clear tape - When labels are printed with black boxes around them (a tip I learned from Onno) they look fantastic when placed on project boxes! Don’t buy the knock-off clear labels, as they aren’t truly clear. The clear tape you need to purchase has the brand name “DYMO” written on the tape dispenser.
-
FT232H breakout board (adafruit) - This is where I got the FT232H board used in this video. You can find additional similar FT232H breakout boards on Amazon.
-
FT232R breakout board - Everyone sells these. I got some lately on Amazon, but I’ve gotten them before on eBay too.
-
TTL-232R cable - If you’re making a device which you want to appear a bit more professional, this cable has the FT232R built-in and it just has several pins (in a female header) you can snap onto your board.
-
Bit Bang Modes for the FT232R - FTDI datasheet detailing how to Bit-Bang the FT232R chip. In practice, the terms, language, and code examples in this datasheet seem similar enough to the FT232H that it probably is all you need to get started, since it’s the large-scale concepts which are most important.
-
Introduction to the FTDI BitBang mode - A Hack-A-Day article from 2009 mentions FTDI chips can be used to bit-bang pin states and they have their own LED blink examples. Their article does hint at using this method to bit-bang SPI, but it fails entirely to note the FT232R bug that surely has confused multiple people in the past…
-
FT232R BitBang SPI example - This code uses libftdi, not the default driver supplied by FTDI (libftd2xx).
-
FT232R BitBang mode is broken - an article from 2012 detailing how bad the timing is when bit-banging pin states on the FT232R.
-
Official acknowledgement of the FT232R timing problem is described in the TN_120 FT232R Errate Technical Note in section 3.1.2 where they state the problem as: “The output may be clocked out at different speeds to allow for different pulse widths. However this clocking stage is not synchronized with the incoming data and can result in the pulse widths varying unexpectedly on the output.”
-
AD9850 Complete DDS Synthesizer Datasheet
Bit-banging pin states on FTDI devices is relatively simple, even using the standard drivers and API. The FTD2XX_NET library on NuGet provides a simple way to do this. The output of the FT232H is much more accurate in the time domain than the FT232R. Although there are crazy timing issues with the FT232R, it works fine when driving most SPI devices. Here we used this technique to write a console application to control an AD9850 DDS directly from an FT232R using command line arguments. When given a formal enclosure, this project looks (and works) great!
If you make something cool by bit-banging a FTDI device, let me know about it!
The Internet of Things now includes Festivus poles! **Festivus is a holiday celebrated on December 23rd, and its customary practices include a Festivus pole, Festivus dinner, airing of grievances, feats of strength, and Festivus miracles. The internet contains a few nods to the holiday, including what happens when you Google for the word Festivus (a Festivus pole is displayed at the bottom of the page). In 2015 I had the honor of gifting the world with the first Festivus pole video game, and today I am happy to unveil the world’s first internet-enabled Festivus pole. Every time somebody tweets #Festivus or #FestivusMiracle, the light at the top of the pole illuminates! All in the room then excitedly exclaim, “it’s a Festivus miracle!”
The IoT Festivus Pole is powered by a Raspberry Pi (a Pi 2 Model B, although any Pi would work) running a Python script which occasionally checks for tweets using the twitter API (via twython, a pure-python twitter API wrapper) and controls the GPIO pin 12 with RPi.GPIO (extra docs). After writing the Python script (which should work identically in Python 2 or Python 3), I got it to run automatically every time the system boots by adding a line to /etc/rc.local (surrounding it with parentheses and terminating the line with & to allow it to run without blocking the startup sequence). The LED was added to the end of a long wire (with a series 220-ohm resistor) and connected across the Raspberry Pi header pins 12 (PWM) and 14 (GND). I set PWM frequency to 100 Hz, but this is easily configurable in software.
To build the Festivus pole I got a piece of wood and a steel conduit pipe from Lowe’s (total <$5). Festivus purists will argue that Festivus poles should be made from aluminum (with its very high strength to weight ratio). I live in an apartment and don’t have a garage, so my tool selection is limited. I cut the wood a few times with a jigsaw and glued it together to make an impressive stand similar to those of traditional Festivus poles. I have a few hole saw drill bits, but none of them perfectly matched the size of the pipe. I traced the outline of the pipe on the wood and cut-out a circular piece with a Dremel drill press in combination with a side-cutting bit. The hole was slightly larger than required for the pipe, so I used a few layers of electrical tape on the bottom of the pipe to “seal” the base of the pipe into the hole, then poured acrylic epoxy into the empty space. Clamping it against a desk allowed the epoxy to set such that the pole was rigidly upright, and the result was a fantastic-looking Festivus pole! It’s a bit smaller in size than the famous one featured in Seinfeld, but I think it is appropriately sized for my apartment.
Adding the computer was easy! Internet capability was provided via a USB WiFi card. Code is at the bottom of this page. The LED was connected to Raspberry Pi header pins 12 and 14. The wiring was snaked through the conduit.
The code will work on Python 2 and Python 3.
Pip can be used to install RPi.GPIO and twython: pip install python-dev python-rpi.gpio twython
import RPi.GPIO as GPIO
import time
from twython import Twython
APP_KEY = 'zSNYBNWHmXhU3CX765HnoQEbm'
APP_SECRET = 'getYourOwnApiKeyFromTwitterWebsite'
twitter = Twython(APP_KEY, APP_SECRET)
auth = twitter.get_authentication_tokens()
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
p = GPIO.PWM(12, 100)
p.start(0)
if __name__=="__main__":
tweetLast=0
checkLast=0
duty=100
while True:
if (checkLast+5)<time.time():
checkLast=time.time()
print("checking twitter...")
tweetLatest=twitter.search(q='festivus')["statuses"][0]["created_at"]
if tweetLatest!=tweetLast:
print("IT'S A FESTIVUS MIRACLE!")
tweetLast=tweetLatest
duty=100
else:
print('nothing')
if duty>=0:
p.ChangeDutyCycle(duty)
time.sleep(.3)
duty-=1
This Festivus pole has been up and running for the last few days and I’m excited to see how much joy it has brought into my household! Admittedly the Raspberry Pi seems to be overkill, but at the time I was considering having it also output audio every time a tweet is made but I never decided on the clip to use so I omitted the feature. An ESP8266 WiFi module interfaced with a microntroller can do the same job with more elegance and lower cost, so I may consider improving it next year. Until then, Happy Festivus!
Florida is about to get hit by a massive hurricane, and my home town is in the direct path! I am well prepared with lots of food, water, and communications equipment. While the storm itself is dangerous, part of getting ready for it means preparing for the potential to be out of power for weeks. A staple go-to for light when the power is out is candles. Instinctively people tend to reach for candles and kerosene lamps (in Florida they’re called hurricane lanterns). However, these sources of light can be extremely dangerous!
With the storm one day away, my roommates and I began pooling our emergency supplies in the living room and I grew uneasy about how many matches and candles were accumulating. With severe weather, wind, falling trees, tornadoes, and projectiles blowing around there is an appreciable risk of knocking-over a flame and starting a fire. This risk multiplies when you consider that people often fall asleep with flames running, perhaps even in another room!
I thought how great it would be to have a bunch of LED candles, but there is absolutely no way I can buy one now. Although I could just leave a flashlight on shining at the ceiling, it would produce too much light and the batteries would die before long. With the storm one day away, every store in this town is out of water, most groceries are out of canned foods, and most of the gas stations are out of gas and have locked up. Flashlights, radios, and LED candles are surely gone from all the stores as well. I decided to hack-together several LED candles to use around the house over the next several days, and the result came out great!
I taped together 2 AA batteries and soldered a resistor and a white LED in series with jumper to serve as an on/off switch. It’s not yellow and doesn’t flicker like fancy LED candles, but who cares? This is perfectly functional, and for lighting a room I would say it’s a superior alternative to fire-based candles when the power is out for long periods of time. The batteries will last much longer than they would if we just turned on a flashlight and aimed it at the ceiling too. My white LEDs (generic low current clear 5mm LEDs) have about a 20º light emission angle. To improve its function as a room light I taped a sheet of paper around a glass cup and set it over the top to act as a light diffuser. This couldn’t be simpler!
If the light diffuser is removed this thing works pretty well as a flashlight. I practiced walking around a dark closet and pointing it around and was impressed at how much it is able to illuminate a relatively narrow area. This is a good time to add a basic warning reminding people that soldering directly to batteries is potentially dangerous for the person (and may be destructive to the battery) and it should be avoided. Battery holders are superior, and batteries with solder tabs already on them are a superior alternative to generic batteries.
I found a box of battery holders and decided to make a second version of this device. I felt better about this one since I didn’t need to solder directly to any batteries. A dot of super glue is all it took to secure the LED to the enclosure, and it even stands upright!
I’ll use some scratch match to predict how long this device will stay lit. I’ll run the math first for the 2xAA version. Placing an ammeter in the circuit while the LED was on revealed it consumes 1.8 mA of current. PowerStream has a great website showing battery discharge curves for various consumer grade batteries. Eyeballing the graph it looks like most batteries doesn’t start to drop voltage significantly until near the end of their life. To make calculations simple, let’s just use the mAH (milliamp hour) rating that the manufacturer provides… except I can’t find where Amazon specs their “Amazon basics” battery. A consumer review indicates 997 mAh at 100 mA discharge rate. I’m sure our duration would be far beyond this since we are drawing less than 1/50 of that much current, but let’s just say 1000 mAh to be conservative. We can double that since we are using two AA batteries in this circuit, so 2000 mAh / 1.8 mA = 46 days. Interestingly, the 3xAAA battery presents a larger voltage to the led/resistor so it draws more current (6.3 mA) and 3000 mAh / 6.3 mA it is expected to last only about 19 days. I could increase the value of the resistor to compensate, but it’s already built and it’s fine enough for my needs.
When the storm has passed and things return to normal, I’ll consider making a few different designs and testing how long they actually last. Many battery tests use relatively high current challenges so their discharge finishes in days rather than weeks or months… but with a sensitive voltmeter circuit attached to a logging raspberry pi or something, I’d be interested to see the battery discharge curve of a DIY LED candle on a weeks/months timescale! For now I feel prepared for the upcoming storm, and with several DIY LED candles to light my home instead of actual candles, I’ll feel safer as well.
Two months later (Nov 11, 2017) this thing is still going strong! I’ve left it on continuously since it was built, and I’m truly surprised by how long this has lasted… I’m going it continue leaving it running to see how much longer it goes. For future builds I will add more LEDs and not be so concerned about longevity. It may be work noting that a build like this would have been great for residents of Puerto Rico, because much of that island is still without power. This is a photograph in a dimly-lit room after more than 2 months continuous use: