ATtiny85 And Shift Registers

I decided I wanted to play with shift registers, and the classic circuit for that is running a bunch more LED’s from a chip than the chip has pins, so I decided on an 8 pin ATtiny85 running 16 LED’s as a Larson scanner (Knight Rider or Cylon effect).

I started out on breadboard which was a terrible mistake as I ended up needing two breadboards to provide enough space to run 16 LED’s via resistors all with a shared GND. Should have gone straight to perfboard (I think veroboard would have the same problem as breadboard due to solid tracks instead of pads).

The shiftOut() function takes a bit of getting used to, especially when using two 74HC595 shift registers so need to output 16 bits instead of an 8-bit byte (and when it takes an hour of playing with code to realise one of the chip’s output pins it busted!) and for some reason when moving from breadboard to perfboard I had to change the code to use MSBFIRST instead of LSBFIRST or it would run the LED sequence on each chip instead of across the two…..?!

Anyway, here’s the circuit diagram:

And here’s the code, where you can see I visualised the effect in binary split over the two registers, which then gets latched, shifted out twice, then unlatched. All using 3 pins from the ATtiny85!

const int latchPin = 1; // st_cp=12
const int clockPin = 2; // sh_cp=11
const int dataPin = 0; // ds=14

void write2Registers(byte patternA, byte patternB)
{
    // set latch low so output doesnt change whilst sending in data
    digitalWrite(latchPin, LOW);

    // shift out the bits
    shiftOut(dataPin, clockPin, MSBFIRST, patternA);
    shiftOut(dataPin, clockPin, MSBFIRST, patternB);

    // set latch high to send output
    digitalWrite(latchPin, HIGH);

    // pause
    delay(100);
}

void setup()
{
    // configure pins
    pinMode(latchPin, OUTPUT);
    pinMode(dataPin, OUTPUT);
    pinMode(clockPin, OUTPUT);
}

void loop()
{
    write2Registers(B00000000,B00000001);
    write2Registers(B00000000,B00000011);
    write2Registers(B00000000,B00000111);
    write2Registers(B00000000,B00001110);
    write2Registers(B00000000,B00011100);
    write2Registers(B00000000,B00111000);
    write2Registers(B00000000,B01110000);
    write2Registers(B00000000,B11100000);
    write2Registers(B00000001,B11000000);
    write2Registers(B00000011,B10000000);
    write2Registers(B00000111,B00000000);
    write2Registers(B00001110,B00000000);
    write2Registers(B00011100,B00000000);
    write2Registers(B00111000,B00000000);
    write2Registers(B01110000,B00000000);
    write2Registers(B11100000,B00000000);
    write2Registers(B11000000,B00000000);
    write2Registers(B10000000,B00000000);
    write2Registers(B11000000,B00000000);
    write2Registers(B11100000,B00000000);
    write2Registers(B01110000,B00000000);
    write2Registers(B00111000,B00000000);
    write2Registers(B00011100,B00000000);
    write2Registers(B00001110,B00000000);
    write2Registers(B00000111,B00000000);
    write2Registers(B00000011,B10000000);
    write2Registers(B00000001,B11000000);
    write2Registers(B00000000,B11100000);
    write2Registers(B00000000,B01110000);
    write2Registers(B00000000,B00111000);
    write2Registers(B00000000,B00011100);
    write2Registers(B00000000,B00001110);
    write2Registers(B00000000,B00000111);
    write2Registers(B00000000,B00000011);
}

And a Makefile for use with the arduino-tiny core:

ISP_PROG     	   = usbasp
BOARD_TAG          = attiny85at8
ALTERNATE_CORE     = tiny
AVRDUDE_OPTS       = -v

include /usr/share/arduino/Arduino.mk

Also to help debugging I wrote a simplified code that would light a single LED:

void turnOnLed(int led)
{
    digitalWrite(latchPin, LOW);

    if (led >= 8)
    {
        shiftOut(dataPin, clockPin, MSBFIRST, 1<<(led-8));
        shiftOut(dataPin, clockPin, MSBFIRST, 0);
    }
    else
    {
        shiftOut(dataPin, clockPin, MSBFIRST, 0);
        shiftOut(dataPin, clockPin, MSBFIRST, 1<<led);

    }

    digitalWrite(latchPin, HIGH);
}

Which you can then call from setup() for instance, so it only gets run once:

for(int i=0; i<16; i++)
{
    turnOnLed(i);
    delay(120);
}

I also found that sometimes when first powered on, the circuit it would flash on some random LED’s, this is (mostly) solved by putting 10k pull down resistors between the latch pins and GND so that the registers don’t get rubbish data sent to them when the microcontroller is booting. I tried controlling the OE or SRCLR pins from the ATtiny85 but it didn’t fix the problem which is weird as SRCLR is supposed to reset the registers to all zeroes and OE is supposed to enable output!

I also varied the brightness by using PWM on the OE pin by calling analogWrite(4, random(255)); but the effect was a bit naff. You could vary the speed using a trimpot and analogRead(3) I guess, but I’m all out of potentiometers.

The pinouts are:

74HC595:

Q1  = 1		16 = VCC
Q1  = 2		15 = Q0
Q3  = 3		14 = DS (data)
Q4  = 4		13 = OE (output enable - tie to gnd)
Q5  = 5		12 = ST_CP (latch)
Q6  = 6		11 = SH_CP (clock)
Q7  = 7		10 = SRCLR (clear - tie to vcc)
GND = 8		9  = SO

ATtiny85:

RST   = 1		8 = VCC
A3/D3 = 2		7 = D2/A1
A2/D4 = 3		6 = D1
GND   = 4		5 = D0

RPi Soldering Scope

I’m thinking of making a soldering microscope from a Raspberry Pi (either a spare B+ I have or maybe a ZeroW). I had looked at Andonstar ADMSM201 or Lapsun 14MP HDMI 180x scopes, but they each have their limitations – crap software, tiny focal length requiring additional lenses, £200+ pricetags…..

So looking at it, I just have to buy a Pi camera module with a CS mount and a decent lens. Then I can either stream the display or plug into a HDMI monitor. Even without a Barlow lens I should get at least 20cm working area beneath the camera.

The only problem is how to mount it. I’m thinking of a threaded rod from a 3D printer attached to my wooden 3rd arm board and some sort of 3D printed bracket. I only really need up/down not left/right or forwards/backwards (I can just move whatever I’m soldering). I even thought of a laboratory retort stand.

I’m going to try without illumination initially, and potentially make something from a Neopixel ring that could be controlled by the Pi and potentially powered from the same USB supply.

BOM is currently:

I’ve made a pi camera to CS mount adaptor and I’ve also modified my Pi ZeroW case to include a screw-on CS mount adaptor and holes for a shutdown/reset button and wires for a Neopixel ring which I’ll probably use instead of a regular LED ring, or I may use a 12v PSU and some spare LED strip.

ArduinoOTA Solution for ESP8266

I’ve wasted about three days trying to get OTA upgrades to work on my ESP8266 boards. They take 1-2 OTA’s and then don’t even boot into the sketch. Tried my Gizwits WiFi Witty and ESP12F on a breakout board with an LDO as described in my earlier post. Then I tried my old NodeMCUv2 board and it worked fine, all of the time.

Turns out we need moar powah!

The HT7833 LDO on the white breakout boards from the earlier post, are supposed to be able to put out 500mA so I guess its not a current problem but a voltage one, when measuring what gets through to the ESP its just over 3.3v. I bypassed the LDO and fed a 3.7v LiPo to VCC and it works fine now!

Whilst playing around trying to get it to work I added the 4x 10k resistors and a 100nF capacitor across the ESP’s VCC/GND pins as well as a 470uF capacitor across the power rails feeding the LDO, as per here. Didn’t seem to make any difference.

On a related note, the pinout of those white boards is insane. Turns out the VCC pin on the white board is where you should feed the supply into the LDO. It’s connected to VIN (middle pin). But bizarrely VCC on the ESP is not fed from there, its fed from a via from the LDO’s VOUT (right pin) and it seems CH_PD is fed from that via the leftmost 10k resistor on the breakout. So don’t power other components or connect other ESP pins to VCC as it’ll be running 8v or whatever you’re inputting!

I removed the LDO and had to replace the middle 000 resistor (solder blob) so that the breakout VCC goes to the ESP VCC.

So essentially if you want to use the LDO (I’d advise against it) you have to remove the 000 resistor and feed the LDO up to 8v via the breakout’s VCC pin, then run a jumper wire from VOUT on the LDO to the ESP’s VCC pin. I used the leftmost via as seen from the top, near VCC/GPIO13 pins rather than running a wire over and under the breakout.

If you don’t want to use the LDO, leave the breakout as it was and feed 3.7v into the VCC pin.

I’ve received my free Amazon Echo – wow I need to get another when they’re £80 again! They are amazing! I’ve been using the fauxmoESP library to control the LED on an ESP8266, sooooo easy.

Here’s my sketch, including OTA code. Note to generate an MD5SUM to use as your OTA password, you need to call echo -n "otapassphrase" | md5sum to ensure you don’t get a newline! Then pass espota.py the plaintext string.

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include "fauxmoESP.h"

// init vars
const int RED = 15;
const int GREEN = 12;
const int BLUE = 13;

// constructor
fauxmoESP fauxmo;

void callback(uint8_t device_id, const char * device_name, bool state)
{
    Serial.print("Device ");
    Serial.print(device_name);
    Serial.print(" state: ");
    if (state)
    {
        Serial.println("ON");
        analogWrite(RED, random(0,1023));
        analogWrite(GREEN, random(0,1023));
        analogWrite(BLUE, random(0,1023));
    }
    else
    {
        Serial.println("OFF");
        analogWrite(RED, 0);
        analogWrite(GREEN, 0);
        analogWrite(BLUE, 0);
    }
}

void setup()
{
    // configure led
    pinMode(RED, OUTPUT);
    pinMode(GREEN, OUTPUT);
    pinMode(BLUE, OUTPUT);
    analogWrite(RED, 512);
    analogWrite(GREEN, 0);
    analogWrite(BLUE, 0);

    // debug
    Serial.begin(115200);
    Serial.setDebugOutput(false);
    Serial.println("After connection, ask Alexa to 'turn pixel on' or 'off'");

    // wifi
    WiFi.mode(WIFI_STA);
    WiFi.begin("myssid", "mypassword");
    WiFi.config(IPAddress(192, 168, 1, 2), IPAddress(192, 168, 1, 1), 
         IPAddress(255, 255, 255, 0), IPAddress(8,8,8,8));

    while (WiFi.status() != WL_CONNECTED)
    {
        delay(500);
        Serial.print(".");
    }

    // ota
    ArduinoOTA.setPasswordHash("b7e82cbfc5bf5f1a44d8e5e526e2f1fe");
    ArduinoOTA.begin();

    // fauxmo
    fauxmo.addDevice("pixel");
    fauxmo.onMessage(callback);
}

void loop()
{
    fauxmo.handle();
    ArduinoOTA.handle();
}

I’ve improved my ESP Makefile to handle OTA and debugging etc (mind the word-wrapping on the fqbn line)

ARDUINO_PATH = $(HOME)/arduino-1.8.3
SKETCHBOOK   = $(HOME)/arduino16
ESPTOOL		 = $(SKETCHBOOK)/hardware/esp8266com/esp8266/tools/esptool/esptool
ESPOTA		 = $(SKETCHBOOK)/hardware/esp8266com/esp8266/tools/espota.py
SKETCH 		 = $(notdir $(CURDIR)).ino
TARGET_DIR   = $(CURDIR)/build-esp8266
MONITOR_PORT = /dev/ttyUSB0
OTA_IP		 = 192.168.1.2
OTA_PASSWD	 = otapassphrase
#DEBUG		 = ,Debug=Serial,DebugLevel=all_____
DEBUG		 = 

all:
	@ mkdir -p $(TARGET_DIR)

	$(ARDUINO_PATH)/arduino-builder -compile -logger=machine \
	-hardware "$(ARDUINO_PATH)/hardware" \
	-hardware "$(SKETCHBOOK)/hardware" \
	-tools "$(ARDUINO_PATH)/tools-builder" \
	-tools "$(ARDUINO_PATH)/hardware/tools/avr" \
	-built-in-libraries "$(ARDUINO_PATH)/libraries" \
	-libraries "$(SKETCHBOOK)/libraries" \
	-fqbn=esp8266com:esp8266:generic:CpuFrequency=80,CrystalFreq=26,FlashFreq=40,
        FlashMode=dio,UploadSpeed=115200,FlashSize=4M3M,ResetMethod=nodemcu$(DEBUG) \
	-ide-version=10803 \
	-build-path "$(TARGET_DIR)" \
	-warnings=none \
	-prefs=build.warn_data_percentage=75 \
	-verbose "$(SKETCH)"

flash:
	$(ESPTOOL) -v -cd nodemcu -cb 115200 -cp $(MONITOR_PORT) -ca 0x00000 -cf $(TARGET_DIR)/$(SKETCH).bin

ota:
	$(ESPOTA) -i $(OTA_IP) -I 192.168.1.3 -a $(OTA_PASSWD) -f $(TARGET_DIR)/$(SKETCH).bin

clean:
	rm -rf $(TARGET_DIR)

monitor:
	screen $(MONITOR_PORT) 115200

Once you’ve discovered your device using the Alexa app (or go to echo.amazon.com then “Smart Home” > Devices > Discover) you just say “Alexa, turn pixel on”.

Soldering board with helping hands

Inspired by this Reddit post, I decided I’d have a go at making a soldering board with helping hands.

The bill of materials is over £10, but I’ve bought more than I need, so probably under a fiver for the build:

Note that my local PoundLand was selling stuff for 90p for a few months, but its back to £1 now 🙁

I used some M3x25 hex bolts and nuts to attach the crocodile clips to the hoses, but you could use whatever you have, some people even use zip ties.

My hoses seem to have a 12.1mm connector, some people have 1/2″ or 12.5mm diameter, contrary to the 1/4″ claims on ebay.

I also added some heatshrink over the teeth on the crocodile clips, so they don’t short/scratch my circuits.

The hardest part was cutting the wood – it was pretty rough and not straight or the right size, so first I tried cutting it with a hacksaw which worked well but took ages. The I tried a jigsaw which was faster but awkward, I ended up using a full saw and my Dremel to bevel the corners with some 150 grit sandpaper to smooth the surfaces.

I tested the heatproof silicone mat by warming the iron to 350c and melting some solder, then flicking it onto the mat. It worked a treat and didn’t even stick, even though its only rated for 150c I think.

To stop it moving and scratching my desk, I added some anti-skid felt feet, although you could probably add another mat and then you could turn it over when its dirty.

I drilled the holes after putting the heatproof mat on which was a mistake as it tore up the mat. I can see the drilled holes through the material so when the replacement mat arrives I will cut corresponding holes in the mat. The 12mm drillbit was just the right size, I’ll put some epoxy in and the arms will never move.

Here is the finished article holding a circuit:

And here it is holding the weight of my cellphone, which is acting as an inspection camera and lamp:

Update: I’ve replaced the nut and bolt holding the crocodile clips on with a banana plug. I made the hole in the orange tip slightly larger and pushed the banana plug through then filled the inside of the tip with hot glue (well not totally, the blue hose has to fit!) so now I can slip on whatever connect I fancy as long as it has a banana plug connector!

Budget Portable Power Supply

I just made a portable power supply out of a few components. I wanted something more flexible than my bench supply.

So the parts list is: TP4056, which can charge a LiIon or LiPo battery at the same time as powering a circuit from it, or USB. In powerbank terms it would be called pass-through. It also provides over [dis]charge protection but I don’t think reverse polarity protection (could be solved with a diode). I made another post detailing how to adjust the charge current, but I’ve left it at the default 1A for this project. I buy these in packs of five usually, they come to about 26p each.

Next up is the MT3608 DC-DC boost convertor, not sure why they’re called that when the chip is actually a B6286K. That can boost 2v input to 28v (24v?) 2A output, adjustable with the onboard potentiometer. Cost me 29p. I used some small jumper cables to join the OUT +/- of the TP4056 to the IN +/- of the boost.

Taking the output from that we have the voltmeter which samples the input and displays it on a nice 7-segment display. Cost me 76p. You have to wire the red and white wires both to the boost converter’s output as the display only works down to 4.5v, but in this configuration you can can power the boost converter from 2v and have the voltmeter still display as long as the boost’s output is 4.5v or more. Largely irrelevant in this project as the TP4056 output is always going to be above about 3.3v or so from the batteries. A smaller alternative without the case is this for 75p.

I soldered some pin headers onto the boost converter’s output pads alongiside the voltmeter wires. I also soldered some JST cables to the BAT +/- pads of the TP4056 as eventually I’ll use a LiPo battery instead of the Li-Ion (which I just temporarily connected using magnets). To charge the battery just connect a powerbank or whatever to the micro USB connector on the TP4056 board (not the one on the boost board). JST cables cost me 10p each in a pack of ten pairs (red+black female & red+black male, times ten).

So total cost of £1.40, plus the price of the battery.

I made some 3D printable containers for the individual parts but probably won’t use them for this project:

Update: I’m thinking about making a case that will hold the battery, boost, charger and voltmeter with a JST connector or Dupont cables or screw terminals as output, access to the potentiometer for adjusting the voltage and the micro USB port for charging; as everything is about the same size: