The personal website of Scott W Harden
September 9th, 2017

Hurricane Hack - DIY LED Candle

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.

3xAAA Version

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!

How long will it last?

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.

UPDATE

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:

Markdown source code last modified on January 18th, 2021
---
title: Hurricane Hack - DIY LED Candle
date: 2017-09-09 11:27:43
tags: circuit
---

# Hurricane Hack - DIY LED Candle

**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!

<div class="text-center img-border img-small">

[![](livehere_thumb.jpg)](livehere.jpg)

</div>

**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](https://en.wikipedia.org/wiki/Flameless_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!

<div class="text-center">

[![](hurricaine-diy-led-candle_thumb.jpg)](hurricaine-diy-led-candle.jpg)

</div>

**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!

<div class="text-center img-border img-medium">

[![](063_thumb.jpg)](063.jpg)

</div>

**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._

# 3xAAA Version

**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!

<div class="text-center img-border img-small">

[![](081_thumb.jpg)](081.jpg)
[![](078_thumb.jpg)](078.jpg)

</div>

# How long will it last?

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](https://www.powerstream.com/AA-tests.htm) 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.

<div class="text-center img-border img-small">

[![](088_thumb.jpg)](088.jpg)
[![](091_thumb.jpg)](091.jpg)

</div>

**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.

## UPDATE

**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:

<div class="text-center img-border">

[![](IMG_0303_thumb.jpg)](IMG_0303.jpg)

</div>
September 2nd, 2017

PS4 Controller Hack - Adding Auto-Run

After a long day it can be really nice to have a relaxing hobby to clear your head, and few activities shut down your brain as effectively as video games. However, a newly released video game is physically hurting me as my impulse to move quickly causes me to perpetually click the run button. After a few days of game-play and a really sore left thumb, I decided to do something about it: hack-in a microchip to automatically click the button for me.

Modifying game controllers to do things like automatically rapid fire is nothing new. I once modified a USB computer mouse to add an extra "rapid fire" button (link). Hackaday ran a story about a guy who hacked a PS4 controller to add mouse and keyboard functionality. Today's hack isn't quite as elaborate, but it's very effective. Here I show how I modified a PlayStation 4 controller to automatically click the L3 button so I am always running. This auto-run functionality mimics the auto-run feature built into many games (like Titanfall 2 which spoiled me to expect this), but I built the circuit so it can be toggled on and off by clicking the L3 button. After playing Titanfall 2 for the last few months, the recent release of the Call of Duty WWII beta is driving me crazy as it requires me to click the run button over and over (every two seconds) which, after an afternoon of playing, is actually painful.

Assessing the PS4 Controller

I started out by looking online to see what the PS4 controller looked like inside. Imgur has a great PS4 dualshock controller teardown photo collection which was an excellent starting place. From these photos I realized this hack would be pretty easy since the L3 "click" action is achieved by a through-hole SPDT tactile switch placed under the joystick.

I was surprised to find my PS4 controller (below) was a little different (green for starters), but the overall layout was the same. I quickly identified the 4 pins of the L3 tactile switch and got to work...

After probing around with a multimeter and an oscilloscope, I was able to determine which pins do what. Just from looking at the trace it's pretty obvious that two of the pins are the positive voltage rail. In this controller the positive voltage (VCC) is about 3 volts, so keep that in mind and don't hook-up a 5V power supply if you decide to debug this thing.

To test my idea I attached 3 wires to VCC, GND, and SENSE and ran into the other room where my PS4 was. As I held the left joystick up in a game, shorting the SENSE and GND wires (by tapping them together) resulted in running! At this point I knew this hack would work, and proceeded to have a microcontroller control voltage of the L3 sense line.

Simulating L3 Presses with a Microcontroller

I glued a microcontroller (ATTiny85) to the circuit board, then ran some wires to the points of interest. Visual inspection (and a double check with a multimeter when the battery was in) provided easy points for positive and ground which could power my microcontroller. The "L3 sense" pin (which toggles between two voltages when you press the L3 button) was run to pin 3 (PB4) of the microcontroller. In a production environment current limiting resistors and debounce capacitors would make sense, but in the spirit of the hack I keep things minimalistic.

The device could be easily reassembled, and there was plenty of room for the battery pack and its plastic holder to snap in place over the wires. Excellent!

While I was in the controller, I removed the light-pipe that carries light to the diffuser on the back. The PS4 has an embarrassingly poor design (IMO) where the far side of the controller emits light depending on the state of the controller (blue for in use, black for off, orange for charging, etc). This is a terrible design in my opinion because if you have a glossy and reflective TV screen like I do, you see a blue light reflect back in the screen and bobble up and down as you hold the controller. Dumb! Removing the light pipe dramatically reduced the intensity, but still retains the original functionality.

Programming the microcontroller was achieved with an in circuit serial programmer (USBtinyISP) with test clips. This is my new favorite way to program microcontrollers for one-off projects. If the pins of the microcontroller aren't directly accessable, breaking them out on 0.1" headers is simple enough and they make great points of attachment for test clips. The simplest code to continuously auto-run is achieved by just swinging the sense line between 5V and 0V. This is the code to do that:

for(;;){ // do this forever
    // simulate a button press
    PORTB|=(1<<PB4); // pull high
    _delay_ms(50); // hold it there
    // simulate a button release
    PORTB&=~(1<<PB4); // pull low
    _delay_ms(50); // hold it there
}

Sensing Actual Button Presses to Toggle Auto-Run On/Off

Simulating L3 presses was as simple as toggling the sense line between VCC and GND, but sensing manual L3 presses wasn't quite as easy. After probing the output on the scope (see video) I realized that manual button presses toggle between voltages of about 2V and 3V, and the line never really goes down to zero (or below VCC/2) so it's never read as "off" by the digital input pin. Therefore, I changed my strategy a bit. Instead of clamping between 5V and 0V, I toggled between low impedance and high impedance states. This seemed like it would be gentler on the controller circuit, as well as allow me to use the ADC (analog-to-digital controller) of the microcontroller to read voltage on the line. If voltage is above a certain amount, the microcontroller detects a manual button press is happening and toggles the auto-run functionality on/off. The new code is this:

for(;;){ // do this forever

    // simulate a button press
    PORTB|=(1<<PB4); // pull high
    DDRB|=(1<<PB4); // make output
    _delay_ms(50); // hold it there

    // simulate a button release
    DDRB&=~(1<<PB4); // make input
    PORTB&=~(1<<PB4); // pull low
    _delay_ms(50); // hold it there

    // check if the button is actually pressed to togggle auto press
    if (ADC>200) { // if the button is manually pressed
        _delay_ms(100); // wait a bit
        while (ADC>200) {} // wait until it depresses
        while (ADC<200) {} // then wait for it to be pressed again
    }
}

It works! After giving it a spin on the Call of Duty WWII Beta, I'm happy to report that this circuit is holding up well and I'm running forever effortlessly. I still suck at aiming, shooting, and not dying though.

Follow-Up Notes

Follow-up #1: After playing the fast-paced and highly dynamic Titanfall 2 for so long, I rapidly became disenchanted with the Call of Duty WWII game-play which now feels slow and monotonous in comparison. Although this auto-sprint controller hack works, I don't really use it because I barely play the game I made it for! I'm going back to exclusively playing Titanfall 2 for now, and if you get the chance I highly recommend giving it a spin!

Follow-up #1: Call of DUty WWII added auto-sprint a few months after this post was made

Markdown source code last modified on January 18th, 2021
---
title: PS4 Controller Hack - Adding Auto-Run
date: 2017-09-02 15:48:22
tags: circuit
---

# PS4 Controller Hack - Adding Auto-Run

**After a long day** it can be really nice to have a relaxing hobby to clear your head, and few activities shut down your brain as effectively as video games. However, a newly released video game is physically hurting me as my impulse to move quickly causes me to perpetually click the run button. After a few days of game-play and a really sore left thumb, I decided to do something about it: hack-in a microchip to automatically click the button for me.

**Modifying game controllers to do things like automatically rapid fire is nothing new.** I once modified a USB computer mouse to add an extra "rapid fire" button ([link](https://www.swharden.com/wp/2010-12-28-full-auto-rapidfire-mouse-modification/)). Hackaday ran a story about a guy who[ hacked a PS4 controller to add mouse and keyboard functionality](https://hackaday.com/2013/12/12/modifying-a-ps4-dualshock4-controller-to-use-a-mouse-and-keyboard/). Today's hack isn't quite as elaborate, but it's very effective. Here I show how I modified a PlayStation 4 controller to automatically click the L3 button so I am always running. This auto-run functionality mimics the auto-run feature built into many games (like [Titanfall 2](https://www.youtube.com/watch?v=EXwdWuSuiYA) which spoiled me to expect this), but I built the circuit so it can be toggled on and off by clicking the L3 button. After playing Titanfall 2 for the last few months, the recent release of the [Call of Duty WWII](https://www.youtube.com/watch?v=D4Q_XYVescc) beta is driving me crazy as it requires me to click the run button over and over (every two seconds) which, after an afternoon of playing, is actually painful.

![](https://www.youtube.com/embed/v-164sv21Tw)

## Assessing the PS4 Controller

**I started out by looking online to see what the PS4 controller looked like inside.** Imgur has a [great PS4 dualshock controller teardown photo collection](http://imgur.com/a/ytRW5) which was an excellent starting place. From these photos I realized this hack would be pretty easy since the L3 "click" action is achieved by a [through-hole SPDT tactile switch](https://learn.sparkfun.com/tutorials/switch-basics) placed under the joystick.

<div class="text-center">

[![](teardown_thumb.jpg)](teardown.jpg)

</div>

**I was surprised to find** my PS4 controller (below) was a little different (green for starters), but the overall layout was the same. I quickly identified the 4 pins of the L3 tactile switch and got to work...

<div class="text-center img-border">

[![](069_thumb.jpg)](069.jpg)

</div>

**After probing around** **with a multimeter and an oscilloscope,** **I was able to determine which pins do what.** Just from looking at the trace it's pretty obvious that two of the pins are the positive voltage rail. In this controller the positive voltage (VCC) is about 3 volts, so keep that in mind and don't hook-up a 5V power supply if you decide to debug this thing.

<div class="text-center img-border">

[![](095_thumb.jpg)](095.jpg)

</div>

**To test my idea I attached 3 wires to VCC, GND, and SENSE and ran into the other room where my PS4 was.** As I held the left joystick up in a game, shorting the SENSE and GND wires (by tapping them together) resulted in running! At this point I knew this hack would work, and proceeded to have a microcontroller control voltage of the L3 sense line.

## Simulating L3 Presses with a Microcontroller

<div class="text-center img-border">

[![](145_thumb.jpg)](145.jpg)

</div>

**I glued a microcontroller ([ATTiny85](http://www.atmel.com/images/atmel-2586-avr-8-bit-microcontroller-attiny25-attiny45-attiny85_datasheet.pdf)) to the circuit board, then ran some wires to the points of interest.** Visual inspection (and a double check with a multimeter when the battery was in) provided easy points for positive and ground which could power my microcontroller. The "L3 sense" pin (which toggles between two voltages when you press the L3 button) was run to pin 3 (PB4) of the microcontroller. In a production environment current limiting resistors and debounce capacitors would make sense, but in the spirit of the hack I keep things minimalistic.

<div class="text-center img-border">

[![](158_thumb.jpg)](158.jpg)

</div>

**The device could be easily reassembled,** and there was plenty of room for the battery pack and its plastic holder to snap in place over the wires. Excellent!

<div class="text-center img-border">

[![](161_thumb.jpg)](161.jpg)

</div>

**While I was in the controller,** I removed the light-pipe that carries light to the diffuser on the back. The PS4 has an embarrassingly poor design (IMO) where the far side of the controller emits light depending on the state of the controller (blue for in use, black for off, orange for charging, etc). This is a terrible design in my opinion because if you have a glossy and reflective TV screen like I do, you see a blue light reflect back in the screen and bobble up and down as you hold the controller. Dumb! Removing the light pipe dramatically reduced the intensity, but still retains the original functionality.

<div class="text-center img-border">

[![](166_thumb.jpg)](166.jpg)

</div>

**Programming the microcontroller** was achieved with an in circuit serial programmer ([USBtinyISP](https://www.ebay.com/sch/i.html?&_nkw=USBtinyISP)) with test clips. This is my new favorite way to program microcontrollers for one-off projects. If the pins of the microcontroller aren't directly accessable, breaking them out on 0.1" headers is simple enough and they make great points of attachment for test clips. The simplest code to continuously auto-run is achieved by just swinging the sense line between 5V and 0V. This is the code to do that:

```c
for(;;){ // do this forever
    // simulate a button press
    PORTB|=(1<<PB4); // pull high
    _delay_ms(50); // hold it there
    // simulate a button release
    PORTB&=~(1<<PB4); // pull low
    _delay_ms(50); // hold it there
}

```

## Sensing Actual Button Presses to Toggle Auto-Run On/Off

**Simulating L3 presses was as simple as toggling the sense line between VCC and GND, but _sensing_ manual L3 presses wasn't quite as easy.** After probing the output on the scope (see video) I realized that manual button presses toggle between voltages of about 2V and 3V, and the line never really goes down to zero (or below VCC/2) so it's never read as "off" by the digital input pin. Therefore, I changed my strategy a bit. Instead of clamping between 5V and 0V, I toggled between low impedance and high impedance states. This seemed like it would be gentler on the controller circuit, as well as allow me to use the ADC (analog-to-digital controller) of the microcontroller to read voltage on the line. If voltage is above a certain amount, the microcontroller detects a manual button press is happening and toggles the auto-run functionality on/off. The new code is this:

```c
for(;;){ // do this forever

    // simulate a button press
    PORTB|=(1<<PB4); // pull high
    DDRB|=(1<<PB4); // make output
    _delay_ms(50); // hold it there

    // simulate a button release
    DDRB&=~(1<<PB4); // make input
    PORTB&=~(1<<PB4); // pull low
    _delay_ms(50); // hold it there

    // check if the button is actually pressed to togggle auto press
    if (ADC>200) { // if the button is manually pressed
        _delay_ms(100); // wait a bit
        while (ADC>200) {} // wait until it depresses
        while (ADC<200) {} // then wait for it to be pressed again
    }
}
```

**It works!** After giving it a spin on the Call of Duty WWII Beta, I'm happy to report that this circuit is holding up well and I'm running forever effortlessly. I still suck at aiming, shooting, and not dying though.

## Follow-Up Notes

Follow-up #1: After playing the fast-paced and highly dynamic Titanfall 2 for so long, I rapidly became disenchanted with the Call of Duty WWII game-play which now feels slow and monotonous in comparison. Although this auto-sprint controller hack works, I don't really use it because I barely play the game I made it for! I'm going back to exclusively playing Titanfall 2 for now, and if you get the chance I highly recommend giving it a spin!

![](https://www.youtube.com/embed/ktw2k3m7Qko)

Follow-up #1: Call of DUty WWII added auto-sprint a few months after this post was made
August 20th, 2017

Microcontroller Action Potential Generator

Here I demonstrate how to use a single microcontroller pin to generate action-potential-like waveforms. The output is similar my fully analog action potential generator circuit, but the waveform here is created in an entirely different way. A microcontroller is at the core of this project and determines when to fire action potentials. Taking advantage of the pseudo-random number generator (rand() in AVR-GCC's stdlib.h), I am able to easily produce unevenly-spaced action potentials which more accurately reflect those observed in nature. This circuit has a potentiometer to adjust the action potential frequency (probability) and another to adjust the amount of overshoot (afterhyperpolarization, AHP). I created this project because I wanted to practice designing various types of action potential measurement circuits, so creating an action potential generating circuit was an obvious perquisite.

The core of this circuit is a capacitor which is charged and discharged by toggling a microcontroller pin between high, low, and high-Z states. In the high state (pin configured as output, clamped at 5V) the capacitor charges through a series resistor as the pin sources current. In the low state (pin configured as output, clamped at 0V) the capacitor discharges through a series resistor as the pin sinks current. In the high-Z / high impedance state (pin configured as an input and little current flows through it), the capacitor rests. By spending most of the time in high-Z then rapidly cycling through high/low states, triangular waveforms can be created with rapid rise/fall times. Amplifying this transient and applying a low-pass filter using a single operational amplifier stage of an LM-358 shapes this transient into something which resembles an action potential. Wikipedia has a section describing how to use an op-amp to design an active low-pass filter like the one used here.

The code to generate the digital waveform is very straightforward. I'm using PB4 to charge/discharge the capacitor, so the code which actually fires an action potential is as follows:

// rising part = charging the capacitor
DDRB|=(1<<PB4); // make output (low Z)
PORTB|=(1<<PB4); // make high (5v, source current)
_delay_ms(2); // 2ms rise time

// falling part
DDRB|=(1<<PB4); // make output (low Z)
PORTB&=~(1<<PB4); // make low (0V, sink current)
_delay_ms(2); // 2ms fall time
_delay_us(150); // extra fall time for AHP

// return to rest state
DDRB&=~(1<<PB4); // make input (high Z)

Programming the microcontroller was accomplished after it was soldered into the device using test clips attached to my ICSP (USBtinyISP). I only recently started using test clips, and for one-off projects like this it's so much easier than adding header sockets or even wiring up header pins.

I am very pleased with how well this project turned out! I now have an easy way to make irregularly-spaced action potentials, and have a great starting point for future projects aimed at measuring action potential features using analog circuitry.

Notes

  • Action potential half-width (relating to the speed of the action potential) could be adjusted in software by reducing the time to charge and discharge the capacitor. A user control was not built in to the circuit shown here, however it would be very easy to allow a user to switch between regular and fast-spiking action potential waveforms.
  • I am happy that using the 1n4148 diode on the positive input of the op-amp works, but using two 100k resistors (forming a voltage divider floating around 2.5V) at the input and reducing the gain of this stage may have produced a more reliable result.
  • Action potential frequency (probability) is currently detected by sensing the analog voltage output by a rail-to-rail potentiometer. However, if you sensed a noisy line (simulating random excitatory and inhibitory synaptic input), you could easily make an integrate-and-fire model neuron which fires in response to excitatory input.
  • Discussion related to the nature of this "model neuron" with respect to other models (i.e., Hodgkin–Huxley) are on the previous post.
  • Something like this would make an interesting science fair project

Source Code on GitHub

Markdown source code last modified on January 18th, 2021
---
title: Microcontroller Action Potential Generator
date: 2017-08-20 15:12:33
tags: science, circuit, microcontroller, python
---

# Microcontroller Action Potential Generator

__Here I demonstrate how to use a _single_ microcontroller pin to generate action-potential-like waveforms. __The output is similar [my fully analog action potential generator circuit](https://www.swharden.com/wp/2017-08-12-analog-action-potential-generator-circuit/), but the waveform here is created in an entirely different way. A microcontroller is at the core of this project and determines when to fire action potentials. Taking advantage of the pseudo-random number generator ([rand() in AVR-GCC's stdlib.h](http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#gae23144bcbb8e3742b00eb687c36654d1)), I am able to easily produce unevenly-spaced action potentials which more accurately reflect those observed in nature. This circuit has a potentiometer to adjust the action potential frequency (probability) and another to adjust the amount of overshoot (afterhyperpolarization, AHP). I created this project because I wanted to practice designing various types of action potential _measurement_ circuits, so creating an action potential _generating_ circuit was an obvious perquisite.

![](https://www.youtube.com/embed/2s8t3UsONFs)

__The core of this circuit is a capacitor which is charged and discharged by toggling a microcontroller pin between high, low, and high-Z states.__ In the high state (pin configured as output, clamped at 5V) the capacitor charges through a series resistor as the pin sources current. In the low state (pin configured as output, clamped at 0V) the capacitor discharges through a series resistor as the pin sinks current. In the high-Z / high impedance state (pin configured as an _input_ and little current flows through it), the capacitor rests. By spending most of the time in high-Z then rapidly cycling through high/low states, triangular waveforms can be created with rapid rise/fall times. Amplifying this transient and applying a low-pass filter using a single operational amplifier stage of an [LM-358](http://www.ti.com/lit/ds/symlink/lm158-n.pdf) shapes this transient into something which resembles an action potential. Wikipedia has a section describing how to [use an op-amp to design an active low-pass filter](https://en.wikipedia.org/wiki/Low-pass_filter#Active_electronic_realization) like the one used here.

<div class="text-center img-border">

[![](action-potential-generator-circuit_thumb.jpg)](action-potential-generator-circuit.jpg)

</div>

__The code to generate the digital waveform is very straightforward.__ I'm using PB4 to charge/discharge the capacitor, so the code which actually fires an action potential is as follows:

```c
// rising part = charging the capacitor
DDRB|=(1<<PB4); // make output (low Z)
PORTB|=(1<<PB4); // make high (5v, source current)
_delay_ms(2); // 2ms rise time

// falling part
DDRB|=(1<<PB4); // make output (low Z)
PORTB&=~(1<<PB4); // make low (0V, sink current)
_delay_ms(2); // 2ms fall time
_delay_us(150); // extra fall time for AHP

// return to rest state
DDRB&=~(1<<PB4); // make input (high Z)
```

__Programming the microcontroller__ was accomplished after it was soldered into the device using test clips attached to my ICSP ([USBtinyISP](https://www.ebay.com/sch/i.html?&_nkw=USBtinyISP)). I only recently started using test clips, and for one-off projects like this it's so much easier than adding header sockets or even wiring up header pins.

<div class="text-center img-border">

[![](ap-generator-programmer_thumb.jpg)](ap-generator-programmer.jpg)
[![](ap-generator-programmer-close_thumb.jpg)](ap-generator-programmer-close.jpg)

</div>

__I am very pleased with how well this project turned out!__ I now have an easy way to make irregularly-spaced action potentials, and have a great starting point for future projects aimed at _measuring_ action potential features using analog circuitry.


<div class="text-center img-border">

[![](ap-generator-running_thumb.jpg)](ap-generator-running.jpg)
[![](ap-generator-running-2_thumb.jpg)](ap-generator-running-2.jpg)

</div>

### Notes

*   Action potential half-width (relating to the speed of the action potential) could be adjusted _in software_ by reducing the time to charge and discharge the capacitor. A user control was not built in to the circuit shown here, however it would be very easy to allow a user to switch between regular and fast-spiking action potential waveforms.
*   I am happy that using the 1n4148 diode on the positive input of the op-amp works, but using two 100k resistors (forming a voltage divider floating around 2.5V) at the input and reducing the gain of this stage may have produced a more reliable result.
*   Action potential frequency (probability) is currently detected by sensing the analog voltage output by a rail-to-rail potentiometer. However, if you sensed a noisy line (simulating random excitatory and inhibitory synaptic input), you could easily make an integrate-and-fire model neuron which fires in response to excitatory input.
*   Discussion related to the nature of this "model neuron" with respect to other models (i.e., Hodgkin–Huxley) are on the [previous post](https://www.swharden.com/wp/2017-08-12-analog-action-potential-generator-circuit/).
*   Something like this would make an interesting science fair project

### Source Code on GitHub

*   <https://github.com/swharden/AVR-projects/>

*   <https://github.com/swharden/AVR-projects/tree/master/ATTiny85%202017-08-19%20action%20potential%20generator>
August 12th, 2017

Action Potential Generator Circuit

Few biological cells are as interesting to the electrical engineer as the neuron. Neurons are essentially capacitors (with a dielectric cell membrane separating conductive fluid on each side) with parallel charge pumps, leak currents, and nonlinear voltage-dependent currents. When massively parallelized, these individual functional electrical units yield complex behavior and underlie consciousness. The study of the electrical properties of neurons (neurophysiologically, a subset of electrophysiology) often involves the development and use of sensitive electrical equipment aimed at studying these small potentials produced by neurons and currents which travel through channels embedded in their membranes. It seems neurophysiology has gained an emerging interest from the hacker community, as evidenced by the success of Back Yard Brains, projects like the OpenEEG, and Hack-A-Day's recent feature The Neuron - a Hacker's Perspective.

While contemplating designs for action potential detection and analysis circuitry, I realized that it would be beneficial to be able to generate action-potential-like waveforms on my workbench. The circuit I came up with to do this is a fully analog (technically mixed signal) action potential generator which produces lifelike action potentials.

Cellular Neurophysiology for Electrical Engineers (in 2 sentences): Neuron action potentials (self-propagating voltage-triggered depolarizations) in individual neurons are measured in scientific environments using single cell recording tools such as sharp microelectrodes and patch-clamp pipettes. Neurons typically rest around -70mV and when depolarized (typically by external excitatory input) above a threshold they engage in a self-propagating depolarization until they reach approximately +40mV, at which time a self-propagating repolarization occurs (often over-shooting the initial rest potential by several mV), then the cell slowly returns to the rest voltage so after about 50ms the neuron is prepared to fire another action potential. Impassioned budding electrophysiologists may enjoy further reading _Active Behavior of the Cell Membrane _and Introduction to Computational Neuroscience.

The circuit I describe here produces waveforms which visually mimic action potentials rather than serve to replicate the exact conductances real neurons employ to exhibit their complex behavior. It is worth noting that numerous scientists and engineers have designed more physiological electrical representations of neuronal circuitry using discrete components. In fact, the Hodgkin-Huxley model of the initiation and propagation of action potentials earned Alan Hodgkin and Andrew Huxley the Nobel Prize in Physiology and Medicine in 1936. Some resources on the internet describe how to design lifelike action potential generating circuits by mimicking the endogenous ionic conductances which underlie them, notably Analog and Digital Hardware Neural Models, Active Cell Model, and Neuromorphic Silicon Neuron Circuits. My goal for this project is to create waveforms which resemble action potentials, rather than waveforms which truly model them. I suspect it is highly unlikely I will earn a Nobel Prize for the work presented here.

The analog action potential simulator circuit I came up with creates a continuous series action potentials. This is achieved using a 555 timer (specifically the NE555) in an astable configuration to provide continuous square waves (about 6 Hz at about 50% duty). The rising edge of each square wave is isolated with a diode and used to charge a capacitor. While the charge on the capacitor is above a certain voltage, an NPN transistor (the 2N3904) allows current to flow, amplifying this transient input current. The capacitor discharges predictably (as an RC circuit) through a leak resistor. A large value leak resistor slows the discharge and allows that signal's transistor to flow current for a longer duration. By having two signals (fast and slow) using RC circuits with different resistances (smaller and larger), the transistors are on for different durations (shorter and longer). By making the short pulse positive (using the NPN in common collector configuration) and the longer pulse negative (using the NPN in common emitter configuration), a resistor voltage divider can be designed to scale and combine these signals into an output waveform a few hundred mV in size with a 5V power supply. Pictured below is the output of this circuit realized on a breadboard. The blue trace is the output of the 555 timer.

Between the capacitance of the rectification diode, input capacitance of the transistor, and stray parasitic capacitance from the physical construction of my wires and the rails on my breadboard, there is sufficient capacitance to accumulate charge which can be modified by changing the value of the leak resistor.

This circuit produces similar output when simulated. I'm using LTspice (free) to simulate this circuit. The circuit shown is identical to the one hand-drawn and built on the breadboard, with the exception that an additional 0.1 µF capacitor to ground is used on the output to smooth the signal. On the breadboard this capacitance-based low-pass filtering already exists due to the capacitive nature of the components, wires, and rails.

A few improvements naturally come to mind when considering this completed, functional circuit:

  • Action potential frequency: The resistor/capacitor network on the 555 timer determines the rate of square pulses which trigger action potentials. Changing these values will cause a different rate of action potential firing, but I haven't attempted to push it too fast and suspect the result would not be stable is the capacitors are not given time to fully discharge before re-initiating subsequent action potentials.

  • Microcontroller-triggered action potentials: Since action potentials are triggered by any 5V rising edge signal, it is trivially easy to create action potentials from microcontrollers! You could create some very complex firing patterns, or even "reactive" firing patterns which respond to inputs.

For example, add a TSL2561 I2C digital light sensor and you can have a light-to-frequency action potential generator!

  • Adjusting size and shape of action potentials: Since the waveform is the combination of two waveforms, you can really only adjust the duration (width) or amplitude (height) of each individual waveform, as well as the relative proportion of each used in creating the summation. Widths are adjusted by changing the leak resistor on the base of each transistor, or by adding additional capacitance. Amplitude and the ratio of each signal may be adjusted by changing the ratio of resistors on the output resistor divider.

  • Producing -70 mV (physiological) output: The current output is electirically decoupled (through a series capacitor) so it can float at whatever voltage you bias it to. Therefore, it is easy to "pull" in either direction. Adding a 10k potentiometer to bias the output is an easy way to let you set the voltage. A second potentiometer gating the magnitude of the output signal will let you adjust the height of the output waveform as desired.

  • The 555 could be replaced by an inverted ramp (sawtooth): An inverted ramp / sawtooth pattern which produces rapid 5V rising edges would drive this circuit equally well. A fully analog ramp generator circuit can be realized with 3 transistors: essentially a constant current capacitor charger with a threshold-detecting PNP/NPN discharge component.

  • This action potential is not all-or-nothing: In real life, small excitatory inputs which fail to reach the action potential threshold do not produce an action potential voltage waveform. This circuit uses 5V rising edges to produce action potential waveforms. However, feeding a 1V rising edge would produce an action potential 1/5 the size. This is not a physiological effect. However, it is unlikely (if not impossible) for many digital signal sources (i.e., common microcontrollers) to output anything other than sharp rising edge square waves of fixed voltages, so this is not a concern for my application.

  • Random action potentials: When pondering how to create randomly timed action potentials, the issue of how to generate random numbers arises. This is surprisingly difficult, especially in embedded devices. If a microcontroller is already being used, consider Make's write-up on the subject, and I think personally I would go with a transistor-based avalanche nosie generator to create the randomness.

  • A major limitation is that irregularly spaced action potentials have slightly different amplitudes.I found this out the next day when I created a hardware random number generator (yes, that happened) to cause it to fire regularly, missing approximately half of the action potentials. When this happens, breaks in time result in a larger subsequent action potential. There are several ways to get around this, but it's worth noting that the circuit shown here is best operated around 6 Hz with only continuous regularly-spaced action potentials.

In the video I also demonstrate how to record the output of this circuit using a high-speed (44.1 kHz) 16-bit analog-to-digital converter you already have (the microphone input of your sound card). I won't go into all the details here, but below is the code to read data from a WAV file and plot it as if it were a real neuron. The graph below is an actual recording of the circuit described here using the microphone jack of my sound card.

import numpy as np
import matplotlib.pyplot as plt
Ys = np.memmap("recording.wav", dtype='h', mode='r')[1000:40000]
Ys = np.array(Ys)/max(Ys)*150-70
Xs = np.arange(len(Ys))/44100*1000
plt.figure(figsize=(6,3))
plt.grid(alpha=.5,ls=':')
plt.plot(Xs,Ys)
plt.margins(0,.1)
plt.title("Action Potential Circuit Output")
plt.ylabel("potential (mV)")
plt.xlabel("time (ms)")
plt.tight_layout()
plt.savefig("graph.png")
#plt.show()

Let's make some noise! Just to see what it would look like, I created a circuit to generate slowly drifting random noise. I found this was a non-trivial task to achieve in hardware. Most noise generation circuits create random signals on the RF scale (white noise) which when low-pass filtered rapidly approach zero. I wanted something which would slowly drift up and down on a time scale of seconds. I achieved this by creating 4-bit pseudo-random numbers with a shift register (74HC595) clocked at a relatively slow speed (about 200 Hz) having essentially random values on its input. I used a 74HC14 inverting buffer (with Schmidt trigger inputs) to create the low frequency clock signal (about 200 Hz) and an extremely fast and intentionally unstable square wave (about 30 MHz) which was sampled by the shift register to generate the "random" data. The schematic illustrates these points, but note that I accidentally labeled the 74HC14 as a 74HC240. While also an inverting buffer the 74HC240 will not serve as a good RC oscillator buffer because it does not have Schmidt trigger inputs.

An inverting buffer created a fast and a slow clock to produce 4-bit pseudo-random numbers:

reminder how an inverting buffer can act as an oscillator:

the full circuit realized on the breadboard:

output of the 4-bit pseudo-random number generator:

4-bit output smoothed through a single-stage RC filter:

noise combined with action potential waveforms:

The addition of noise was a success, from an electrical and technical sense. It isn't particularly physiological. Neurons would fire differently based on their resting membrane potential, and the peaks of action potential should all be about the same height regardless of the resting potential. However if one were performing an electrical recording through a patch-clamp pipette in perforated patch configuration (with high resistance between the electrode and the internal of the cell), a sharp microelectrode (with high resistance due to the small size of the tip opening), or were using electrical equipment or physical equipment with amplifier limitations, one could imagine that capacitance in the recording system would overcome the rapid swings in cellular potential and result in "noisy" recordings similar to those pictured above. They're not physiological, but perhaps they're a good electrical model of what it's like trying to measure a physiological voltage in a messy and difficult to control experimental environment.

This project was an interesting exercise in analog land, and is completed sufficiently to allow me to move toward my initial goal: creating advanced action potential detection and measurement circuitry. There are many tweaks which may improve this circuit, but as it is good enough for my needs I am happy to leave it right where it is. If you decide to build a similar circuit (or a vastly different circuit to serve a similar purpose), send me an email! I'd love to see what you came up with.

UPDATE: add a microcontroller

I enhanced this project by creating a microcontroller controlled action potential generator. That article is here: https://www.swharden.com/wp/2017-08-20-microcontroller-action-potential-generator/

Markdown source code last modified on January 18th, 2021
---
title: Action Potential Generator Circuit
date: 2017-08-12 23:04:32
tags: science, circuit, microcontroller, python
---

# Action Potential Generator Circuit

__Few biological cells are as interesting to the electrical engineer as the neuron. Neurons are essentially capacitors (with a dielectric cell membrane separating conductive fluid on each side) with parallel charge pumps, leak currents, and nonlinear voltage-dependent currents. __When massively parallelized, these individual functional electrical units yield complex behavior and underlie consciousness. The study of the electrical properties of neurons ([neurophysiologically](https://en.wikipedia.org/wiki/Neurophysiology), a subset of [electrophysiology](https://en.wikipedia.org/wiki/Electrophysiology)) often involves the development and use of sensitive electrical equipment aimed at studying these small potentials produced by neurons and currents which travel through channels embedded in their membranes. It seems neurophysiology has gained an emerging interest from the hacker community, as evidenced by the success of [Back Yard Brains](https://backyardbrains.com/), projects like the [OpenEEG](http://openeeg.sourceforge.net/doc/hw/), and Hack-A-Day's recent feature [_The Neuron - a Hacker's Perspective_](http://hackaday.com/2017/06/02/the-neuron-a-hackers-perspective/).

While contemplating designs for action potential detection and analysis circuitry, I realized that it would be beneficial to be able to _generate_ action-potential-like waveforms on my workbench. The circuit I came up with to do this is a fully analog (technically mixed signal) action potential generator which produces lifelike action potentials.

![](https://www.youtube.com/embed/MBwQrIVJqfs)

__Cellular Neurophysiology for Electrical Engineers (in 2 sentences):__ Neuron action potentials (self-propagating voltage-triggered depolarizations) in individual neurons are measured in scientific environments using single cell recording tools such as [sharp microelectrodes](http://www.scholarpedia.org/article/Intracellular_recording) and [patch-clamp pipettes](https://en.wikipedia.org/wiki/Patch_clamp). Neurons typically rest around -70mV and when depolarized (typically by external excitatory input) above a threshold they engage in a self-propagating depolarization until they reach approximately +40mV, at which time a self-propagating repolarization occurs (often over-shooting the initial rest potential by several mV), then the cell slowly returns to the rest voltage so after about 50ms the neuron is prepared to fire another action potential. Impassioned budding electrophysiologists may enjoy further reading _[Active Behavior of the Cell Membrane](http://www.bem.fi/book/04/04.htm) _and [_Introduction to Computational Neuroscience_.](http://ecee.colorado.edu/~ecen4831/cnsweb/cns1.html)

<div class="text-center img-border">

![](a.gif)

</div>

__The circuit I describe here produces waveforms which visually mimic action potentials__ rather than serve to replicate the exact conductances real neurons employ to exhibit their complex behavior. It is worth noting that numerous scientists and engineers have designed more physiological electrical representations of neuronal circuitry using discrete components. In fact, the [_Hodgkin-Huxley model_](https://en.wikipedia.org/wiki/Hodgkin%E2%80%93Huxley_model) of the initiation and propagation of action potentials earned Alan Hodgkin and Andrew Huxley the Nobel Prize in Physiology and Medicine in 1936. Some resources on the internet describe how to design lifelike action potential generating circuits by mimicking the endogenous ionic conductances which underlie them, notably _[Analog and Digital Hardware Neural Models](https://people.ece.cornell.edu/land/PROJECTS/NeuralModels/)_, [_Active Cell Model_](https://courses.cit.cornell.edu/bionb442/labs/f2007/lab6.html), and [_Neuromorphic Silicon Neuron Circuits_](http://journal.frontiersin.org/article/10.3389/fnins.2011.00073/full). My goal for this project is to create waveforms which _resemble_ action potentials, rather than waveforms which truly _model_ them. I suspect it is highly unlikely I will earn a Nobel Prize for the work presented here.

<div class="text-center img-border">

[![](circuit_hand_thumb.jpg)](circuit_hand.jpg)

</div>

__The analog action potential simulator circuit I came up with creates a continuous series action potentials.__ This is achieved using a [555 timer](https://en.wikipedia.org/wiki/555_timer_IC) (specifically the [NE555](http://www.ti.com/lit/ds/slfs022i/slfs022i.pdf)) in an astable configuration to provide continuous square waves (about 6 Hz at about 50% duty). The rising edge of each square wave is isolated with a diode and used to charge a capacitor*. While the charge on the capacitor is above a certain voltage, an [NPN transistor](https://en.wikipedia.org/wiki/Bipolar_junction_transistor) (the [2N3904](https://www.onsemi.com/pub/Collateral/2N3903-D.PDF)) allows current to flow, amplifying this transient input current. The capacitor* discharges predictably ([as an RC circuit](https://en.wikipedia.org/wiki/RC_circuit)) through a leak resistor. A large value leak resistor slows the discharge and allows that signal's transistor to flow current for a longer duration. By having two signals (fast and slow) using RC circuits with different resistances (smaller and larger), the transistors are on for different durations (shorter and longer). By making the short pulse positive (using the NPN in [common collector configuration](https://en.wikipedia.org/wiki/Common_collector)) and the longer pulse negative (using the NPN in [common emitter configuration](https://en.wikipedia.org/wiki/Common_emitter)), a [resistor voltage divider](https://en.wikipedia.org/wiki/Voltage_divider) can be designed to scale and combine these signals into an output waveform a few hundred mV in size with a 5V power supply. Pictured below is the output of this circuit realized on a breadboard. The blue trace is the output of the 555 timer.

<div class="text-center img-border">

[![](scope_b_thumb.jpg)](scope_b.jpg)
[![](scope_a_thumb.jpg)](scope_a.jpg)

</div>

Between the capacitance of the rectification diode, input capacitance of the transistor, and stray parasitic capacitance from the physical construction of my wires and the rails on my breadboard, there is sufficient capacitance to accumulate charge which can be modified by changing the value of the leak resistor.


<div class="text-center img-border">

[![](breadboard_thumb.jpg)](breadboard.jpg)

</div>

__This circuit produces similar output when simulated.__ I'm using [LTspice](http://www.linear.com/designtools/software/) (free) to simulate this circuit. The circuit shown is identical to the one hand-drawn and built on the breadboard, with the exception that an additional 0.1 µF capacitor to ground is used on the output to smooth the signal. On the breadboard this capacitance-based low-pass filtering already exists due to the capacitive nature of the components, wires, and rails.

<div class="text-center img-border">

[![](circuit_thumb.jpg)](circuit.png)
[![](sim_trace2_thumb.jpg)](sim_trace2.png)
[![](sim_trace_thumb.jpg)](sim_trace.png)

</div>

__A few improvements naturally come to mind__ when considering this completed, functional circuit:

* **Action potential frequency:** The resistor/capacitor network on the 555 timer determines the rate of square pulses which trigger action potentials. Changing these values will cause a different rate of action potential firing, but I haven't attempted to push it too fast and suspect the result would not be stable is the capacitors are not given time to fully discharge before re-initiating subsequent action potentials.

* **Microcontroller-triggered action potentials:** Since action potentials are triggered by any 5V rising edge signal, it is trivially easy to create action potentials from microcontrollers! You could create some very complex firing patterns, or even "reactive" firing patterns which respond to inputs. 

For example, add a <a href="https://www.adafruit.com/product/439">TSL2561 I2C digital light sensor</a> and you can have a light-to-frequency action potential generator!

* **Adjusting size and shape of action potentials:** Since the waveform is the combination of two waveforms, you can really only adjust the duration (width) or amplitude (height) of each individual waveform, as well as the relative proportion of each used in creating the summation. Widths are adjusted by changing the leak resistor on the base of each transistor, or by adding additional capacitance. Amplitude and the ratio of each signal may be adjusted by changing the ratio of resistors on the output resistor divider.

* **Producing -70 mV (physiological) output:** The current output is electirically decoupled (through a series capacitor) so it can float at whatever voltage you bias it to. Therefore, it is easy to "pull" in either direction. Adding a 10k potentiometer to bias the output is an easy way to let you set the voltage. A second potentiometer gating the magnitude of the output signal will let you adjust the height of the output waveform as desired.

* **The 555 could be replaced by an inverted ramp (sawtooth):** An inverted ramp / sawtooth pattern which produces rapid 5V rising edges would drive this circuit equally well. A <a href="http://www.learningaboutelectronics.com/Articles/Ramp-generator-circuit-with-transistors.php">fully analog ramp generator circuit</a> can be realized with 3 transistors: essentially a constant current capacitor charger with a threshold-detecting PNP/NPN discharge component.

* **This action potential is not all-or-nothing:** In real life, small excitatory inputs which fail to reach the action potential threshold do not produce an action potential voltage waveform. This circuit uses 5V rising edges to produce action potential waveforms. However, feeding a 1V rising edge would produce an action potential 1/5 the size. This is not a physiological effect. However, it is unlikely (if not impossible) for many digital signal sources (i.e., common microcontrollers) to output anything other than sharp rising edge square waves of fixed voltages, so this is not a concern for my application.

* **Random action potentials:** When pondering how to create randomly timed action potentials, the issue of how to generate random numbers arises. This is surprisingly difficult, especially in embedded devices. If a microcontroller is already being used, consider <a href="http://makezine.com/projects/really-really-random-number-generator/">Make's write-up on the subject</a>, and I think personally I would go with a <a href="http://holdenc.altervista.org/avalanche/">transistor-based avalanche nosie generator</a> to create the randomness.

* **A major limitation is that irregularly spaced action potentials have slightly different amplitudes.**I found this out the next day when I created a hardware random number generator (yes, that happened) to cause it to fire regularly, missing approximately half of the action potentials. When this happens, breaks in time result in a larger subsequent action potential. There are several ways to get around this, but it's worth noting that the circuit shown here is best operated around 6 Hz with only continuous regularly-spaced action potentials.

<div class="text-center img-border img-medium">

[![](File_000-2_thumb.jpg)](File_000-2.jpeg)

</div>

__In the video I also demonstrate how to record the output of this circuit using a high-speed (44.1 kHz) 16-bit analog-to-digital converter you already have (the microphone input of your sound card).__ I won't go into all the details here, but below is the code to read data from a WAV file and plot it as if it were a real neuron. The graph below is an _actual recording_ of the circuit described here using the microphone jack of my sound card.

```python
import numpy as np
import matplotlib.pyplot as plt
Ys = np.memmap("recording.wav", dtype='h', mode='r')[1000:40000]
Ys = np.array(Ys)/max(Ys)*150-70
Xs = np.arange(len(Ys))/44100*1000
plt.figure(figsize=(6,3))
plt.grid(alpha=.5,ls=':')
plt.plot(Xs,Ys)
plt.margins(0,.1)
plt.title("Action Potential Circuit Output")
plt.ylabel("potential (mV)")
plt.xlabel("time (ms)")
plt.tight_layout()
plt.savefig("graph.png")
#plt.show()

```

<div class="text-center img-medium">

[![](graph_thumb.jpg)](graph.png)

</div>

__Let's make some noise!__ Just to see what it would look like, I created a circuit to generate slowly drifting random noise. I found this was a non-trivial task to achieve in hardware. Most noise generation circuits create random signals on the RF scale (white noise) which when low-pass filtered rapidly approach zero. I wanted something which would slowly drift up and down on a time scale of seconds. I achieved this by creating 4-bit pseudo-random numbers with a shift register ([74HC595](https://www.sparkfun.com/datasheets/IC/SN74HC595.pdf)) clocked at a relatively slow speed (about 200 Hz) having essentially random values on its input. I used a [74HC14](https://assets.nexperia.com/documents/data-sheet/74HC_HCT14.pdf) inverting buffer (with Schmidt trigger inputs) to create the low frequency clock signal (about 200 Hz) and an extremely fast and intentionally unstable square wave (about 30 MHz) which was sampled by the shift register to generate the "random" data. The schematic illustrates these points, but note that I accidentally labeled the 74HC14 as a 74HC240. While also an inverting buffer the [74HC240](https://assets.nexperia.com/documents/data-sheet/74HC_HCT240.pdf) will not serve as a good RC oscillator buffer because it does not have Schmidt trigger inputs.

An inverting buffer created a fast and a slow clock to produce 4-bit pseudo-random numbers:
<div class="text-center img-border img-medium">

[![](File_000-5_thumb.jpg)](File_000-5.jpeg)

</div>

reminder how an inverting buffer can act as an oscillator:
<div class="text-center">

[![](schmitt-trigger-osc_thumb.jpg)](schmitt-trigger-osc.png)

</div>

the full circuit realized on the breadboard:
<div class="text-center img-border">

[![](File_004_thumb.jpg)](File_004.jpeg)

</div>

output of the 4-bit pseudo-random number generator:
<div class="text-center img-border">

[![](File_001_thumb.jpg)](File_001.jpeg)

</div>

4-bit output smoothed through a single-stage RC filter:
<div class="text-center img-border">

[![](File_002_thumb.jpg)](File_002.jpeg)

</div>

noise combined with action potential waveforms:
<div class="text-center img-border">

[![](File_003_thumb.jpg)](File_003.jpeg)

</div>

__The addition of noise was a success, from an electrical and technical sense.__ It isn't particularly physiological. Neurons would fire differently based on their resting membrane potential, and the peaks of action potential should all be about the same height regardless of the resting potential. _However_ if one were performing an electrical recording through a patch-clamp pipette in perforated patch configuration (with high resistance between the electrode and the internal of the cell), a sharp microelectrode (with high resistance due to the small size of the tip opening), or were using electrical equipment or physical equipment with amplifier limitations, one could imagine that capacitance in the recording system would overcome the rapid swings in cellular potential and result in "noisy" recordings similar to those pictured above. They're not physiological, but perhaps they're a good electrical model of what it's like trying to measure a physiological voltage in a messy and difficult to control experimental environment.

__This project was an interesting exercise in analog land,__ and is completed sufficiently to allow me to move toward my initial goal: creating advanced action potential detection and measurement circuitry. There are many tweaks which may improve this circuit, but as it is good enough for my needs I am happy to leave it right where it is. If you decide to build a similar circuit (or a vastly different circuit to serve a similar purpose), send me an email! I'd love to see what you came up with.

## UPDATE: add a microcontroller

I enhanced this project by creating a [microcontroller controlled action potential generator](https://www.swharden.com/wp/2017-08-20-microcontroller-action-potential-generator/). That article is here: <https://www.swharden.com/wp/2017-08-20-microcontroller-action-potential-generator/>
August 2nd, 2017

TTL Triggered Stimulus Generator

I was presented with a need to rapidly develop a pulse generator to take a TTL input and output a programmable output (for now 0.1 ms pulses at 20 Hz for as long as the input is high). I achieved this with a one-afternoon turnaround and the result looks great! This post documents the design and fabrication of this prototype device, with emphasis placed on design considerations and construction technique.

By stocking large quantities of frequently-used items, inventors can build beautiful and functional prototypes for new ideas at the drop of a hat. While it's easy to inexpensively accumulate tens of thousands of passive components (resistors, capacitors, etc.), it's the slightly more expensive components that people tend to order only when they need it for a project. However, paying high shipping rates or waiting months for items to arrive from overseas dramatically increases the barrier for initiating new projects. In my own workshop I have noticed that stocking large volumes of slightly more costly items (inductors, microcontrollers, connectors, enclosures, LED bezels, etc.) lowers the barrier for me to start new projects, and has proved to be a good investment! Now I can build a product on the same day that I have the idea! Today's idea takes the form of a TTL-controlled pulse generator for physiology applications.

I designed the enclosure before I designed the circuit. Metal enclosures are always expensive compared to their plastic counterparts. Steel enclosures are difficult to drill, and aluminum enclosures are expensive. My most cringe-worthy stocking expenditure is ordering metal enclosures in quantities of 10+. The last I checked this specific one is listed as, "Aluminum Instrument Box Enclosure Case+Screw For Project Electronic 26X71X110MM" and is a little under $4 each. Brass hex stand-off nuts and black steel screws don't exactly match the aluminum, but they're what I had on hand. I knew I would need power and a BNC input and output, so I put those 3 on the back. I wasn't sure about the exact functionality of this device (and it may change after it is initially implemented) but I thought a single button and two LEDs would be a good starting point.

The circuit demonstrates the general flow of this device: a microcontroller-controlled project with a buffered output. I drew this schematic after I finished the build (I kept adding passives here and there as I tested it out) but before I started I knew the gist of how I would organize the project. Mentally I knew that as long as my microcontroller (ATTiny2313) could sense the TTL input and had control over _all _outputs (LEDs and BNC alike), I had a lot of flexibility to control the operation of this device in software. I used a generic LM7805 linear voltage regulator with a few decoupling capacitors to take a who-knows-what input voltage (up to 40V) and turn it into a stable 5V output. Note that both inputs (the BNC TTL input and the push-button) have decoupling capacitors near the microcontroller input pin to aid in debouncing.

I'm leaning on a 74HC541 inverting line driver to clamp the output voltage firmly at TTL levels. The microcontroller (an ATTiny2313) isn't really designed to source of sink much current (I think it's rated to 20 mA max) and I don't know about the input circuitry of the stimulus isolator I intend to control (and don't forget about the impedance of 50-ohm cable). The line driver helps me take some of the pressure off the microcontroller and help me feel better about reliably driving the output BNC.

Should I have optically isolated the input? Well, probably not... the application at hand is low importance. If I wanted to rely on optical isolation I would probably lean on the H11B1 as previously used in my opto-isolated laser build. In retrospect I kind of wish I had just because it would have been cooler!

__I added a header to allow me to program the microcontroller with a programmer configured with test clip grabbers.__ I have an AVR ISP MKII (clone), and building a programming adapter that uses test clips was one of the best decisions I ever made! It makes programming (and the inevitable re-programming) a breeze.

The program isn't too complex. It uses a polling method to continuously check for the state of the input TTL. When it's high, it starts a new cycle (0.1 ms pulse, 49.9ms delay, yielding 20 Hz). The code is ready to add a "mode select" feature (which uses the front-panel push-button to select different stimulation protocols), but that functionality is not included in the example below. Note that a lot of the millisecond and microsecond delays are empirically determined by picking a value and checking its output on the oscilloscope. I should note that absolute timing isn't critical for my application, as long as it's consistent. For this reason I'm not relying on the internal RC clock (which is temperature sensitive), but instead am using an external 20MHz crystal as a time source. It's still temperature sensitive (and so are the loading capacitors on each side of it), but dramatically less so than the RC option. Note that the crystal wasn't in the original photos, but it was added for later photos.

Configure the ATTiny2313 to use an external crystal clock source

@echo off
avrdude -c usbtiny -p t2313 -U lfuse:w:0xff:m -U hfuse:w:0xdf:m -U efuse:w:0xff:m
pause

The core program: main.c

#define F_CPU 20000000UL
#include <avr/io.h>
#include <util/delay.h>

volatile char state=0;

void output_HIGH(){PORTB|=(1<<PB4);}
void output_LOW(){PORTB&=~(1<<PB4);}
void LED1_ON(){PORTB|=(1<<PB3);}
void LED1_OFF(){PORTB&=~(1<<PB3);}
void LED2_ON(){PORTB|=(1<<PB2);}
void LED2_OFF(){PORTB&=~(1<<PB2);}

void singlePulse_20Hz_100us(){
    output_HIGH();
    _delay_us(100);
    output_LOW();
    _delay_us(900);
    LED2_ON();
    _delay_ms(20);
    LED2_OFF();
    _delay_ms(28);
    _delay_us(920);
}

void poll(){
    if ((PIND&(1<<PD4))){singlePulse_20Hz_100us();}
}

int main(void){
    DDRB=255; // all outputs
    DDRD=0; // all inputs
    PORTD=(1<<PD5); // pull front button high
    LED1_ON();
    for(;;){
        poll();
    }
}

Compile and load the ATTiny2313

@echo off
del *.elf
del *.hex
avr-gcc -mmcu=attiny2313 -Wall -Os -o main.elf main.c -w
avr-objcopy -j .text -j .data -O ihex main.elf main.hex
pause
avrdude -c usbtiny -p t2313 -U flash:w:"main.hex":a

I didn't think to check my height profile! I got lucky, and things fit fine. Socketed ICs can be close calls, and so can vertically-installed electrolytic capacitors. Now that it was programmed and everything fit, it was time to seal it up and make labels.

The finished product looks great! Never underestimate the power of clear labels and square outlines. Following deployment, a couple screws will let me open it up and access the programming header in case I need to change the stimulation protocols stored in the microchip. I am pleased with how professional of a result I was able to achieve in one sitting! I look forward to seeing how this device works for my application.

PS: Microcontroller code for this project (and many others) is stored in my ever-growing AVR-Projects GitHub page: https://github.com/swharden/AVR-projects

PPS: I smiled when a Google search revealed the PulsePal, "A low-cost programmable pulse generator for physiology and behavior".

Markdown source code last modified on January 18th, 2021
---
title: TTL Triggered Stimulus Generator
date: 2017-08-02 21:08:00
tags: circuit, microcontroller
---

# TTL Triggered Stimulus Generator

__I was presented with a need to rapidly develop a pulse generator to take a TTL input and output a programmable output (for now 0.1 ms pulses at 20 Hz for as long as the input is high).__ I achieved this with a one-afternoon turnaround and the result looks great! This post documents the design and fabrication of this prototype device, with emphasis placed on design considerations and construction technique. 

<div class="text-center img-border">

[![](pics-10017_thumb.jpg)](pics-10017.jpg)

</div>

**By stocking large quantities of frequently-used items, inventors can build beautiful and functional prototypes for new ideas at the drop of a hat.** While it's easy to inexpensively accumulate tens of thousands of passive components (resistors, capacitors, etc.), it's the slightly more expensive components that people tend to order only when they need it for a project. However, paying high shipping rates or waiting months for items to arrive from overseas dramatically increases the barrier for initiating new projects. In my own workshop I have noticed that stocking large volumes of slightly more costly items (inductors, microcontrollers, connectors, enclosures, LED bezels, etc.) lowers the barrier for me to start new projects, and has proved to be a good investment! Now I can build a product _on the same day that I have the idea_! Today's idea takes the form of a TTL-controlled pulse generator for physiology applications.

<div class="text-center img-border">

[![](pics-10000_thumb.jpg)](pics-10000.jpg)
[![](pics-10001_thumb.jpg)](pics-10001.jpg)

</div>

__I designed the enclosure before I designed the circuit.__ Metal enclosures are always expensive compared to their plastic counterparts. Steel enclosures are difficult to drill, and aluminum enclosures are expensive. My most cringe-worthy stocking expenditure is ordering metal enclosures in quantities of 10+. The last I checked this specific one is listed as, "Aluminum Instrument Box Enclosure Case+Screw For Project Electronic 26X71X110MM" and is a little under $4 each. Brass hex stand-off nuts and black steel screws don't exactly match the aluminum, but they're what I had on hand. I knew I would need power and a BNC input and output, so I put those 3 on the back. I wasn't sure about the exact functionality of this device (and it may change after it is initially implemented) but I thought a single button and two LEDs would be a good starting point.

<div class="text-center img-border">

[![](schem_thumb.jpg)](schem.jpg)

</div>

__The circuit demonstrates the general flow of this device: a microcontroller-controlled project with a buffered output.__ I drew this schematic _after_ I finished the build (I kept adding passives here and there as I tested it out) but before I started I knew the gist of how I would organize the project. Mentally I knew that as long as my microcontroller ([ATTiny2313](http://www.atmel.com/Images/Atmel-2543-AVR-ATtiny2313_Datasheet.pdf)) could sense the TTL input and had control over _all _outputs (LEDs and BNC alike), I had a lot of flexibility to control the operation of this device in software. I used a generic [LM7805](http://ee-classes.usc.edu/ee459/library/datasheets/LM7805.pdf) linear voltage regulator with a few decoupling capacitors to take a who-knows-what input voltage (up to 40V) and turn it into a stable 5V output. Note that both inputs (the BNC TTL input and the push-button) have [decoupling capacitors](https://en.wikipedia.org/wiki/Decoupling_capacitor) near the microcontroller input pin to aid in [debouncing](http://www.labbookpages.co.uk/electronics/debounce.html).

<div class="text-center img-border">

[![](pics-10004_thumb.jpg)](pics-10004.jpg)
[![](pics-10003_thumb.jpg)](pics-10003.jpg)

</div>

__I'm leaning on a [74HC541 inverting line driver](http://www.ti.com/lit/ds/symlink/cd74hct540.pdf) to clamp the output voltage firmly at TTL levels.__ The microcontroller (an [ATTiny2313](http://www.atmel.com/Images/Atmel-2543-AVR-ATtiny2313_Datasheet.pdf)) isn't really designed to source of sink much current (I think it's rated to 20 mA max) and I don't know about the input circuitry of the stimulus isolator I intend to control (and [don't forget about the impedance of 50-ohm cable](http://www.electronics-tutorials.ws/inductor/ac-inductors.html)). The line driver helps me take some of the pressure off the microcontroller and help me feel better about reliably driving the output BNC.

__Should I have optically isolated the input?__ Well, probably not... the application at hand is low importance. If I wanted to rely on optical isolation I would probably lean on the [H11B1](https://www.vishay.com/docs/83609/h11b1.pdf) as previously used in my [opto-isolated laser build](https://www.swharden.com/wp/2016-07-28-opto-isolated-laser-controller-build/). In retrospect I kind of wish I had just because it would have been cooler!

<div class="text-center img-border">

[![](pics-10008_thumb.jpg)](pics-10008.jpg)
[![](pics-10007_thumb.jpg)](pics-10007.jpg)

</div>

__I added a header to allow me to program the microcontroller with a programmer [configured with test clip grabbers](https://www.ebay.com/sch/i.html?_nkw=test+clip+grabber).__ I have an AVR ISP MKII (clone), and building a programming adapter that uses test clips was one of the best decisions I ever made! It makes programming (and the inevitable re-programming) a breeze.

<div class="text-center img-border">

[![](pics-10010_thumb.jpg)](pics-10010.jpg)
[![](pics-10009_thumb.jpg)](pics-10009.jpg)

</div>

__The program isn't too complex.__ It uses a polling method to continuously check for the state of the input TTL. When it's high, it starts a new cycle (0.1 ms pulse, 49.9ms delay, yielding 20 Hz). The code is ready to add a "mode select" feature (which uses the front-panel push-button to select different stimulation protocols), but that functionality is not included in the example below. Note that a lot of the millisecond and microsecond delays are empirically determined by picking a value and checking its output on the oscilloscope. I should note that absolute timing isn't critical for my application, as long as it's consistent. For this reason I'm not relying on the internal RC clock (which is temperature sensitive), but instead am using an external 20MHz crystal as a time source. It's still temperature sensitive (and so are the loading capacitors on each side of it), but dramatically less so than the RC option. Note that the crystal wasn't in the original photos, but it was added for later photos.

__Configure the ATTiny2313 to use an external crystal clock source__

```bash
@echo off
avrdude -c usbtiny -p t2313 -U lfuse:w:0xff:m -U hfuse:w:0xdf:m -U efuse:w:0xff:m
pause
```

### The core program: main.c

```c
#define F_CPU 20000000UL
#include <avr/io.h>
#include <util/delay.h>

volatile char state=0;

void output_HIGH(){PORTB|=(1<<PB4);}
void output_LOW(){PORTB&=~(1<<PB4);}
void LED1_ON(){PORTB|=(1<<PB3);}
void LED1_OFF(){PORTB&=~(1<<PB3);}
void LED2_ON(){PORTB|=(1<<PB2);}
void LED2_OFF(){PORTB&=~(1<<PB2);}

void singlePulse_20Hz_100us(){
    output_HIGH();
    _delay_us(100);
    output_LOW();
    _delay_us(900);
    LED2_ON();
    _delay_ms(20);
    LED2_OFF();
    _delay_ms(28);
    _delay_us(920);
}

void poll(){
    if ((PIND&(1<<PD4))){singlePulse_20Hz_100us();}
}

int main(void){
    DDRB=255; // all outputs
    DDRD=0; // all inputs
    PORTD=(1<<PD5); // pull front button high
    LED1_ON();
    for(;;){
        poll();
    }
}
```

### Compile and load the ATTiny2313

```bash
@echo off
del *.elf
del *.hex
avr-gcc -mmcu=attiny2313 -Wall -Os -o main.elf main.c -w
avr-objcopy -j .text -j .data -O ihex main.elf main.hex
pause
avrdude -c usbtiny -p t2313 -U flash:w:"main.hex":a
```

__I didn't think to check my height profile!__ I got lucky, and things fit fine. Socketed ICs can be close calls, and so can vertically-installed electrolytic capacitors. Now that it was programmed and everything fit, it was time to seal it up and make labels.

<div class="text-center img-border">

[![](pics-10014_thumb.jpg)](pics-10014.jpg)
[![](pics-10015_thumb.jpg)](pics-10015.jpg)

</div>

__The finished product looks great!__ Never underestimate the power of [clear labels and square outlines](http://www.qsl.net/pa2ohh/tlabels.htm). Following deployment, a couple screws will let me open it up and access the programming header in case I need to change the stimulation protocols stored in the microchip. I am pleased with how professional of a result I was able to achieve in one sitting! I look forward to seeing how this device works for my application.

<div class="text-center img-border">

[![](pics-10017_thumb.jpg)](pics-10017.jpg)
[![](pics-10018_thumb.jpg)](pics-10018.jpg)

</div>

PS: Microcontroller code for this project (and many others) is stored in my ever-growing AVR-Projects GitHub page: [_https://github.com/swharden/AVR-projects_](https://github.com/swharden/AVR-projects)

PPS: I smiled when a Google search revealed the [PulsePal](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4263096/pdf/fneng-07-00043.pdf), "A low-cost programmable pulse generator for physiology and behavior".
Pages