Category Archives: Uncategorized

PIC strobe tuner

I decided pretty much all bought guitar tuners are crap. They’re never accurate and they’re useless if there’s any other noise going on. I set out to make something better, and here it is.

Someone did make something like this commercially, but it’s not as cool as mine. I forget what it was.
Edit: Planet Waves CT06

I figured the easiest and best way to accomplish the task of tuning a string is with a strobe light flashing at the right frequency.  What we set out to achieve is some arrangement of flashing LEDs that throws bands of light onto guitar strings. When the string oscillates at the same frequency as the flashing of the light we won’t see any movement because the string is in the same place each time the light flashes. If the string is out of tune the bands of light will appear to wobble. Read about strobe tuners if you’re not keeping up so far.

The easiest way to make lights flash at an accurate but switchable frequency has to be the venerable PIC running off a crystal oscillator. So let’s start there.
I had a PIC 16f819 sitting in my parts bin, and it’s perfect for the job for many reasons. The best part is one of its 3 timers is 16-bit, so we should be able to get super accurate timings with it. It seems a shame to waste the other 2 so I figured why not use 3 LEDs that can be for tuning half-step down or whatever.
A 4 MHz crystal makes the PIC run at an incredibly convenient 1 microsecond instruction cycle, so if we know the frequencies of all our notes it’s trivial to work out how to set the timers. Similarly wiring 3 LEDs to pins on a PIC is about as easy as it gets, so the only hard part is the code…

I won’t go too much into the details, but I can tell you it was a faff getting the 3 timers to interrupt (many “enable” bits need to be set and they’re scattered all through the datasheet). Once they do we just need code to figure out which timer tripped, code to switch strings on a button push, and a bit of magic to compensate for the time spent turning lights on and off.

Our magic numbers are 82.4, 110, 146.8, 196, 246.9 and 329.6 Hz for a guitar. I wasn’t sure at first how I was going to keep track of which note the strobes were flashing at as we cycle through the strings, but then I reasoned that with 3 LEDs we can count to 6 in binary, so when the button is pushed to change we go to a delay loop (that also serves to debounce the button) that lights up LEDs showing the number of the string, from 1-6, that we’re moving to.
I set up the 3 LEDs so that the green runs off the 16-bit timer and always strobes at standard tuning, i.e. EADGBE. The red is always 1/2 step down in case I ever lend this thing to some scruffy guy with long hair from the 1990s, and blue is a little different.

The disadvantage of this strobe setup is it’s impossible to tell if you’re too high or too low without adjusting, so I set the blue LED to flash a little fast, meaning that if blue is the most stationary band don’t go any higher. This should prevent string snappage. The exception is on the E string (string 1), where blue is set a full step down to make drop D quick and easy.

The rest is simple maths, not so simple code and masses and masses of debugging [Thanks Adam!!]. Once it was working I measured the frequencies accurately and tweaked the timer loadings a little. Now it’s probably the most accurate tuner I’ll ever see.

BOM:
3 super-mega-uberbright LEDs + resistors of your choice
1 PIC
1 4 MHz crystal (+small caps per PIC datasheet)
1 offcut of stripboard
1 power switch
1 push button

+ batteries + case = Done.
DSC_0162DSC_0165
The case is some junk, I don’t remember from what. It’s a little bigger than I needed, but that leaves room to store some picks. No puns please.

Advertisements

Solar powered automated plant waterer

My plants are unhappy. I go away a lot and they don’t get looked after. I figured I could do something about this.
First off, I should own up to having solved this problem before. Mk1 used one of these commercially available things

41rfZkmnj5L._SY300_

and a water pump smashed out of a battery powered kids water pistol, a bit of hose and a phone charger. It was plugged into the wall via a timer plug so it was only powered for 15 minutes a day, but the probes still corrode way too fast to be useful long term and the pump draws a lot of electrics (800mA for up to a minute at a time) so it’s constrained to live near a socket. In light of this first success my requirements have been refined. Mk2 must:

1. Water plants when they need it (see how that one’s top of the list)
2. Be able to run from (cheap!) solar power
3. Not electrolyse the soil probes away
4. Cost less than a commercially available alternative

Now, I’ve seen this kind of thing done loads on hackaday. People just grab an arduino/pi/whatever and buy a solenoid valve and call it good. Well requirement 2 kinda dumps on the pi, probably the arduino too, and 2 and 4 both rule out the 12/24V solenoid valve. But we still need some kind of intelligence to meet #3 by limiting the time we run current through wet dirt.
Enter the PIC. It draws like 20 microAmps or something in sleep mode, and even with everything running it’s hardly what you’d call thirsty. These things are ideal for solar power. I went for the 12f683 because it’s small and cheap, but almost any would do.

We also need a bespoke power supply. This part was fairly easy. It’s 4 solar garden lights, costing £1 each, with the guts removed. Maplin part# N08FX to be precise.

solar-powered-stainless-steel-post-light

Each contains a AAA NiMH cell and a crap solar panel (and a white LED and switch, together worth about 80p, that I get to keep). The solar cells are connected in series, the AAAs are connected in series, and the solar cells charge the batteries through a diode so they can’t drain at night. I added a USB connector to make wiring easy and universal.
I figured a custom stand would be a nice touch, so I drew and printed this too.

legs1 post stand

DSC_0155 DSC_0156

Now we’re going to need a valve that can control water with 5 Volts and, to be sensible, not draw much more than an Amp. I searched extensively and found, never mind requirement #4, you just can’t buy that. This is where the project hit its first wall. I’d have to invent a valve.
I played with coils of wire wound on steel bolts lifting steel ball bearings. The results were encouraging, so I started trying to design a valve seat that a ball bearing would seal against. I tried moulding stuff out of silicone sealant but the results were too grabby for the solenoid to pull against. I tried drilling very very neat holes in all sorts of things but nothing was perfectly round enough to seal. I tried 3d printing parts but they all leaked, no matter what I tried. I tried 3d printing something that would take an o-ring that would seat a ball bearing. No joy – the o-ring always distorted just enough to leak.

Then, one day, as I was washing the dishes and ran out of detergent I spotted something. The cap on the bottle had a perfectly made toroidal hole that would work perfectly. A quick bit of drawing and I had a valve assembly that would open and close and control water with electricity. This felt like a hell of an achievement. I’m still quite proud of it.

valve1 DSC_0153
DSC_0151

I left it for a while to see that it held water long term, and realised the ball bearing I’d used wasn’t going to work. It had rusted badly in a week. A quick search threw up a ball that is stainless but still magnetic, (that’d be 420 stainless), so I ordered a pack of 10. It worked, but not as well. Anything over an inch or two of water and the attraction was too weak to pull against the pressure. So I unwound half of the coil, snipped it and re wound it in parallel with other half to give some more Amp-turns. I don’t know how many turns it is but now the coil draws about 650mA and can open the valve under 4 inches of water, which is enough.
The coil is wound on a screw between two 3d-printed plastic discs. This screw is threaded into a little cylindrical thingy that sits in the neck of a regular cut down drinks bottle. With the dish soap bottle cap screwed on, turning the coil assembly with a screwdriver adjusts the clearance between the armature and the ball bearing so I don’t need to worry about the tolerances on those plastic parts. Simple, right?

Now that all works, it was time to figure out the logic. I wanted to be able to adjust the dryness of soil at which we decide to water, so I’d need some kind of trim pot, but how big? I stuck some screws in plant pots a few cm apart and found as expected wet soil is low resistance and drying soil can be anywhere up to 200k, but is usually about 50k at the point when you’d start to feel sorry for the plant. I went for a 100k because I had that in the parts bin. Ideally would be twice that but meh. It’ll work.
Still the PIC leaves many choices of implementation because it has so many peripherals to choose from. Do I use an ADC with the pot providing a reference voltage? Or a comparator? Or some other weird exotic with the analog inputs? In the end I chose the comparator because I understood that part of the datsheet after only 4 read throughs. That’s simpler than anything else in there…

I set about writing some code to set up all the peripherals on the pic (most of the code) and then the actual working bit (not so much). The plan is for the PIC to wake up from sleep, apply power to the probes, wait half a second for the potentials to settle down (I’d noticed drift when using a meter), then if soil resistance > pot resistance open the valve for 2 seconds. Then power off the probe voltage and comparator (important!) and sleep again.
2 seconds is enough – that lets about a teaspoon of water out, so it won’t take many cycles to saturate even the driest of pots.
The pic is clocked internally and wakes from sleep using the watchdog timer interrupt. The longest that can run is 272 seconds – about 5 mins. There’s no need to wake up that often, so I just made it sleep twice. This means it wakes and runs the test every 9 minutes.

I also wanted some kind of “calibration mode” so that I could set the pot to a sensible value without waiting 9 minutes after every tweak to see if it was right. So on waking up (or on power on) the PIC checks an input for the presence of a jumper link that sets “calibration mode”. If this is present the sleep cycle is swapped for a 2 second delay loop, meaning the test gets run every 2-ish seconds. Combined with a LED in parallel with the coil this should let me set up the device in a sensible amount of time, with or without the valve connected.

I wrote all the code while my PIC programmer was on order. I’d always borrowed one before but figured it was time.
When it arrived I breadboarded up a test circuit and was amazed to find the code was really nearly right. All I’d done was forget to put the correct delay value back in the delay loops after simulating with an artificially shortened one. One small change and it was working! When has PIC assembly EVER worked second time?! I’m super proud of that too!

So to stripboard.

DSC_0144_labels

I threw this together with a micro USB socket for power (see what I did there? So you can use a phone charger instead of solar if you want…) and remembered to add a diode so the flyback from the valve doesn’t toast the PIC. I knew to do that because I remember feeling the shocks when playing around testing the valve with wet hands. It tingles…

The PIC drives a MOSFET to switch the high current to the valve. The probe and valve connect on pin headers for convenience.
The soil resistance probe is just a pair of stainless screws through a bit of PLA. Nothing to see there.

DCIM100GOPRO

I knocked up a little bottle stand and channel for the water out of PLA also, and set it in place. It works, as designed, amazingly. Its legs are the stands recycled from the garden lights I used in the power supply. The board hangs off the side of the stand and some ugly trailing wires lead off via strain reliefs to the solenoid and probes.

trough ring

DCIM100GOPRO

In the video below the part of the plant is being played by a glass, because it’s winter and there are no plants really left to water. You’ll have to take my word about the soil probe working properly. This is with the test jumper installed, so cycling constantly. In run mode there’s a 9 minute wait, but that doesn’t make good tv.

The solar cells have no trouble keeping the batteries topped up, and they in turn have no trouble feeding 600mA for 2 seconds every now and then. I think they’d struggle with anything more hungry than a PIC, though. It’s been fine looking after my chili plant for 2 months now 🙂