Saturday, December 2, 2017

Perl, Neopixels, Arduino and Pi

Some time ago I acquired an Adafruit 'Neopixel' stick.

If you're unfamiliar with these they are strings of WS2812 or compatible LEDs that you drive with a single PWM signal ( Neopixel stick described here ).

In recent releases of the HiPi Perl modules I've concentrated on having the modules operate without any requirement for root privileges and I'm committed to maintaining approach going forwards. That presents a problem with PWM. There is plenty of existing code for the Pi that allows you to drive  PWM signals directly. For Perl user-space code, interfacing with Joan's pigpio daemon looks the best option to me.

However for HiPi's device driver approach my only use of PWM so far has been driving servos and for that I've used external I2C PWM boards with HiPi::Interface::PCA9685. These boards can't provide the type of PWM signal the LED stick requires so something different was needed.

Four weeks ago I got my first Arduino. I wanted to drive a radio I2C breakout that was causing me grief with the Pi. It all came together rather easily. The Pi communicates with the Arduino using SPI and the Arduino controls the radio over I2C. I know I'm rather late to the party interfacing the Pi with Arduino but it's new to me and I wanted to do it using Perl.

Given the low cost of compatible boards based on the Open Source Arduino,  it seems a good option to use the devices in Pi projects and I remembered the Neopixel stick I owned.

"Arduino" Nanos and Pro Minis  duly arrived from China and I was controlling the pixel stick from a Pi using HiPi and I2C within a few hours.

I tried a 5v Nano, a 5v Pro Mini and a 3v3 Pro Mini, all with success.

Circuit for a 5v Pro Mini

According to spec, each pixel at full brightness might draw 60mA or 480mA for the whole 8 pixel stick so I needed a separate power solution.

A used a 2.5 amp 5v official Pi power supply to power a Pi Zero W, the "Arduino" Pro Mini, and the Neopixel stick all via a micro usb breakout. The 5v from the breakout is connected to a 5v pin on the Pi, the VCC pin on the Pro Mini, and the 5v pin on the Neopixels. All are connected to a common ground, of course.

SDA on the Pi is connected to A4 on the Pro Mini, and SCL to A5. These connections are made through a logic level converter as the Pro Mini logic is 5v. The 3v3 pin on the Pi provides the low voltage connection for the converter while the 5v on the high side is taken direct from the 5v power supply.

D6 on the Pro Mini is connected to the data pin on the Neopixel stick, but any of the PWM capable pins could be used.

Important - Don't Fry The Arduino

We have connected the 5v power directly to the VCC pin on the Pro Mini. The Pro Mini must be programmed using an FTDI cable or similar. You MUST remove the connection from the Pro Mini VCC to 5v before you connect the FTDI or bad things will happen ( from experience - I have a Pro Mini permanently running 'Blink' now ).


I've posted the Arduino sketch and example Perl code to Raspduino Github that demonstrates controlling the pixels from a Pi.

Alternate Setup and Power

Arduino Nano

I tried the setup with a 5v Nano which apart from the different pin positions on the Nano was a drop in replacement. Again, although the Nano can be programmed via its USB connection, as we have the 5v supply connected to the 5v pin on the Nano for the working setup, I'm sure we must remove the connection to the 5v rail before we connect the USB.

3v3 8mhz Arduino Pro Mini

Dropping in an 8mhz 3v3 Pro Mini means that I don't need the logic level converter for the I2C connection so that's one less component. For the 3v3 Pro Mini the power connection from the 5v rail goes to the RAW pin on the Pro Mini, not the VCC. According to specification this also means that the inbuilt protections on the board allow me to connect the FTDI programmer (set to 3v3 ) without removing the 5v connection to RAW, but I confess I have not tried it. Connecting the 3v3 D6 logic pin from the Arduino to the data pin on the Neopixel stick is fine. The Neopixel remains connected to the 5v rail for power.

Alternate Power

I think it would be fine to power this whole setup by powering the Pi Zero directly and taking 5v from the Pi 5v pin. If you wanted to attach more Neopixels you would need to power those separately. In that case I think I'd power the Pi directly, the Arduino from the Pi 5v, and a separate supply for the pixels.

In all schemes, remember that everything needs to share a common ground and remember to disconnect the Arduino 5v/VCC pin from the 5v supply rail before attaching your FTDI or USB cable.

Next Steps

I'm interested in having a simple pixel flashing and text message display project up for Christmas. A previous random purchase means that I have a few nRF24L01+ boards that I've never used. I see they are well supported with Arduino libraries so I need to add support to HiPi so I can control the Arduino end of things at a distance. Python libraries exist for the Pi of course, but it's not Perl is it.

Wednesday, November 29, 2017

HiPi Modules 0.68 Released

I needed to get an LCD screen working with one of the PCF8574 based backpacks popular in Arduino land. A couple of new modules are packaged:

  • HiPi::Interface::LCDBackpackPCF8574
  • HiPi::Interface::PCF8574

Sunday, September 24, 2017

HiPi Modules 0.66 Released

This release includes several major changes.

  • The HiPi GUI and dependence on wxPerl has been dropped.
  • HiPi::BCM2835 and HiPi::Wiring have been moved to their own distributions.
  • New HiPi:GPIO module for direct access to GPIO pin functions.
  • Installation is now by a deb package, from CPAN or using the source tarball.
  • Documentation has been rewritten.
  • The main distribution now uses the Perl Artistic License
  • A comprehensive command line tool - hipi-energenie - has been added to allow control of Energenie sockets, monitors and adapters.
HiPi has been updated to work with the current Raspbian Stretch and is also tested with later releases of Raspbian Jessie.  The distribution may still work on earlier versions of Raspbian Jessie.

Thursday, April 21, 2016

HiPi Modules 0.50 Released

This release adds basic support for the Energenie ENER314-RT RF controller board and an interface module for controlling ENER002 radio controlled sockets.

The ENER314-RT board is the 2 way transceiver version that appears to allow full control over the RFM69 controller using the Raspberry Pi  SPI device driver.  Access to additional functionality on this board will be added in the future.

Wednesday, April 6, 2016

HiPi Modules 0.49 Released

Change log

 - Re-factored Build.PL so there are no sudo calls or package installations in there. Moved all that to hipi-install script which now prompts for confirmation before installing any packages.

- Removed suid scripts hipi-i2c and hipi-pud as they  are obsolete since introduction of raspi-gpio and update of i2c device driver in Rasbian distribution.

The installation of packages in the Build.PL script of the distribution was vexing some folks severely. I've removed it so hopefully all can sleep well. They did after all have a point.

All necessary dependency checking is now done in the separate auto installation script - hipi-install - which also now prompts the user for confirmation before installing any package dependencies or pre-built binaries (Wx distribution ).

Monday, April 4, 2016

Raspberry Pi Gadget

The latest issue of MagPi magazine contains instructions on how to create a Raspberry Pi Gadget. That's a Pi Zero that you plug into a host machine's USB port. The host machine sees the USB gadget as a network interface and can therefore communicate with the Pi. The Pi Zero draws its power from the host machine across the USB connection and can use any connected networks on the host machine.

To much fun to pass up really. As the MagPi article refers to, there are also instruction on the adafruit site. Both sources state the need for a USB to serial TTL console cable that allows you to communicate with the Pi Zero over the serial port. I found that because I already had my Pi Zero set up and available on my network through an ethernet connection, I could perform all the setup on the Pi Zero just by ssh-ing to it before shutting down and using it as a gadget with my laptop.

Other than that, everything worked as described in the articles apart from DNS settings.

I couldn't get the Windows Internet Connection to share DNS service with the Pi. It allocated an IP fine and delivered itself as the DNS server (it got written to /etc/resolv.conf on the Pi) but the laptop didn't respond to any requests. Probably a problem with my laptop settings, but my quick fix was to add a couple of lines to /etc/resolv.conf.head that pointed at my home network and nameserver.


and all works fine.

I have to figure out a proper solution for when I'm out and about and want to share a wireless connection - but I can always fall back on editing /etc/resolv.conf.head and reloading everything with

sudo ifdown usb0
sudo ifup usb0

I needed a case for the gadget. The MagPi article shows a naked Pi Zero attached to a laptop with a simple cableless USB Micro B to A converter. It does look cool - but years of experience of destroying USB things and connectors with dongles that stick out of the side of laptops made me think "case and cable needed".

I've recently discovered the wonders of hot glue guns and have a case bought with my first Raspberry Pi Model B. The case has always been fairly pointless as you can't get at the GPIO pins with it closed. It isn't pointless anymore.

Still necessary to open the case to get at GPIO pins but at least now they won't be encrusted with discarded food chunks from my laptop bag.

HiPi::Perl Modules 0.46 released

This release adds methods that help the use of the I2C kernel device drivers with external devices that require repeated start transfers to work. This has been available in the kernel drivers for some time.