Wednesday, 15 July 2020

Night photo update on Raspberry Pi HQ camera with 16mm lens

To continue from yesterday's post, yesterday evening I took some photos of near and far street lamps (heavy cloud so no stars...). The results show the same effects, but possibly more pronounced.

Overall the bloom is greatly improved by stopping down to 2.8. In some circumstances there may be a further slight improvement by going to F4, but the difference is pretty small.

All these photos are taken with 2 second exposure.

Tuesday, 14 July 2020

Raspberry pi HQ Camera and 16mm lens quality

The 16mm lens sold alongside the Raspberry Pi HQ lens is a really nice lens. After some misgivings about the image quality I got with some early testing, some more careful tests show that the lens has a rather specific problem that can easily be avoided, although it does slightly limit what it can do.

I have compared the photos from the HQ camera and 16mm lens with the images from a DSLR (crop sensor) using a 55mm lens which gives very nearly the same field of view. I also did a couple of tests using the canon lens with the Raspberry Pi camera, and there is little improvement (if any) over the results with the standard 16mm lens. Give there is 15X price difference, this is pretty remarkable.

 What is the problem?

The only problem I have found is that wide open (F1.4) the lens has significant bloom, which can really show up on some types of photo (Distant street lights at night have a big bloom around them; stars do as well, although it is not usually as noticeable.)

The good news is that this problem disappears rapidly as the lens is stopped down and from F2.8, there is no real evidence of the problem. Sample images at the end of this page.

Thursday, 5 September 2019

Very simple remote stepper motor controller

This project creates a simple unipolar stepper motor controller, running on a raspberry pi (zero or other), written in python and using bluedot to remotely control the motor.

Because this is driving the stepper at a low level, the maximum speed is rather limited, to drive a stepper fast you need to use an autonomous stepper controller (such as a trinamic stepper driver), or use a driver such as an A4988 with DMA driven i/o to maintain precise timing.

The use of bluetooth means that only Raspberry pi's with bluetooth on board can be used - so It should work on Pi3, Pi4 and Pi Zero-W.

The hardware driver is a simple ULN2003 driver chip.

Short video here.

Find our more about the excellent bluedot here.

Setup / installation

Any version of raspbian can be used for this, for pi zero particularly, I recommend using Raspbian Lite. This setup was tested on raspbian Buster, on a Pi Zero W.

This setup tested on a clean build of Raspbian Lite (Buster).

  1. After initial preparation of the sd card, and the usual updates.......
  2. sudo apt install python3-pigpio git python3-pip
  3.  and then
  4. sudo pip3 install bluedot
  5. Now get the bluedot app on an android phone or setup a second raspberry pi to use as the controller. Follow the appropriate instructions from the bluedot documents.
  6. Pair the 2 devices - Note: if you use command line on the raspberry pi, you need
    sudo bluetoothctl
  7.  Finally get the app from github. I use a folder 'gitbits' and put the git software there, if you change this the script will need to be amended.
    cd ~/
    mkdir gitbits
    cd gitbits
    git clone


Start the pigpio daemon if it is not already running:
sudo gpiod
  - or -
sudo gpiod -c 256  # -c 256 makes the daemon run at high priority
On the Raspberry pi with the motor, in a command prompt (either via ssh or through a terminal window):
cd ~/gitbits/bluepystepper
 - or- if on raspbian lite:
sudo python3
Now on the controller (android device or another Pi). Run the bluedot app and connect to the motorised Pi. Once the big blue dot appears, you can control the stepper by adjusting the speed.

The motor is set to stopped when starts, and each tap on the blue dot changes the speed:
  • tapping on the centre of the dot sets speed to 0.
  • tapping on the right of the dot sets speed to 1 if the motor is stopped, doubles the speed if the motor is going forward, and halves the speed if the motor is going backwards.
  • tapping on the left of the dot sets the speed to -1 if the motor is stopped, doubles the speed if the motor is going backwards, and halves the speed if the motor is going forwards.

Setting up to auto-start on boot

There are 2 steps required:
  1. arrange for pigpiod to start automatically on boot
  2. arrange for the python app to start automatically on boot

Starting the pigpio daemon on boot

I use crontab to arrange to start the pigpio daemon on boot.
sudo crontab -e
Note the first time you run crontab it prompts for an editor - use the number for your preferred editor, I use nano (option 2):
no crontab for pi - using an empty one

Select an editor. To change later, run 'select-editor'.
1. /bin/ed
2. /bin/nano <---- easiest
3. /usr/bin/vim.tiny
and add the line (amend the location if whereis shows it is somewhere else):
@reboot /usr/bin/pigpiod -c 256
The -c 256 parameter runs the daemon in realtime mode - that is it is given a high CPU priority.

Starting the app on boot

Again I use crontab, but this time as a user:
(Note: if running raspbian lite use 'sudo crontab -e' instead)
crontab -e
 and add this line:
@reboot sleep 7; /home/pi/gitbits/bluepystepper/

Monday, 22 October 2018

Raspberry Pi Zero W and Raspberry Pi 3 headless setup

As I mentioned in an earlier blog, the RPi-Cam-Web-Interface package is a much better fit for Raspberry Pi, and particularly a Pi Zero than packages like motion / motioneye. This is primarily because RPi-Cam-Web-Interface exploits the tight integration between a Raspberry Pi and a Raspberry Pi Camera, to be faster and more efficient than is possible using more generic interfaces. This allows the reliable use of faster framerates and / or higher resolutions.

This post describes the initial setup of a Raspberry Pi, including connecting it to a home network and accessing it from a PC, laptop or tablet on the home network.

Preparing to power on for the first time

This stage has the following steps:
  1. Put raspbian onto the micro SD card
  2. Tweak the micro SD card to enable access from you wifi network
  3. Insert into the raspberry pi and power on
  4. Access the Raspberry pi from another computer.

Put Raspbian onto the the micro SD card

Normally you only need to to this once, but if you want to start again from square one for any reason, then you can do this again. It WILL overwrite the SD card so any files there before you do this will disappear.

The easiest way to get a shiny fresh installation of Raspbian onto the SD card is to use Etcher, which works on Windows, Linux and MAC - it doesn't however run on tablets or phones, so for now this step has to be done on a desktop or laptop PC.

There is an excellent and full guide to using Etcher here, and there is a fairly detailed video here. Below are a couple of extra bits of info and a short guide if you don't need all the background info.

Use Raspbian Lite NOT full Raspbian.

Don't immediately put the SD card into your pi after running Etcher - there are a couple of extra things first (see step 99 below).

To minimise the overheads and get the best out of your pi I recommend using Raspbian Lite - this is especially useful on a Raspberry Pi Zero which has more limited processor power and memory than its bigger brothers.

Text version - flashing the SD card:

  1. Download Etcher from here. The Download for xxx button normally has the right version selected, so you don't need to be puzzled by the various option that appear if you open the drop down box.
  2. Install Etcher on your PC
  3. Download Raspbian Lite - Click on Download  ZIP under Raspbian Stretch Lite on the right of this page On the pop-up window, ensure Save File is checked and click OK. If you start at the main Raspberry Pi download page, click on Raspbian (NOT NOOBS) to get to the page with Raspbian Lite on it.
  4. Put the micro SD card in a suitable adapter - either a USB adapter or a full size SD card adapter if your PC / laptop has a full size SD card reader.
  5. Plug in the adapter. Ignore any messages from the operating system, and close any windows that pop-up ( DONT CLOSE ETCHER!)
  6. Run Etcher - on Windows press the Windows key and it should be near the top under "recently added". You can also press the Windows key and type etcher, and it should appear beside something that looks like a very small (vinyl) record.
  7. In Etcher Click on Select Image and then click on downloads at the top of the left pane. Select the ZIP file downloaded from
  8. The middle section of Etcher should already be showing your micro SD card and will skip straight to highlight Flash. Click on Flash! and wait for the card to be flashed and verified.
  9. Remove the SD card adapter from the PC, DONT PUT IT IN THE PI YET!

Text version - final tweaks to the card:

After watching countless numbers of really awful videos, this one is reasonable. Ignore the first part about imaging the disc - start at around 1:40. This video does things in reverse order, and carries on to some of the configuration steps I have in the next section. Use the extended info below for the wpa-supplicant file however.
  1. Put the adapter back in the PC and, using File Manager, find and open the boot disk - look in This PC to find it.
  2. Right click in the file area and from the drop down select New then Text Document. Rename the new text document to ssh and remove the '.txt' (you will probably need to enable File name extensions in the View Tab at the top of the screen)
  3. Now create another new document and add the following contents:
  4. change MyNetworkSSID to the name of your wifi network, and likewise the password. Leave the quotes in place.
  5. rename this file to wpa_supplicant.conf
  6. Safely remove the adapter from your PC and remove the micro SD card.

First switch on and connecting from PC

Starting the Pi

This is easy. plug the micro SD card into the Pi and then connect power to the micro USB power adapter (the one at the very end of the card). The green light should flicker a bit after a few seconds. It will take 20 seconds to 1 minute to start this first time, subsequent boots will be faster.

Connecting from linux

This one is really easy:
  1. Open a new terminal window and enter this command:
  2. ssh -l pi raspberrypi
  3. Enter yes at the fingerprint prompt, hit Return
  4. Enter the preset password raspberry at the password prompt, hit Return. The session should start
If the device cannot be found (ssh: Could not resolve hostname raspberrypi) see the problem solving section at the end of this blog

Connecting from Windows

Installing PuTTY

Once only, you need to install PuTTY. There is a good guide here, it is best to follow that guide.


Once installed, run PuTTY as described here. The previous video also has a good section on this starting at around 5:15. When you first power up your pi it should connect if you enter
as the host name. If that does not work, try
Login with username pi and password raspberry. Now it's time to do a little bit more setup.

If even raspberrypi.local fails see the problems section at the end of this post.

Connecting from Mac

For now the best advice I can give is to follow this.

Final setup of Raspberry Pi

Once logged in there are a few further steps before setting up the camera software:
  1. Change the hostname
  2. Change the password
  3. set the local language (which also makes the keyboard behave)
  4. enable the camera
These are all done using raspi-config.
sudo raspi-config
This also is in the video at 6:55. It doesn't cover enabling the camera, but it is in raspi-config in the Interfacing Options sections.

Reboot the Raspberry Pi. Now we are ready to setup the camera software which I'll cover in a separate post.


ssh / putty  fails to connect

Sometimes raspberrypi fails to connect.

try raspberrypi.local instead.

If that fails you will need to use the pi's IP address. You should be able to find this in your router's web pages, but exactly how varies from router to router.

Tuesday, 16 October 2018

Making a cheap and flexible trailcam with Raspberry Pi

I know there are quite a few versions of this around, but after seeing the short item on Springwatch about a DIY Raspberry Pi based I thought it would be good project for any at the local U3A who still fancied a play with technology, but wanted some help / backup.

So I started at the recommended web site, ordered a kit, nicked a powerbank from another project and set off.

The I made case is distinctly Blue Peter, but functional and I used a glue gun for the first time in my life (only somewhat messilly!) I used a recycled carry out box, some cardboard and the top of an old bottle as a lens shield.

After a couple of hiccups due to not setting up the config files properly on the memory card (so the wifi didn't hook up), I just used the supplied config and it was up and running.

The powerbank (Anker Powercore 10000) works a treat and will run the setup for much more than 24 hours.


The software is pretty limited and limited to pi zero W and pi B 3 and the only network option is to setup as a wifi hotspot - so to use the camera you need to change the wifi  setup on whatever device you use (phone, tablet, laptop etc) to the camera (and of course loose all acess to the rest of the world in the meantime). Clearly the bunch of folks who wrote this software have such large gardens their home wifi doesn't reach the edges.

For me, I'd rather be able to use any sensible network option - including a private hotspot if appropriate, but also using any existing wifi, or even wired LAN (allowing the use of power over ethernet so there is not battery problem).

A bit of digging suggested that this motion software would work well even on a Pi Zero. It works well, and does seem to do everything necessary pretty well. It also enables multiple cameras (even on multiple Raspberry Pis) to be managed through a single interface. This package however, loads a Pi Zero quite heavily and this means that video frame size has to be scaled back to avoid overloading the a Pi Zero.

I'm also looking at RPi-Cam-Web-Interface, which is written to exploit the Raspberry Pi Camera with its built in GPU support. Although not quite as sophisticated as motion, this may be a better option overall for a Pi Zero in particular. I'll post more about this later.

The gory details of the entire Raspberry Pi build process for motioneye on raspbian lite are below (elapsed time on pi Zero in brackets):

Friday, 3 August 2018

log book notes: trinamic 5130-bob on Raspberry pi

These are notes on using a trinamic 5130-bob on Raspberry Pi.
Here is a short video of it running....

I intend to have a Python interface to the device eventually.

I'm running this on a pi 3b+ with raspbian lite installed.

I'm basically following this page, but switching 5160 to 5130.

The broadcom driver version I have is 1.56. (check here)
tar zxvf bcm2835-XXX.tar
cd bcm2835-XXX
sudo make check
sudo make install

The trinamic software also needs wiringPi installed....

And you need to follow their (trinamic) directory instructions to make to work.

Finally small differences in the detailed spec between 5130 and 5160 mean that the GCONF setting should be 04, not 0C - see main.c line 39 on the trinamic website page linked above.

I could now drive a motor successfully. I have added a few more tweaks to the code which are detailed below. (The code is here as well)

Saturday, 31 March 2018

DC motor control with an RPi - writing the software

This explains some of the lessons learned and techniques I have used to develop Raspberry Pi software to provide accurate and responsive control for DC motors using feedback from rotary encoders.

It follows on from my previous post, showng the results of some initial testing on the feasability of good motor control using just a Raspberry Pi (i.e. without an arduino or other micro controller).

I very much liked the approach described by David Anderson explaining his approach to basic robot control, so the overall approach I used was to write software that is called from a regular timer function to run the control loop.

Such software can then be run directly from some higher level control loop, or wrapped up to provide a freestanding process controlled through a pipe or web service.

My usual approach to time / function critical software is to start with Python and get to a point where:
  • it all works well - great - job done.
  • it is hopeless - its never going to work this way - try something completely different or give up.
  • its working, but not quite well enough - perhaps a couple of small key parts can be re-implemented -possibly in C.
In this case - so far at least - the pure python version looks to be doing the job well.

I have a couple of repositories on github with the code for this, but these are still very much a work in progress.

motor drivers are here.

simple robot control is here. - So far this only does remote control from a web browser.