Showing posts with label Getting Started with Adafruit Trinket. Show all posts
Showing posts with label Getting Started with Adafruit Trinket. Show all posts

Friday, December 26, 2014

New Video

A new video is posted on my Amazon author page at https://www.amazon.com/author/mikebarela.  It is distilled from my appearance on the Adafruit Ask an Engineer show on September 17th, 2014.  The video covers my discussions with Ladyada and PT on the Adafruit Trinket and writing the book Getting Started with Adafruit Trinket just before Maker Faire 2014.



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.

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.

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!

Monday, April 21, 2014

Book Announcement: Getting Started with Trinket

Now you may officially know why I have not posted much in the last few months.  From September through December, I was writing tutorials on http://learn.adafruit.com as previously discussed.  I was contacted by Maker Media who asked for a book on the Adafruit Trinket microcontroller.  How could I refuse?  So I have been writing and writing some more.  Today Amazon now has it for preorder:

Getting Started with Adafruit Trinket


The price will probably be a bit less than those listed as it will probably be edited down a bit more.

A prerelease hardcopy will be out for Maker Faire New York, September 20 & 21st.

More information and it develops.  Thank you to my viewers for your support of my projects.  Mike