Sunday, January 14, 2024

Thermostat Monitor System

 Summary

I was having trouble keeping track of which of my house's three heating zones was activating the boiler and pump at any given time. So I built a device to monitor which zone was calling for heat. 

Zone activation sensor system, with indicator lights

I used Prometheus to keep track of zone activation over time. 


Two days of zone activation lots via Prometheus. The temperature dropped over the last 8 hours, so you can see the upper floor is constantly active.

Build Process

My house has a hot-water heating system with three zones, each controlled by a valve like this one


Zone controller, photo via Amazon.com

There are three zones, each managed with one of these zone valves. Each zone valve is controlled by a thermostat.

Boiler and control valves 

These control values take a 24 Volt Alternating Current (VAC) input. When a thermostat calls for heat, it does so by sending 24 VAC to its control valve. The control valve opens the physical valve and sends 24 VAC to the boiler.


Diagram of power flow through the boiler system. The thermostat is effectively a switch that, when activated, closes the connection between the 24VAC transformer and the zone control valve.

The boiler system uses a transformer to convert the standard 120 VAC wall power to the 24 VAC used for the heating system. 

Each thermostat has its own settings for when to call for heat. The basic function of a thermostat is to track a set point (e.g., 68 degrees) and a current temperature (e..g, 70 degrees). If the current temperature is below the set point (in the case of a heating thermostat, e.g., 65 degrees) the thermostat will close the circuit from the transformer to the control valve. The control valve will open the pipe for hot water to the associated region of the house and will also close a circuit to provide 24 VAC to the boiler. This, in turn, causes the boiler to heat up and the water pump to turn on. 

Side note: newer thermostats like a Google Nest Thermostat require a "C-Wire." The C-Wire or "Common-Wire" is a neutral wire that allows the thermostat to use the 24 VAC supplied by the transformer. Without a C-Wire, the thermostat is just like a switch. 

Approach

I wanted to monitor which thermostat was calling for heat at what time and for how long. I also did not want to modify the heating system (I definitely don't want to break out boiler in the dead of winter!) To do this, I took advantage of a handy property of alternating current: you can indirectly measure it. Alternating current creates a magnetic field, which you can measure without directly touching the wires carrying that current. 

I purchased some of these clamp sensors (you can find them on eBay), which can measure alternating current on a wire when clamped around it. 

Clamp current sensor photo via eBay

I connected these to an Adafruit Metro ESP-32 S2, and used its analog ports to measure the voltage reported by each clamp sensor. 


Adafruit Metro ESP32-S2 via Adafruit


I found a prometheus exporter for CircuitPython on github. I had to make one modification to make it work with my board (my fork on GitHub).

I wrote a CircuitPython program to read the analog signals from the clamp sensors, then make that reading available to my prometheus server via a simple HTTP endpoint. Setting up CircuitPython took a bit of work, let me know in a comment if you would like a blog post about that process. 

The endpoint looks like this when viewed with a web browser:

Statistics reported by the thermostat monitor system

I configured my Prometheus server to scrape the thermostat statistics every 15 seconds. To generate a graph of when thermostats are active, I can use a query like:

environment_thermostat_upper_heat{job="thermostat"}

A query and associated results viewed in the Prometheus graph viewer

In this case we can see that the upper floor heat was active almost continuously over the past ten hours. This is not surprising, since an arctic vortex just arrived. 

Conclusion

CircuitPython is really handy for making Internet of Things (IoT) devices that work with an aggregator like Prometheus. 




Sunday, November 19, 2023

Analog to Digital Adapter for Alesis Turbo Mesh Kick Drum


Earlier this year we bought an Alesis Turbo Mesh electric drum kit. The kit works well, but my drum teacher advised me to switch from the spring/trigger kick pedal to a proper kick drum pedal. I ordered a kick pedal and sensor from eBay, but was bummed to discover that the Turbo Mesh kit only works with a "switch" style kick pedal

Fig 1: The Goal (Images via Alesis.com)

The Alesis Turbo Mesh kick pedal connects using a standard 1/4" mono jack. When the pedal is depressed, the two conductors in the mono jack are connected and the control module recognizes it as a "kick." You can easily demonstrate this by touching both parts of the kick drum 1/4" connector with a wire. 

The higher quality kick sensors are analog, meaning instead of a binary on/off (like a switch) they change the voltage on a signal wire, which is interpreted by a microcontroller. This is how electric drum kits get different volume levels for different intensity strikes. In this case, I needed to convert the analog signal from the kick pedal to a binary on/off before the signal reached the Alesis drum controller. 

Fig 2: Connections (Images via Alesis.com and Sparkfun.com)

I bought an Arduino Redboard from Sparkfun for about $20. The Redboard has analog and digital I/O pins, making it an easy choice for this project. I also had some relays left over from the Fort Lock project. I bought a couple of 1/4" jacks. 

Part List

Digital Resources

Process

Redboard Setup

The Arduino IDE didn’t work out of the box with the Redboard, which requires an additional support package. 

I had to add this line to the Arduino IDE preferences to add additional boards to support. 

https://raw.githubusercontent.com/sparkfun/Arduino_Apollo3/main/package_sparkfun_apollo3_index.json

After updating the preferences, I was able to select the Redboard and connect!

Reading Analog Values

I connected the kick drum sensor to an analog pin on the Redboard and used the example serial reader code to read the voltage levels from the kick sensor.

Fig 3: Serial reader sample code from Sparkfun

Using the built-in LED I was able to reliably detect "kicks" using the analog sensor and looking for sudden increases in voltage. The Arduino software does not provide time guarantees, but the main loop ran fast enough that any latencies were not noticeable. 

Writing Digital Values

To trigger the "kick" I needed to "connect" the two conductors from the Alesis control unit's kick cable. There were a couple of options available to accomplish this. Using a multimeter, I found that the Voltage difference between the two kick sensor conductors was 3 Volts. 

To make the signal consistent, I had to create a common ground connection between the drum control unit and my Redboard. I connected a solid wire from the Redboard to part of the breakout connector on the bottom of the drum control unit. 

Fig 4: Connection point to establish common ground


I tried sending 3V directly using the Redboard's digital output pins, but the latency was too high (it sounded like about 300-500 milliseconds). My suspicion is that the Redboard could not supply the voltage quickly enough the internal resistance in the drum control unit. 

To overcome this issue, I used a relay that acted like a switch to close the connection between the two signal lines in the kick drum cable. I can't find the exact product I used (it was from eBay) but it was similar to this (Amazon). I understand from talking with friends that an OpAmp could have achieved the same thing, but I have not learned how to use those yet. 

I designed a simple case in FreeCAD and printed it. 

Fig 5: 3D Model of Enclosure



Fig 6: Wiring Diagram



Saturday, July 8, 2023

Build Log: Keycode Access Control for the Fort

 My sons requested a keycode-based access control system for their backyard fort. I'm pretty happy with what we created. In this blog post I'll talk through the details of what we built and why. 

The keypad and custom mounting bracket

Parts List

Microcontroller

  • Adafruit Metro ESP32-S2
  • The Adafruit Metro ESP32-S2 is a fairly inexpensive ($20) controller that includes wifi and Adafruit's Qwiic I2C connector system. 

Keypad

Electronic Lock



Relay



  • Fuses (PTC Fuse Resettable Fuses 72V 0.5A 500mA RXEF030 Series)
  • Diodes
  • Qwiic Button

Architecture

Requirements

I worked with my friend Peter to develop requirements for the lock. The requirements are written up on github here. In summary, the lock: 
  1. shall not allow a child to get stuck
  2. shall be resistant to critters/teenagers
  3. shall not require physical keys
The current solution meets all three of these requirements. 

Cyberphysical System Models

This project gave me a good excuse to spend time learning the Assume Guarantee REasoning Environment (AGREE). AGREE is a model-based tool that helps you build and verify an argument for correctness in your architecture. 


This specification, written in the Architecture Analysis and Design Language (AADL) describes a magnetic lock actuator that physically actuates (locks) only when power is applied to the system and the lock gets an actuation signal. The first section, "lock_actuator" describes the desired behavior in terms of assumptions and guarantees. The section section, "lock_actuator.magnetic_actuator" describes the state transitions of a particular lock design. 

This type of reasoning is helpful when thinking through design choices. For example, I considered using a solenoid lock like this version from SparkFun: 


But this kind of lock does not meet the conditions described in my assumptions and guarantees. Namely, (as I verified through bench testing) the SparkFun solenoid lock physically actuates (locks) when power is not applied. Such behavior would violate the requirement, "shall not allow a child to get stuck," because a power loss could cause a child to become stuck in the fort.   

Electrical Architecture

Architecture Figure 1: Electrical System

Electrical Summary

A MOSFET relay with a dedicated 12VDC source controls power to the electronic lock. At Peter's recommendation, both sides of the 12VDC supply are protected with resettable fuses to avoid shorting the system (if, for example, something is wired backward). 

The ESP32 board uses one of its General Purpose Input/Output (GPIO) pins to trigger the relay. Input to the ESP32 comes from two sensors (a button and a keypad) both of which are connected via the I2C protocol using Sparkfun's Qwiic cable system (which I absolutely love). 

Flyback Diode

When the magnetic field of the electromagnetic lock collapses, it can induce a current in the reverse of the normal direction. To avoid damaging the microcontroller, I used a Flyback Diode as described on this page. Normally current flows in the B to A direction (illustrated on the figure), but when the lock's power is disengaged, the collapsing magnetic field induces a current in the A to B direction, making a circuit back through the electronic lock to dissipate the remaining power. 

Physical Assembly

The lock I purchased was intended to by mounted on the interior of a door frame. The fort door is an exterior door, so this mounting approach would not work (the lock would be exposed to weather). To mount the lock entirely inside the fort, I welded a "Z" bracket from two pieces of "L" steel. 

Architecture Figure 2: Installation. After taking this photo I had to shim the steel plate (attached to the black painted steel) so that it made good contact with the magnet.


I installed the microcontroller, relay, and exit button in a standard 2 gang electrical box. 

Architecture Figure 3: Exit button and installation box


My son and I designed the keypad mounting bracket in TinkerCAD (this was great practice for him using a micrometer). You can find the .stl file here

Architecture Figure 4: Mounting Bracket for SparkFun QwiicKeypad


Software Architecture

Architecture Figure 5: Software

The software design is pretty simple. As shown in Architecture Figure 5, the microcontroller simply waits for input from either the keypad or the exit button, storing up keypresses to compare to a saved code, resetting if input times out. 

Development Environment

This was my first time using CircuitPython and the Mu Python editor. Overall I'm still on the fence about CircuitPython. It is certainly convenient once it is working, but the set up process is not as straightforward as I'm accustomed to with Arduino. 

1. Install CircuitPython to Board Flash Memory

  • Download esptool (I used version 4.4)
  • Find ESP32 in my device manager (on Windows this was COM8)
  • Write circuitpython bootloader to flash
    • .\esptool-v4.4-win64\esptool-v4.4-win64\esptool.exe --port COM8 --after=no_reset write_flash 0x0 .\tinyuf2-adafruit_metro_esp32s2-0.12.0\combined.bin
Once CircuitPython is loaded on your board's flash memory, the board will appear as a storage device.

2. Install Mu Editor

You can download Mu here. Overall I'm content with Mu, but the CircuitPython model seems to require I only use a single file, "code.py" which makes version control cumbersome.

3. Install Required Libraries

CircuitPython libraries are not the same as regular Python libraries. You can find many CircuitPython libraries here. Copy the files for the devices you are using from the lib directory of the CIrcuitPython library to the lib directory on your board. For example, I copied sparkfun_qwiickeypad.mpy to the lib directory of my board so that I could do:

import board
import sparkfun_qwiickeypad
...
i2c = board.I2C()
keypad = sparkfun_qwiickeypad.Sparkfun_QwiicKeypad(i2c)

Testing

Access Testing

My kids helped me test the lock - both the entry PIN and the exit button work as desired. We also talked through the emergency protocol if they need to leave the fort and the exit button is not working (just unplug it). 

Power Consumption

I used a KASA wifi power outlet adapter to measure power consumption. It holds steady at about 9 Watts for the lock alone (not including the controller). 

Final Thoughts

I learned a ton doing this project. The practice doing circuit design was particularly valuable, as I made (and corrected) lots of mistakes. 




Saturday, June 3, 2023

How I built a stand for the UCS Millennium Falcon


Freestanding Millennium Falcon display with lighting

The Ultimate Collector Series (UCS) Millennium Falcon is an incredible LEGO set. It is absolutely huge, 33" by 23" and 28 pounds. My office has one in our common room, and this year I decided that I wanted to build a stand for it; a standard office table was not doing it justice. 

Acceptable, but boring, way to display the coolest set ever

I started by making paper sketches of stand options. I wanted it to be free standing so that visitors to the office could be greeted in proper geeky fashion. I wanted the 'Falcon to appear to be taking off and easy for viewers to see. 

Initial design (digitized)

I purchased some scrap steel from Coremark Metals and set about making a small scale prototype from 1/4" and 1/8" hot rolled steel. I used Metal Inert Gas (MIG) process welding with the Hobart Handler given to me by my brother Graham, who also makes some pretty cool stuff. 

Small-scale prototype

I was pleased with the small-scale prototype, and my coworkers were too. Bolstered by their feedback and the stability of the small design, I started work on the full size version. I scanned the 'Falcon with Polycam to get measurements and locations of its landing struts, then went back to Coremark to purchase enough steel for the full size version. 

Polycam lets you pull measurements from a 3d model

I purchased about 50 pounds of steel for this project:
  • 1/8" Steel sheet for the landing pad
  • Squares of 1/4" steel bar for the base (to make it heavy)
  • 4' Length of square steel tubing for the upright
I assembled the base first. I used seven square 1/4" steel bars together to make a U shaped base that allowed space for wiring to come down through the upright, with a larger square 1/4" steel bar on top. I drilled a small hole in the center to run wires through. I tacked the small squares together before doing most of the welding, which helped to minimize warping of the steel from welding. 

Base design

I used several additional pieces of 1/4" and 3/16" steel to build a small box to house electronics at the base and cover the gap left at the bottom of the base. I was happy with the upright angle I used on the prototype, so I copied it to the angle to the upright and cut it with an angle grinder. I beveled the edges a little bit to try to minimize the profile of the weld. 

Wiring box

Attaching the landing pad was tricky, as my welding magnets and clamps did not work well to situate the upright. Eventually I made it work.

Full size stand welded and cleaned for painting

I spray painted the stand with a metal primer and glossy black paint (I was excited and probably would have gotten a more durable paint coat if I'd waited longer between primer and paint). I brought it into the office and was pleased with the result.

Fit test

I taped off the locations of the landing struts, then brought it back home again to trim the landing pad and assemble the lighting. I purchased a bunch of pre-wired LEDs and some speaker wire. I set the lights up in three strings, so that I could have multiple lighting settings. I decided to run a positive and negative line for each string to maximize flexibility. 

Trimmed, taped, and wired

I installed a couple of (unnecessarily) large switches at the base, as well as a motion sensor. For the motion sensor I 3D printed an additional mounting bracket, since it had to be located above the base to detect movement. 

Bracket for motion sensor, with rectangular hole at the base for a bar magnet and round hole for wiring. 

I used 1 1/2" bar magnets for several parts of the assembly. First, I used the bar magnets to help anchor the 'Falcon's landing struts so that the force wasn't entirely on the rear struts. Second, I used a bar magnet to attach the motion sensor bracket to the base. Third, I used a bar magnet to hold the battery in place inside the base. Finally, I used bar magnets to attach a steel cover to the wiring chamber of the base. 

I ran blue LEDs to the front and rear of the ship, and red LEDs to the upper gunner's station. I'm pretty happy with the result. 

The gunner's station lights up when people walk by

Everything is powered by a single 9v battery. The only power consumption is from the LEDs and motion sensor, so I am hoping it will last several weeks on one charge. 





 









Sunday, December 11, 2022

Population Dynamics in the Harry Potter Universe

 In Harry Potter and the Chamber of Secrets Hagrid makes an interesting comment. He says

"Most wizards these days are half-blood anyway. If we hadn’t married Muggles we’d’ve died out."

This comment got me thinking about the genetics and population dynamics of the Harry Potter universe. This topic prompted a lot of questions.  

Why is the magic population so small relative to the non-magic population? 

Hagrid's comment implies that there is something inherent in magic users that puts downward pressure on their population. There are a few possible explanations:

  • A genetic effect associated with the same genes that allow magic use also reduces fertility.
  • Certain magic users face cultural pressure not to have children (for example, witches might face pressure not to have children, resulting in more magic using men choosing non-magical partners). 
  • Hagrid could be referring to a specific event, rather than a general trend (for example, say there was a catastrophic event in the past which eliminated a critical mass of magic users).
Let's see if we can isolate one of these potential causes. 

Size of the Magical Population

The size of the magical population is hard to gauge, but it is clearly fairly small. Let's work out an estimate. We know that Hogwarts is the only school of magic in the UK, and that attendance at Hogwarts of another school of magic is (effectively) mandatory for magic users. Based on the size of the great hall, which ostensibly contains all of the students and staff at once, Hogwarts likely has about 200 students ages 11 - 18. 

According to the UK office of statistics, people ages 11-18 made up about 10% of the total population in 1998 (about 5.9m out of 58m people). We know that witches and wizards can live longer than regular people, so let's generously assume people 11-18 make up 5% of the magic population. Based on these assumptions, the total magical population is the UK in 1998 was approximately 4,000 people, or about 0.007% of the total UK population. The UK accounted for approximately 1% of the global population in 1998. This implies a global wizarding population of about 400,000. 

The quidditch world cup is said to have 100,000 attendees in Harry Potter and the Goblet of Fire. The 2002 Football World Cup had about 2,700,000 attendees (27x as many) [5]. The world population in 2002 was 6.2 billion [6], meaning 0.04% of the world population attended the Football World Cup. One could extrapolate that the global wizarding population is 100,000 / 0.0004 = 250,000,000. However, I believe the ease of transportation in the wizarding world means a much higher percentage of the world population attends the quidditich world cup, likely as high as 25%, based on a a global wizarding population of 400,000. 

This population estimate aligns nicely with the apparent scale of the magic economy: magic users in the UK are effectively a small town. The ease of transportation enabled by magic (apparition, flue powder) means that the population can be geographically distributed over a large area without losing the cultural cohesion of a small community. A variety of observations seem consistent with the magical population existing as a distributed small town (or perhaps small nation-state):

  • They have their own currency.
  • They have a small number of businesses (one bank, several restaurants, several specialized stores).
  • They have a single government.
  • Most Magic users seem to prefer (or are limited to?) magic-specific occupations. It is implied that this is a cultural preference, rather than law. 
There a some potential inconsistencies with this view. For example, In 1998 approximately 5% of the UK population was employed in Education. [2] The magical community does not seem to follow this employment demographic, since the staff at Hogwarts is only about 0.25% of the magical population. 

Regardless, I think there are metrics to this concept that deserve more thought. Are there aspects of a small-town culture that put downward pressure on population? 

Magical Ability Propagation

There's general consensus in the literature that magical ability is inherited, though it's unclear whether the trait is expressed on a single gene or multiple, or whether it is dominant or recessive. [3] [4]. This is consistent with the potential for non-magic users to have magical children (e.g., Hermione) and the ability for magic users to have non-magical children (squibs, like Mr. Filch).

Birth Rates Among Magic Users

We don't get to meet many magical families, but with the exception of the Weasley family almost all magic families have only one child. This sample set is also likely skewed toward a higher birth rate, since most of the characters are children. 

Listing all of the adults of an age to have had children:
  • Weasleys - 7 Children
  • Dumbledores - 3 Children
  • Hagrid's Parents - 1 Child (his half brother is not a magic user and does not count)
  • Potters - 1 Child
  • Malfoys - 1 Child
  • Lovegoods - 1 Child
  • Longbottoms - 1 Child
  • McGonogal - 0 Children

Unfortunately there are a lot of characters about whom we don't know enough to make a robust data set.  For example, we aren't told whether McGonogal has siblings or children. There is not enough data to make a meaningful statistical statement about magical birth rates. Anecdotally we can only observe that many adults in the Potter books are childless (or have children that are never mentioned). Only one Hogwarts teacher ever has a child (Lupin). The rest have no mention of children:
  • Dumbledore
  • Hagrid
  • Lockhart
  • McGonogal
  • Slughorn
  • Snape

Education and Autonomy

It is well established that increases in education and (and presumably the resulting autonomy), particularly for women, lead to overall decreases in birth rate [7]. Although the wizarding world seems to have maintained some aspects of the patriarchy of the world at large (the ministers of magic are all male, for example), women in the wizarding world clearly have education and autonomy at a level on par, or ahead of, most modern economies. The birth rate has been steadily decreasing for many years [8].  Was Hagrid making a veiled sexist comment about witches preferring to have fewer children than their muggle counterparts? 


Further Research

There is a lot of research yet to be done. Some ideas:

  • Set up population simulation to try to determine approximate rates for the wizarding world. 
  • Analyze the supply chains for magic users - how do they differ from those of non-magic users? 
  • Is in-breeding a problem among magic users? The preference for "wizard blood" among some users could lead to such situations. 

[1] UK Office For National Statistics

[2] UK Office For National Statistics

[3] Live Science

[4] Muggle Net

[5] Statista

[6] Census

[7] Worldbank

[8] Macrotrends

Thursday, May 13, 2021

Building the Mega Drop

Figure 1: Mega Drop (With Ramp)

About a year ago we took down a beautiful old box elder in our backyard. It grew on a hill behind our sandbox and swingset. It was big, wide, and had lots of large dead limbs that, unfortunately, were a safety risk for our kids (Figure 2). We had the tree cut down, and were left with a large (approximately 3' diameter) stump on our hill. 

Figure 2: The Box Elder


For this project we used almost exclusively either scrap wood or wood from Home Depot's 70% off pile. I don't have a detailed price list, but I'd guess we spent about $40. 

At first we wanted to build a tree fort, so we used some two by fours to make a level building surface on top of the stump, then we added treated decking boards to build a base. Almost immediately we realized that the "floor" we had created would make an awesome drop for our backyard singletrack trail. We extended the surface so that we had a deck leading from the nearby hillside path to the top of the stump. Soren dubbed this setup the "mega drop."

We used a variety of rocks, logs, and assorted backyard construction materials to shore up the design, then we tried it out. 
Figure 3: Ouch

Although the drop was a reasonable height, the hill on which we had to land was sloped in two directions, which made landing rather tricky. After a mix of successful and unsuccessful attempts, we went back to the drawing board. We decided to change from a drop to a steep ramp, which would maintain the excitement of the descent without the frustration of the sketchy landing. 



Saturday, May 8, 2021

How to Fix a Broken XPower Dump Truck Drive Switch

 My son got an XPower "Pulls up to 50 Lbs!" dump truck for his birthday, but within a week it had stopped working. It would turn on and make noise, but would only drive sporadically. It turns out the problem is a somewhat flaky button design with an easy repair: Add a spring to the button

Figure 1: The XPower Dump Truck Power Button Stopped Responding

Disassembly

There are only two kinds of screws used, both with Phillips heads, so disassembly is easy. You have to remove the wheel drive assembly to get to the circuit panel on which the problem switch is located. 

  1. Remove the bottom plate, which includes the battery pack.
  2. Remove the wheel assembly (all four wheels come off in one large assembly). This will reveal the two screws noted below (Figure 2)
  3. Slide the cab off of the gear and motor assembly. The cab slides "up" from the truck's perspective. 
  4. Remove the circuit board, speaker, and bed-closure button (Figure 3). There is no need to disconnect any wires. 
  5. Remove the button (Figure 4)

Figure 2: Remove these Screws to take off the Cab and Circuit Board

Figure 2.5: Configuration of the Gears

Figure 3: The Circuit Board

Figure 4: The "go" Button is the Problem

Figure 5: The Go Button Switch on the Circuit Board

Repair

The problem is that this button can get stuck in the up or down position. To solve this problem, I added a little spring to the button. I used a metal shears to make a little more room inside the button head (Figure 6), then added a small spring (Figure 7). Make sure that the spring is small enough that the button can still make contact with the switch. 

Figure 6: Making a Little Room for the Spring


Figure 7: Positioning of Spring


Figure 8: Success!