Thursday, 24 September 2020

Tinysensors Redux


Raspberry Pi Zero-W with Tinysensor Hat

It is often instructive to revisit an old project: you never know what you might see with fresh eyes. Such is the case with Tinysensors, which has featured here several times over the years. Since it lives in Github I use that platform's Issues feature to collect ideas which pop into my head when I am otherwise occupied. This provides a nice context from which to start revisiting.

Among the issues addressed since the last time we featured this project are:

  • Switching to the ATTinyCore. From my point of view, the most important feature of this great project is its support for Optiboot. This decreased the testing time for changes to the Tinysensor sketch by an order of magnitude. No more external programmers!
  • Removing the RF24Network library. This supports a very versatile model of organising nRF24L01+ radios into a fully-connected 5-way tree. However it necessarily limits the number of leaf sensors to 5, requiring bridges to interconnect different levels. A star topology removes this limitation, and also reduces the code footprint enough to support debugging via SoftwareSerial.
  • Reducing power-drain. With the above improvements in testability, it didn't take long before a gotcha in power consumption was spotted: a voltage divider made with a built-in pullup resistor consumes current even during SLEEP_MODE_PWR_DOWN!
  • Moving the hub to Raspberry Pi Zero W. This has more memory and more processor for smaller footprint; in addition to built-in WiFi and Bluetooth. Once that was complete, a proper Hat was designed to hold the nRF24L01+ radio and status LEDs... and a power button, just 'cos.
  • Outsourcing the RF24 library to nRF24. These guys have put a lot of work into improving the performance of Maniacbug's excellent original library, which means I don't have to!
Future work? A nice next step might be to redesign the sensor PCB for a coin-cell and surface-mount components. However I'm pleased enough with these changes to park it for another couple of years!

Thursday, 27 August 2020

Soldering surface-mount components


ncp1402 boost converter

The image shows a breakout board for an NCP1402-based boost converter which steps-up 1.5v to 5v; these circuits are widely-used to power 5v (or nowadays 3.3v) from a single coin cell. (The board is my design, in KiCad, based on the manufacturer's recommended application.)

Of more interest here is the regulator component itself. This is the small 5-pin chip midway up the right-hand side of the board. It is a surface-mount SOT23-5 part, approximately 3mm by 1.5mm, far smaller than a grain of rice. Building this breakout board was more an exercise in seeing if it was possible to hand-solder these parts than anything else, since they are widely available for cheap on t'Internet.

So how did I do it? The first step was choosing a hand-soldered footprint for the part in KiCad. Then, once the boards had arrived, this part obviously had to be soldered on first:

  • Apply solder to the pad under one of the further-apart pins (on the left in the picture)
  • Place the chip over this pad with a tweezers and hold it in place
  • Melt the solder on the pad until it flows over the pin
  • Solder the other pin on the left-hand side
  • Solder the three pins on the right-hand side together
  • With a desoldering pump (braid should work too), remove the excess solder from these three pins, so they are no longer shorted together.
Tweezers and magnifying glass (or low-power microscope) are essential, unless you are some sort of assembly robot. Fine-gauge solder and a fine-tipped soldering are helpful too.

Note that the circuit itself helped me "get away" with the final two steps of the procedure above: of the three pins on that side of the regulator, one is NC and the other two are connected to Vout. However checking with a continuity tester indicated that they were no longer shorted together, so the pump had worked as expected.

So the experiment being successful, the next step is to build more surface-mount parts into my designs. 10k resistors and 100n capacitors crop up very often so they would be obvious targets for replacement. Stay tuned!

Wednesday, 22 July 2020

Simple Variable Voltage Bench PSU

Back in the days of the desktop PC, a common DIY project was to re-use its ATX power supply as the basis of a Bench PSU. Sadly those days are slipping into the past as more and more of us move to laptops. All is not lost however, as most laptop transformers also deliver a reasonable output voltage which can easily be down-regulated and supplied to nascent bench projects.

The circuit schematic could hardly be simpler --- I am fairly embarrassed that the best I could do to obtain it for insertion here was "Print Screen" in KiCad. All of the action is provided by an LM317 linear regulator which costs peanuts these days. The output voltage is given by:

Vout = Vref * (1 + RV1 / R1), Vref = 1.25v

Note that this is independent of the input voltage, although obviously it can't be greater. Also we want to choose R1 so that the output is at sensible values for differing positions of the (linear) potentiometer RV1. Given a transformer rated at 19v, we choose R1 = 680R, giving values of Vout:

RV1    Vout 
0      1.25
2k5    5.8
5k     10.4
7k5    15
10k    19.6

Obviously the last value is a little too high, however this value of R1 gives the greatest precision for the transformer at hand.

The design is completed with a cheap LED voltmeter as shown below.


Simple extensions to this design would be to use a combined Voltmeter/Ammeter display (also costing peanuts); adding a current-limiting circuit, or a switched constant-current source, also supported by the LM317. Next time!

Friday, 19 July 2019

KiCad Kata

Pretty (but dumb) LED Matrix
The picture shows the result of another workout with KiCad as executed by Seeed Studio and finished by me. This looks rather prettier than the original and was considerably easier to solder, not to mention more LEDs!

However a few defects are obvious too:

  • the LED spacing is wider than on stripboard, which can make patterns harder to discern
  • the interface split between anodes and cathodes is the same as the original, for no good reason
  • it now uses 16 gpios!
The first and second of these are my fault but the last could have been mitigated by adding an LED driver, such as the MAX7221 to the design:
MAX7221 DIP package
This particular chip requires only a couple of capacitors and a single resistor to function (see the circuit here). Also it is cheap, less than €1 from China and has a mature Arduino library. It could comfortably have fit in the space occupied by the resistors on the left of the picture. On the downside, it requires a 5v supply. (The Arduino playground has a lot of information about it.)

So, it was instructive to lay out an LED matrix and write a library for it, and I'd encourage you to try it for yourself. (However I also won't blame you for simply buying one on eBay, they seem to be cheaper than the cost of parts somehow!)

Wednesday, 17 July 2019

At the Margin

(Who knew that the Economics of Seinfeld was a thing? Given that it was the first link returned by Google, everyone but me I guess!)

The idea of "marginal cost" is usually in mind when I'm preparing a PCB order from Seeed Studio. That's because their product is cheap and high quality but shipping rates are high. This means that it's almost free to load up an order with small items which would not of themselves justify the total cost.

This post describes a couple of such items, which gave me practice (I'm learning KiCad) yet took no longer than a few hours to produce.

The first of these is a breakout board for the (pre-ESP8266 anyway) ubiquitous nRF24L01 radio module. Its main advantage is its low power, allowing projects using it to run off batteries. Its main disadvantage is its breadboard-hostile pinout. Enough said: see picture below.
nRF24L01 breakout
Sparkfun does something similar; however theirs is better, and more expensive! I kept mine pin-compatible though, because why not?

Next up is a breadboard-friendly development board for the attiny84. This had been on my maybe-list for a while but it was only when I discovered Spence Konde's awesome Arduino core for ATtiny that I decided to go for it. The presence of Optiboot sealed the deal for me: the pin header allows sketch upload via a standard FTDI dongle.

attiny84 devboard
Lastly, just to show that they both play nicely together, the pair wired up, and programmed with a sketch using my ATtiny port of the RF24 library (originally written by Maniacbug of course).
All together now!

Wednesday, 22 May 2019

Soldering Kata

7x7 LED Matrix
When I were a lad, LEDs were a big deal, literally: £1 each. Now someone in China manages to make a living selling them for £1 for 100. With inspiration provided by an Instructable, a rainy afternoon and the need for some soldering practice I set to work.

The Instructable's, err, instructions were clear enough:

  • take a piece of protoboard (the sort with pads instead of strips)
  • solder each row of LEDs anode (longer) first to hold them in place
  • cut the cathodes to size and bend them through 90 to connect each row's together
  • test each row after soldering!
  • I then cut a piece of stripboard ("veroboard") into single strips and connected each column's anodes together in a sort of DIY dual-layer PCB arrangement, see below.

Rear view: anodes connected in columns

The last step was the only point where I deviated from the instructions. (I first attempted to fit a single piece of veroboard over all of the anodes but found this impossible: there was too much variation in their positions. Perhaps aligning each row of LEDs as it is soldered using veroboard might work?)

Next: mount vertically on breadboard, add a handful of resistors (470R) and connect to an Arduino!

I wrote an Arduino library for it, available on Github. It contains a couple of examples, the simplest can be seen running below. (The moving light makes keeping the camera focussed impossible, sorry.)

The disadvantages of this bit-banging approach to driving an LED matrix are fairly well-known:
  • it uses an excessive number of GPIOs (14 here)
  • the GPIOs themselves can't supply very much current, so the more LEDs lit, the dimmer they are
  • PWM-style dimming is not practical
  • timing is tricky: since it relies on persistence of vision to work, each refresh cycle must take no more than 40ms
For all of these reasons, using a dedicated driver chip is recommended. More when my MAX7221 (another £1) arrives on the ship from China in a couple of weeks!

Friday, 1 June 2018

It speaks!

Porting TTS to the ESP8266 processor has been on my to-do list for a while now, prompted by a comment on an issue I'd created to remind me. To do this, I realised that I'd have to reacquaint myself with PWM on the ATmega328p.

At its most basic, a PWM waveform is a square-wave with a varying duty-cycle. Both the frequency of the square-wave and the duty-cycle are determined on an AVR by the its built-in timers. PWM on Arduino pin 9 (hardware pin 15, OC1A), for example, is driven by Timer-1, a 16-bit timer.

Several PWM modes are available; TTS chooses the "phase and frequency corrected" option.

The diagram shows how this works to change the duty-cycle of the output pin:
  • the timer counts from BOTTOM to TOP and back down again
  • when it crosses the OCR1 threshold going up, it sets the OC1A output high,
  • when it crosses this threshold on the way down again, it sets the OC1A output low. 
Therefore by dynamically changing OCR1, we can change the "volume" on the output pin.

The final piece of the puzzle is how the frequency of the square wave is determined. Since the timer ticks on every clock cycle, it will take 2*TOP cycles to cycle from BOTTOM to TOP and back. The AVR has a clock frequency of 16MHz and PWM_TOP in the TTS library is defined as 1200/2. This gives a frequency of about 13kHz.

By default PWM on Arduino/ESP8266 has a range of 1023 and frequency of 1kHz. However it provides APIs to change these values. By setting them appropriately, the ESP8266 speaks!

Wemos Mini D1 with TTS amplifier and super-cheap speaker
The picture shows a Wemos Mini D1 connected to a modified version of the simple LM386 amplifier described at the TTS GitHub. (The modifications arose from a Hackaday article on the LM386 and are summarised in the GitHub's README.)

This amplifier sounds surprisingly good, considering its total cost was about €1. Hooking it up to a signal generator and oscilloscope produced the following pretty pictures.

At low volume, the output is a pretty faithful inversion of the input signal (a 1kHz sine wave):

Undistorted Signal
At high volume, the output is clipped by the supply rails (but still sounds pretty good to my untrained ear):
Clipped Signal
These pictures were produced using an Xprotolab Plain, an amazing combination of Oscilloscope, Signal Generator and Protocol Analyzer implemented on an AVR XMEGA. Buy one!
Xprotolab Plain