Thursday, 26 July 2018

Glowing Bean Bags with EL Wire

Overview

Here’s a simple summer project using EL wire and a little sewing to make your own bean bags  you can use for nighttime yard games or your Burning Man juggling act.
Before you begin, check out our introduction to electroluminescent materials including soldering to EL wire!
Let’s get started! For this project you will need:

Stitch Bean Bags

First up, stitch together two squares of any woven fabric with a straight stitch on your sewing machine. To make crisp corners, leave the needle down while you lift the presser foot and pivot the fabric around the needle.
Stitch most of the way around, but leave an opening on one side at least three fingers wide. Clip the corners at a 45 degree angle and turn the bag right side out. Use a pencil or chopstick to push out the corners, then press with an iron.

Add EL Wire

Now it’s time to attach the EL wire. Bend your strand at a 90 degree angle and hide the connector between the layers of fabric. Using a zigzag stitch, stitch over the EL wire, catching the fabric on one side. We're using clear quilting thread on the top, and plain thread in the bobbin.
When you get to a corner, you can again use the needle as a pivot to help bend the EL wire. Be careful not to pierce the wire with the needle, or you could break the tiny corona wires inside.

Use 'em!

All that remains is to plug in the tiny inverter and fill the bag with rice or dry beans. You can hand stitch the bag shut or use velcro tape to make it easier to change the battery.

Joy Featherwing

Overview

Make a game or robotic controller with this Joy-ful FeatherWing.This FeatherWing has a 2-axis joystick and 5 momentary buttons (4 large and 1 small) so you can turn your feather board into a tiny game controller. This wing communicates with your host microcontroller over I2C so it's easy to use and doesn't take up any of your precious analog or digital pins. There is also an optional interrupt pin that can alert your feather when a button has been pressed or released to free up processor time for other tasks.
This FeatherWing features Adafruit Seesaw technology - a custom programmed little helper microcontroller that takes the two analog inputs from the joystick, and 5 button inputs, and converts it into a pretty I2C interface. This I2C interface means you don't 'lose' any GPIO or analog inputs when using this 'Wing, and it works with any and all Feathers! You can easily stack this with any other FeatherWing because I2C is a shared bus. If you have an I2C address conflict, or you want to connect more than one of these to a Feather, there are two address-select jumpers so you have 4 options of I2C addresses
There's an optional IRQ (interrupt) line that you can use if you'd like the Wing to let you know when a button has been pressed. Since its optional (and most Feathers are perfectly happy polling the Wing for data) we left a bunch of solder jumpers so you can select what pin has the IRQ on it. Or just leave it disconnected!
We have an Adafruit Seesaw library for Arduino, Python and CircuitPython so you can get started with the Joy FeatherWing in a jiffy.

Pinouts

Power and Reset Pins

The power to the Joy Featherwing comes from the host Feather's 3.3V regulator, 3.3V and ground are both required and are highlighted above
The Feather's Reset pin ends up coming from the left-most circled pin and connects to the on-board reset button. This is great for resetting when you have the Wing on top

I2C Data Pins

All data and control is done over I2C so it works with any and all Feathers! SDA and SCL are highlighted above. There are 10K pullups to 3.3V on each.

I2C Addressing

Because multiple featherwings of all kinds can be stacked, the I2C address can be changed if necessary. The Joy Featherwing's default I2C address is 0x49, but soldering jumpers A0 and/or A1 can change this:
A0 A1 Addr
open open 0x49 (default)
closed open 0x4A
open closed 0x4B
closed closed 0x4C

Optional Interrupt Pin

Soldering one of the jumpers for the optional interrupt will connect that pin of your Feather to the interrupt line on the Joy Featherwing. This will allow your Feather to know whether a button has been pressed or released without having to read each button over and over in a loop.
Each Feather has different pin names/numbers so check your mainboard for which pin you want to use then solder that jumper closed

Assembly


adafruit_products_DSC_3980.jpg

Prepare the header strip:

Cut the strip to length if necessary. It will be easier to solder if you insert it into a breadboard - long pins down

adafruit_products_DSC_3981.jpg
adafruit_products_DSC_3981.jpg
adafruit_products_DSC_3978.jpg

Add the FeatherWing:

Place the featherwing over the pins so that the short pins poke through the two rows of breakout pads

adafruit_products_DSC_3983.jpg
adafruit_products_DSC_3983.jpg
adafruit_products_DSC_3984.jpg
adafruit_products_DSC_3987.jpg
adafruit_products_DSC_3989.jpg

And Solder!

Be sure to solder all pins for reliable electrical contact.

(For tips on soldering, be sure to check out our Guide to Excellent Soldering).

Start by soldering the first row of header

adafruit_products_DSC_3990.jpg
adafruit_products_DSC_3990.jpg
adafruit_products_DSC_3991.jpg
adafruit_products_DSC_3995.jpg
Now flip around and solder the other row completely

adafruit_products_DSC_3997.jpg
You're done with the two header strips.

Check your solder joints visually and continue onto the next steps

adafruit_products_DSC_3999.jpg
OK You're done! You can now plug your FeatherWing into your Feather and get your JOY on! 

 

Arduino Wiring & Test

Adafruit seesaw Library

To use with the Arduino IDE, you will need to install the Adafruit_seesaw library (here's the Github repository.)
It is available from the Arduino library manager so we recommend using that.
From the IDE open up the library manager...
And type in adafruit seesaw to locate the library. Click Install

Running the Example Code

The library comes with a Joy Featherwing example to get you started fast. Plug the Wing into the Feather and connect the Feather to your computer with the USB cable.
Open the Arudino IDE and load File->Examples->Adafruit_seesaw->joy_featherwing_example
The example assumes the interrupt pin is connected to pin 5 on the Feather via the first solder jumper on the far left. If you have not soldered this jumper the example code will need to be modified.
Click the Upload button and open the Serial Monitor at 115200 baud speed.
When you move the joystick you should see data appear in the format x,y
When you press a button, a notification should appear in the monitor.

FeatherJoyWing library!

If you'd like an all-in-one library that abstracts some of the FeatherWing for you, check out Leonid's FeatherJoyWing library  https://github.com/leonidlezner/FeatherJoyWing

Circuit Python Wiring & Test

Download Adafruit_CircuitPython_seesaw Library

To begin reading data from your Joy Featherwing, you will need to download Adafruit_CircuitPython_seesaw from our github repository. You can do that by visiting the github repo and manually downloading or, easier, just click this button to download the zip
Extract the zipped folder. And rename the folder it contains to Adafruit_seesaw.
Plug your CircuitPython device into your computer via the USB cable, and you should see the CIRCUITPY drive appear.
you should have the lib folder already on the drive. If you do not have the lib folder, or you encounter errors due to missing libraries, you can download the latest library package here.
 Drag the Adafruit_seesaw folder you just renamed to the CIRCUITPY/lib folder on your CircuitPython drive. The folder should look like this:
open the code.py file in the root of the CIRCUITPY drive and copy and paste the following code:
  1. import time
  2.  
  3. from board import SCL, SDA
  4. import busio
  5. from micropython import const
  6.  
  7. from adafruit_seesaw.seesaw import Seesaw
  8.  
  9. # pylint: disable=bad-whitespace
  10. BUTTON_RIGHT = const(6)
  11. BUTTON_DOWN = const(7)
  12. BUTTON_LEFT = const(9)
  13. BUTTON_UP = const(10)
  14. BUTTON_SEL = const(14)
  15. # pylint: enable=bad-whitespace
  16. button_mask = const((1 << BUTTON_RIGHT) |
  17. (1 << BUTTON_DOWN) |
  18. (1 << BUTTON_LEFT) |
  19. (1 << BUTTON_UP) |
  20. (1 << BUTTON_SEL))
  21.  
  22. i2c_bus = busio.I2C(SCL, SDA)
  23.  
  24. ss = Seesaw(i2c_bus)
  25.  
  26. ss.pin_mode_bulk(button_mask, ss.INPUT_PULLUP)
  27.  
  28. last_x = 0
  29. last_y = 0
  30.  
  31. while True:
  32. x = ss.analog_read(2)
  33. y = ss.analog_read(3)
  34.  
  35. if (abs(x - last_x) > 3) or (abs(y - last_y) > 3):
  36. print(x, y)
  37. last_x = x
  38. last_y = y
  39.  
  40. buttons = ss.digital_read_bulk(button_mask)
  41. if not buttons & (1 << BUTTON_RIGHT):
  42. print("Button A pressed")
  43.  
  44. if not buttons & (1 << BUTTON_DOWN):
  45. print("Button B pressed")
  46.  
  47. if not buttons & (1 << BUTTON_LEFT):
  48. print("Button Y pressed")
  49.  
  50. if not buttons & (1 << BUTTON_UP):
  51. print("Button x pressed")
  52.  
  53. if not buttons & (1 << BUTTON_SEL):
  54. print("Button SEL pressed")
  55.  
  56. time.sleep(.01)
Plug your Joy Featherwing onto the top of your CircuitPython Feather board.
When you open the REPL you should see output when you press buttons or move the joystick.

Downloads

Schematic

click to enlarge

Dimensions

in Inches

Tuesday, 24 July 2018

Stand-alone programming AVRs using CircuitPython

Overview

If you've ever wanted a stand alone AVR programmer, that is super easy to use, you've come to the right place!
This guide will show you how to turn any CircuitPython powered board with 4+ GPIO pins into an AVR progammer all on its own. No software like avrdude is needed, this software will program the chip all on its own, just drag the HEX file onto the CircuitPython disk drive.
Perfect to putting bootloaders on empty chips, or field-reprogramming a project!

Supported Chips

In theory, any and all AVR chips with SPI-programming interfaces are supported. However, we only have examples for ATmega328P chips (used in Arduino compatibles), ATtiny85 (used in original Trinket/Gemma), and ATmega2560 (Arduino Mega compatibles)
To program other chips, you'll need to find out the signature, size of the flash, and the flash-page size. You can find this in the datasheet or in avrdude.conf
This code only supports SPI-based programming, not JTAG, SWD or parallel!

Wiring


Nearly all AVRs have a 'serial' programming interface, that's what we'll be using to program them. If your chip requires SWD, JTAG or parallel, this software won't work!
In this example we'll show how to wire up an existing Arduino 328P compatible or raw 328P chip to a Feather M0 for programming
For other chips, the wiring is similar, but you'll need to look up which pins are Power, Ground, Reset, and SCK/MOSI/MISO

Power Pins

Do these pins first because they're easy to forget!
  • If connecting to a Arduino-compatible: connect GND on the Arduino to GND on the Feather. Then either plug the Arduino into USB, or connect the Arduino 5V to Feather USB
  • If connecting to a bare chip: connect both GND pins together and to the Feather GND. Connect AVCC to VCC to the Feather 3V pin
If you're breadboarding a bare ATMega328 chip, don't forget there are *two* power pins and *two* ground pins

Data Pins

  • Connect the CircuitPython SCK pin to the target SCK (on Uno/Atmega328 this is also known as Digital #13)
  • Connect the CircuitPython MISO pin to the target MISO (on Uno/Atmega328 this is also known as Digital #12)
  • Connect the CircuitPython MOSI pin to the target MOSI (on Uno/Atmega328 this is also known as Digital #11)
  • Connect CircuitPython D5 (or any digital pin, as long as you change the code too) to the target RESET
If you are breadboarding a chip, it may need a clock or crystal and it needs to be there to program the chip! If your board has a crystal or oscillator already, skip this. If you're programming a 'raw' ATmega328, you'll want to add it:
  • Connect CircuitPython D9 (or any digital pin with PWM out, as long as you change the code to) to the target XTAL1

Wiring Diagram for Raw ATMega328 Chip


  • VCC lines are Red
  • Ground/GND lines are Black
  • SCK is green
  • MOSI is blue
  • MISO is yellow
  • RESET is purple
  • XTAL is grey
Notice that the notch on the chip is to the right - away from the Feather!

Wiring for Arduino Compatible

For Arduino UNO and compatibles, we recommend powering from USB or DC power. Then connect GND pins together, and wire up Reset, SCK, MOSI, and MISO as seen above.
XTAL pin is not required, Arduinos have on-board crystals.

Software Setup

Installing Library

To use the AVR programming library you'll need to install the Adafruit CircuitPython AVRprog library on your CircuitPython board.
First make sure you are running the latest version of Adafruit CircuitPython for your board.
Next you'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle.  Our introduction guide has a great page on how to install the library bundle for both express and non-express boards.
Remember for non-express boards like the Trinket M0, you'll need to manually install the necessary library from the bundle:
  • adafruit_avrprog.mpy
You can also download the adafruit_avrprog.mpy from its releases page on Github.
Before continuing make sure your board's lib folder or root filesystem has the adafruit_avrprog.mpy file copied over.
Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.
For this simple example, we're assuming you don't need a clock-driving pin here, if you do, see the full example at the end of the page!

Imports

You'll need to import a few libraries
  • board - for assigning hardware pins
  • busio - we use SPI bus to talk to the target device
  • adafruit_avrprog - the library that we're using!

  1. >>> import board
  2. >>> import busio
  3. >>> import adafruit_avrprog

Initialize hardware

Next, create the hardware interface, you'll need an SPI port and one extra pin for the reset line. We'll use board.D5 to match our diagrams on the previous page, but it can be any pin you like!

  1. >>> spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
  2. >>> avrprog = adafruit_avrprog.AVRprog()
  3. >>> avrprog.init(spi, board.D5)

Communication / Signature Check

Next we'll verify that we can talk to the chip, once that works we are best off crafting our programmer into a full main.py project but at least we can quickly determine if things worked out.
  1. Start by initializing the programming interface with avrprog.begin() which will pull the reset line low and send some commands to get the chip to listen.
  2. Then read the signature, you'll get an array of numbers - its probably best to turn this into hex values before printing since they're referred to as hex values in datasheets.
  3. Finally, call avrprog.end()

  1. >>> avrprog.begin()
  2. >>> [hex(i) for i in avrprog.read_signature()]
  3. ['0x1e', '0x95', '0xf']
  4. >>> avrprog.end()
You can see here we have a 0x1E950F chip attached, also known at an ATmega328P

Full Example

You can save this code to main.py and use the REPL to see the signature data, it also includes the code for setting up the crystal-driving PWM output
  1. """
  2. Read Signature Test - All this does is read the signature from the chip to
  3. check connectivity!
  4. """
  5.  
  6. import board
  7. import busio
  8. import pulseio
  9. import adafruit_avrprog
  10.  
  11. spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
  12. avrprog = adafruit_avrprog.AVRprog()
  13. avrprog.init(spi, board.D5)
  14.  
  15. # we can generate an 6 MHz clock for driving bare chips too!
  16. clock_pwm = pulseio.PWMOut(board.D9, frequency=6000000, duty_cycle=65536//2)
  17.  
  18. avrprog.begin()
  19. print("Signature bytes: ", [hex(i) for i in avrprog.read_signature()])
  20. avrprog.end()

SPI / Wiring Errors

If something went wrong, you'll get an SPI transaction failed exception. Check your wiring! Also, sometimes the chip doesn't quite hear us, try connecting again.
Common problems:
  • The target isn't powered - make sure it is powered via USB or via the CircuitPython board. A shared Ground wire is required
  • Make sure you have the reset pin on the target connected to whatever pin you setup when you created the avrprog object
  • On ATmega2560, MOSI and MISO are connected opposite than the way you think. Either way, its OK to try swapping those two wires, see if that helps!
  • The target is expecting a crystal but you don't have one, for example the UNO bootloader requires that the chip have a crystal or oscillator connected up, it's not optional!

Programming Chips

OK now that you've read the signature, you can write some code!
We have a few examples available you can use 'out of the box' - all are available here. You can download the library zip to get all the files. For each programming demo, we also have a matching 'hex' file, that's a requirement - it's the file you'll be programming into the chip!
Copy the programming sketch into main.py and also grab the matching hex file. For example:
  1. """
  2. UNO Optiboot programming example, be sure you have the UNO wired up so:
  3. UNO Ground to CircuitPython GND
  4. UNO 5V to CircuitPython USB or make sure the UNO is powered by USB
  5. UNO Pin 13 -> CircuitPython SCK
  6. UNO Pin 12 -> CircuitPython MISO
  7. UNO Pin 11 -> CircuitPython MOSI
  8. UNO RESET -> CircuitPython D5 (or change the init() below to change it!)
  9. Drag "optiboot_atmega328.hex" onto the CircuitPython disk drive, then open REPL!
  10. """
Indicates you need optiboot_atmega328.hex
Then run the REPL and look for the Ready to GO, type 'G' here to start > prompt and type the letter G into the REPL. You should see the code begin by checking the identity of the chip (the signature), erasing the chip, then programming it.
It will skip most of the flash 'pages' because they're empty. At the end you'll get to the pages that are flashed and verified:
It's very very rare for something to go wrong during verification. But if it does you'll see something like this. Just start over by hitting ^C and ^D in the REPL to begin again.
That's it! You've programmed the chip. For more details, keep reading.

AVRprog API

Defining Chips

Before you can really do anything you need to tell AVRprog library what the chip is. We'll use a python dict for that. Define name (that's for your information and printing errors), sig - a list of the three-byte signature, flash_size - the size of the flash memory in bytes, page_size - the size of each flash memory page in bytes, and fuse_mask - a list of the four fuses in a list [low, high, ext, lock]
Fuse mask is the oddest one, but basically it defines which bits are actually used in each fuse. For example, the ext fuse is often only the bottom three bits, so its 0x07. If you're not sure, you can set all four to 0xFF and then when you burn fuses, set all the high bits to 1.
Here are some chip examples:
  1. attiny85 = {'name': "ATtiny85"}
  2. attiny85['sig'] = [0x1E, 0x93, 0x0B]
  3. attiny85['flash_size'] = 8192
  4. attiny85['page_size'] = 64
  5. attiny85['fuse_mask'] = (0xFF, 0xFF, 0x07, 0x3F)
  1. atmega328p = {'name': "ATmega328P"}
  2. atmega328p['sig'] = [0x1E, 0x95, 0x0F]
  3. atmega328p['flash_size'] = 32768
  4. atmega328p['page_size'] = 128
  5. atmega328p['fuse_mask'] = (0xFF, 0xFF, 0x07, 0x3F)
  1. atmega2560 = {'name': "ATmega2560"}
  2. atmega2560['sig'] = [0x1E, 0x98, 0x01]
  3. atmega2560['flash_size'] = 262144
  4. atmega2560['page_size'] = 256
  5. atmega2560['fuse_mask'] = (0xFF, 0xFF, 0x07, 0x3F)

Verify Signature

avrprog.verify_sig(chip_dict, verbose=True)
We suggest calling this first, you can call it whenever you like, and it will return True/False. chip_dict is that dictionary you made above

Erasing Chip

This one is easy, just call avrprog.erase_chip() - the chip erase command is the same for all chips. It may take a second on bigger chips. You must do this before programming new firmware!
Also, if your chip has the lock-firmware-fuse set, you may have to erase the flash before you can change the lock fuse.

Fuses

You can read, write and verify fuses.
Read fuses with
avrprog.read_fuses(chip_dict)
Which will return a list of the four fuses [low, high, ext, lock]
Write fuses with
avrprog.write_fuses(chip_dict, low=0xll, high=0xhh, ext=0xee, lock=0xkk)
Only arguments that are passed in will be written, so you can choose to write one fuse, or all 4.
Verify fuses with
avrprog.verify_fuses(chip_dict, low=0xll, high=0xhh, ext=0xee, lock=0xkk)
Only arguments that are passed in will be verified, so you can choose to verify one fuse, or all 4.

Flash

OK this is the good part, here's how you can write and verify flash memory. Reading memory to disk is not supported yet!
avrprog.program_file(chip_dict, "filename.hex", verbose=True, verify=True)
This function does all the work really, give it the chip information dictionary, and the name of a file (full path is OK). If verify is True, it will verify each page manually after writing. This is way faster than writing the whole file and then verifying the whole file so we recommend it.
If you really want, you can also verify against a file with:
verify_file(chip_dict, "filename.hex", verbose=True)
But it will check every single byte of the flash chip, so for example, if its a sparse hex file, like most bootloaders are where only a small portion of flash is data and the rest is empty, the empty parts are still checked. So it's very slow!