Saturday, October 11, 2014

Comparing Adafruit's Pro Trinket to Trinket and Uno


A few weeks before Getting Started with Adafruit Trinket went to press, Adafruit introduces the Pro Trinket.  It hits a sweet spot in terms of price, performance, and size between the Trinket and the Arduino Uno.  In my comparison chart in the book it was a bit late to add the Pro Trinket so I'll do it here. First, sizing them up visually:
Adafruit Trinket (top), Pro Trinket (middle) and Arduino Uno (bottom) (source: Adafruit)

The Pro Trinket is closer to Trinket's size than an Arduino Uno.   It's smaller than an Arduino Micro or Nano I believe but not by a lot.


Adafruit Trinket

Adafruit Pro Trinket
Arduino Uno

Processor
ATtiny85
Surface Mount
ATmega328P QFN package
ATmega328P
DIP Package
Digital Pins
5
12
14
Analog Pins
3 (shared)
8
6
Pulse Width Modulated Pins
3
6
6
Pin Voltage
3.3 or 5 volts
3.3 or 5 volts
5 Volts
Board Voltage
3.3 or 5 volts
3.3 or 5 volts
5 Volts (3.3 on a pin)
Regulated Current Out
150ma
150ma
800ma
Power via 3.7V LiPo
Possible
Possible
No
USB Chip Onboard
No
No (but has FTDI header)
Yes
USB Plug Type
Mini-USB
Micro-USB
USB-B
USB Programmable
Yes
Yes
Yes
Memory (RAM/EEPROM)
512 / 512 bytes
2048 / 1024 bytes
2048 / 1024 bytes
Flash Memory (free)
8K (5,130 free)
32K (28,672 bytes free)
32K (32K free)
Add-on Shields
No
No
Yes
Board Size
31mm x 15.5 x 5mm
38mm x 18mm x 2mm
75.14 x 53.51 x 15.08mm
Approximate Cost
$6.95
$9.95
$24.95


In terms of Pro Trinket compatibility with Uno, the Pro Trinket does not have digital pins 2 and 7 exposed to pins (they are permanently used for USB, although you could hack into the USB resistors with very fine wire to get them).  Sketches that use pins 2 and 7 may most likely be modifiable to use other pins.

Like some Uno clones, the Pro Trinket adds analog pins 6 and 7 which could be very useful in some applications.

What do you think?  Does the Pro Trinket fit in well in the spectrum of available microcontrollers?  Capability, price, size?  

Trinket / ATtiny85 Internal Temperature and Voltage Sensing


Here is some info that was considered for Getting Started with Adafruit Trinket but was left on the cutting room floor.


The ATtiny85 on Trinket has the ability to sense the voltage applied to it.  This can be useful to determine if a battery is getting a bit too low.  The ATtiny85 also has a built-in temperature sensor.  Some say they don't use it due to a perceived lack of sensitivity.  But the sensor is fairly accurate if calibrated and if a bit of decimal math is used to adjust the temperature values read.

The temperature calibration is discussed in the Atmel document AVR122 - Calibration of the AVR's internal temperature reference at http://www.atmel.com/Images/doc8108.pdf You may need to take some readings and adjust some values in the code to dial-in the most accurate values over a temperature span.

The reading of voltage and temperature requires reading two values using the ATtiny85 analog to digital converter.  Unlike using the analogRead function on external pins, the voltage and temperature are in special areas that are read in similar method to analogRead.  ATtiny85 internal registers are polled to get the values.

Floating point arithmetic is used which adds considerable code overhead to the compiled program.

The sketch below reads the voltage and temperature and outputs the values over a software serial link.  An FTDI cable or FTDI friend connected to Trinket Pin #0 will output the values to a USB port which may be viewed by a terminal emulator.

/* 
 * Trinket / ATtiny85 Temperature and Voltage Measurement
 * Adapted from
 * http://forum.arduino.cc/index.php/topic,26299.0.html,
 * http://www.mikrocontroller.net/topic/315667, and
 * http://goetzes.gmxhome.de/FOSDEM-85.pdf
*/
// Values will be output to Trinket pin #0 via software serial for this example.  
// The library SendOnlySoftwareSerial by Nick Gammon is used to save a pin over
// using the SoftwareSerial library 
// http://gammon.com.au/Arduino/SendOnlySoftwareSerial.zip

#include <SendOnlySoftwareSerial.h>
SendOnlySoftwareSerial Serial(0);       // Output values via serial out on Pin #0

void setup() {
  // Setup the Analog to Digital Converter -  one ADC conversion
  //   is read and discarded

  ADCSRA &= ~(_BV(ADATE) |_BV(ADIE)); // Clear auto trigger and interrupt enable
  ADCSRA |= _BV(ADEN);                // Enable AD and start conversion
  ADMUX = 0xF | _BV( REFS1 );         // ADC4 (Temp Sensor) and Ref voltage = 1.1V;
  delay(100);                         // Settling time min 1 ms, take 100 ms
  getADC();

  Serial.begin(9600);                 // set up serial output
}

void loop() {
  int i;
  int t_celsius; 
  uint8_t vccIndex;
  float rawTemp, rawVcc;
  
  // Measure temperature
  ADCSRA |= _BV(ADEN);           // Enable AD and start conversion
  ADMUX = 0xF | _BV( REFS1 );    // ADC4 (Temp Sensor) and Ref voltage = 1.1V;
  delay(100);                    // Settling time min 1 ms, wait 100 ms

  rawTemp = (float)getADC();     // use next sample as initial average
  for (int i=2; i<2000; i++) {   // calculate running average for 2000 measurements
    rawTemp += ((float)getADC() - rawTemp) / float(i); 
  }  
  ADCSRA &= ~(_BV(ADEN));        // disable ADC  

  // Measure chip voltage (Vcc)
  ADCSRA |= _BV(ADEN);  // Enable ADC
  ADMUX  = 0x0c | _BV(REFS2);    // Use Vcc as voltage reference,
                                 //    bandgap reference as ADC input

  delay(100);                    // Settling time min 1 ms, there is
                                 //    time so wait 100 ms

  rawVcc = (float)getADC();      // use next sample as initial average
  for (int i=2; i<2000; i++) {   // calculate running average for 2000 measurements
    rawVcc += ((float)getADC() - rawVcc) / float(i);
  }
  ADCSRA &= ~(_BV(ADEN));        // disable ADC
  
  rawVcc = 1024 * 1.1f / rawVcc;
  //index 0..13 for vcc 1.7 ... 3.0
  vccIndex = min(max(17,(uint8_t)(rawVcc * 10)),30) - 17;   

  // Temperature compensation using the chip voltage 
  // with 3.0 V VCC is 1 lower than measured with 1.7 V VCC 
  t_celsius = (int)(chipTemp(rawTemp) + (float)vccIndex / 13);  
                                                                                   
  Serial.print("Temp: ");
  Serial.println(t_celsius);
  Serial.print("Vcc: ");
  Serial.println(rawVcc);
}

// Calibration of the temperature sensor has to be changed for your own ATtiny85
// per tech note: http://www.atmel.com/Images/doc8108.pdf
float chipTemp(float raw) {
  const float chipTempOffset = 272.9;           // Your value here, it may vary 
  const float chipTempCoeff = 1.075;            // Your value here, it may vary
  return((raw - chipTempOffset) / chipTempCoeff);
}

// Common code for both sources of an ADC conversion
int getADC() {
  ADCSRA  |=_BV(ADSC);          // Start conversion
  while((ADCSRA & _BV(ADSC)));    // Wait until conversion is finished
  return ADC;
}

The calibration methods used – voltage compensation, along with user testing for values placed in the chipTemp function, can get the accuracy of measurements to within one degree Celsius.

If you wish to keep parts and pin usage to a minimum, reading the temperature with the onboard sensor may provide the capability you need.

In addition, there are several benefits to being able to measure the supply voltage.  If the project is run on battery, Trinket or connected devices could be turned off if power reaches a predetermined lower limit.

Wednesday, October 8, 2014

Select One From Several Inputs

Yesterday, we selected one of many outputs with only 4 pins from an Adafruit Trinket microcontroller.  Here is the mirror project.  Say you have 8 digital data lines in and only few microcontroller lines (Adafruit Trinket only has 5 I/O pins).  You can use a circuit like the one below:


The chip used is the 74XX151 8 input multiplexer.  The binary address of the line you want is sent on Trinket Pins #0, #1, and #2.  Then the data line selected is read in on Trinket Pin #3.  This leaves Pin #4 as an output for serial data or other use.  The code would similar to the sample code below if we only wanted to read the 74151 input pin 3:

void setup() {
   pinMode(0,OUTPUT);
   pinMode(1,OUTPUT);
   pinMode(2,OUTPUT);
   pinMode(3,INPUT);
}

void loop() {
   digitalWrite(0, HIGH);
   digitalWrite(1, HIGH);
   digitalWrite(2, LOW);
   int mydata = digitalRead(3);
}

For a robust program, you'd probably cycle through each 74151 pin address and read the values you want. 

If you only want to multiplex 4 inputs, you can use a smaller multiplexer or just ground S2 low, freeing Trinket Pin #2.

Tuesday, October 7, 2014

Triggering Ten Pins with Four Outputs

A common issue in microcontrollers: what do you do when the pins on your controller do not match well with the outputs to your controlled items?  This is something I wanted to write about in Getting Started with Adafruit Trinket but I was over on page count as it was, so I'll post it here for you.

What the designer may not normally think about is using TTL logic chips.  Often called 74 series after the original numbering by Texas Instruments, there are hundreds of pre-existing integrated circuits to help make outstanding projects.  A number of years back it was one of the only ways to make circuits.... anyway...

Take Trinket.  There are only 5 I/O pins.  What if you want to use some sensor or serial input to output one of ten outputs (say to the delicious Adafruit Audio FX Sound Board that just came out).  You could be satisfied triggering only 4 of the sound board's trigger pins. But we want more!

So 4 binary digits can actually represent 16 different outputs (0 to 15 or 0 to F in hexadecimal).  Would one create a bunch of 7400 series logic (AND, OR, NAND) gates to change a number to one of several outputs?  You could, but it takes alot of gates;

Gates to decode 4 digital signals to 10 outputs
There is a way to reduce this down to one 16-pin DIP chip - use the 7442 BCD to decimal decoder IC.  All the logic above is already pre-wired into one easy to use chip!  So how would we wire Trinket to use the 7442 to the Adafruit Audio FX Sound Board which wants one of many pins to trigger a file to be played?  See the wiring diagram below:


If you set the right 4 bit number on Trinket pins 0 to 3, it will trigger one of ten outputs to the sound board, triggering the playing of up to ten sounds.  The code to trigger output 5 would include:

void setup() {
   pinMode(0,OUTPUT);
   pinMode(1,OUTPUT);
   pinMode(2,OUTPUT);
   pinMode(3,OUTPUT);
}

void loop() {
   digitalWrite(0, HIGH);
   digitalWrite(1, LOW);
   digitalWrite(2, HIGH);
   digitalWrite(3, LOW);
}

If you hooked a transmitter like an Adafruit Bluefruit or other receiver to Trinket Pin #4, you could use Trinket to set the right combination of 4 states on Pins #0-3 to trigger the right sound output. (Note: if you connect to pins #3 and #4 on Trinket, be sure to disconnect these when you use the USB programming connection, then reconnect when programming is done). And the Sound Board can hold alot of WAV file sound compared to a tiny bit on Trinket itself.

Note the data sheet I link to is the 74HC42.  The letters in the middle (if any) designate different types of logic (some improvements have been made in these chips over the years.  I'm partial to LS logic, although nearly any will do that I'm aware of, just be sure if you use 5 volt logic you are consistent).

You can see the usefulness of older chips in making the most of newer microcontrollers and modules.  With a bit of study of the 74 series chips, I'm sure you can find some that will help you in projects on your drawing board.

Saturday, October 4, 2014

Announced Today: Arduino Gemma

During his October 3rd presentation at Maker Faire Rome, Massimo Banzi gave a preview of a new collaboration and a new board: Adafruit Gemma becomes officially Arduino Gemma, a tiny but powerful wearable microcontroller board in a 27mm diameter package.
Powered by an Attiny85 and programmable with the Arduino IDE over USB, anyone will be able to easily create wearable projects with all the advantages of being part of the Arduino family. The board will be default-supported in the Arduino IDE, equipped with an on/off switch and a microUSB connector.
gemmapresentatio2
gemmapresentation





The Attiny85 is a great processor because despite being so small, it has 8K of flash and 5 I/O pins, including analog inputs and PWM ‘analog’ outputs. It was designed with a USB bootloader so you can plug it into any computer and reprogram it over a USB port (it uses 2 of the 5 I/O pins, leaving you with 3). Ideal for small & simple projects sewn with conductive thread, the Arduino Gemma fits the needs of most of entry-level wearable creations including reading sensors and driving addressable LED pixels.

After the fruitful joint effort developing Arduino Micro, once more the Arduino Gemma has been developed in collaboration with Adafruit Industries, one of the main leaders of the Maker movement. Technical specifications:
Microcontroller: ATtiny85
Operating Voltage: 3.3V
Input Voltage (recommended): 4-16V via battery port
Input Voltage (limits): 3-18V
Digital I/O Pins: 3
PWM Channels: 2
Analog Input Channels: 1
DC Current per I/O Pin: 40 mA
DC Current for 3.3V Pin: 150 mA
Flash Memory: 8 KB (ATtiny85) of which 2.5 KB used by bootloader
SRAM: 0.5 KB (ATtiny85)
EEPROM: 0.5 KB (ATtiny85)
Clock Speed: 8 MHz
MicroUSB for USB Bootloader
JST 2-PH for external battery

Wow, major announcement.  Default support for the ATtiny85 on Gemma would be a big help, to include Adafruit Trinket (the original, not Pro).

What will Arduino do next?

Qi Portable Power Receiving

In light of the new Adafruit Qi Transmitter, I diagram how you would design a Qi power receiver system.

The diagram shows the transmitter and a small, thin receiver built into a credit card size device with a micro-USB jack.  The card-size receivers are actually rated about 100 milliamps greater (600 ma) than the regular Qi receiver coil, Adafruit 1901 (500 ma).

You can plug the receiver directly into something needing power but that juice will go away when the receiver gets more than 8 millimeters away from the transmitter. 

To keep your project powered when the receiver is not powered, you can use the Adafruit Powerboost 500 C to regulate power and provide "off-grid" power with an extra LiPo battery.  Power will be available to the project and charge the battery simultaneously when the receiver is near the transmitter.  When the receiver is moved away, the Powerboost relies on the battery with very little transient changes (to quote Ladyada: "we did this by engineering the heck out of it (the Powerboost)".

Applications


What can you do with a Qi system?  Let's see:

  • Obvious is cell phone and tablet charging (what Qi was designed for)
  • Recharging your projects without plugging them in.  This could include:
  1. Firewalker shoes and other wearables
  2. Electronic Stuffed Animals
  3. Prop accurate projects (where a USB jack would be "unsightly")
  4. ... and more

Another idea is transmission of power through a window.  Why would one wish to do this?  Many people live in rented spaces where holes through exterior walls are not allowed.  Or maybe a homeowner does not want a hole in their house.  Placing a Qi transmitter on the inside and a receiver outside, you can get your project powered outside.  Just be sure the glass space is 2 to 8 millimeters, more than 8 millimeters is outside the Qi spec.

What uses do you see for Qi powering your Maker projects?

Friday, October 3, 2014

Making a Flexibly Powered Qi Charger

Just out yesterday: is a way nice Qi Charging module from Adafruit Industries.  This module allows you to hack together an inductive charger with input from multiple sources.  So I have looked at some methods to power the Qi charger:


  • The standard wall transformer for mains to 5 volts
  • A mobile Qi charger powered by a LiPo Battery
  • A solar powered, LiPo backed Qi Charger
The last two methods allow you to take your Qi charger away from the wall - out and about.  Here are some diagrams for all three methods:


Each of the methods should be connected (individually, not together - that's an either-or arrow) to the Qi charger.  Different LiPo batteries can be used with more or less capacity.  

Pretty much all of the Qi chargers I have seen have a wall connection - they have not been imagined as a mobile device, as most of the Qi chargers (well...) charge a mobile device.

The next article will look at methods for using a Qi receiver module and what we might want to do besides charge our phone or tablet.  Ideas?

Wednesday, October 1, 2014

Getting Back to Blogging after Publishing the Book

As blog readers are probably aware, I have been working on a new book for Maker Media called Getting Started with Adafruit Trinket.  It's been a labor of, well, work.  It has been a great experience but I'm ready to get back to a wider array of projects.

The book details use of the Trinket - an ATtiny85 breakout board from Adafruit Industries.


Here is the back and front cover.  It has changed over the last few months, the title a bit, the cover definitely new and a departure from many Make books.  Another departure is color - the inside is printed in color vs. black and white for most Getting Started books.  This allows you to really see the wiring and the projects.

The source code and the illustrative pictures have been posted on GitHub at https://github.com/trinketbook/GettingStartedWithTrinket

If you would like to see the Adafruit "Ask an Engineer" webcast in which I join Limor "Ladyada" Fried and Phil Torrone talk about many things including the book, here is the video:


Please excuse my actions and the bags under my eyes!  I had been awake for like 22 hours at that point and I need my beauty sleep after 16!  Curse transatlantic flights!

The book is available in final release via ebook in  ePub, Mobi, and/or PDF formats from O'Reilly at http://shop.oreilly.com/product/0636920031598.do or preorder the paperback from Amazon.com.  It should be out by mid-October!