ATtiny104 Xplained NANO Pinout Reference

April 7, 2016 Leave a comment

My Quick Pinout Reference of the ATtiny104 Xplained NANO Development Kit:

ATtiny104_Pinout_03

ATtiny104 ADC and USART Example

March 29, 2016 Leave a comment

This is a working example of the analog acquisition made with the ATTINY104.

ATtiny104_ADC_example2

 

ATtiny104 Xplained NANO and Atmel Studio 7 was used.


 

Here is the  C code:

 

/*ATtiny104_ADC_Serial.c
* Example of the DAC operation.
* A potentiometer is used to produce voltage variations at PA6 (ADC3),
* Analog value (upper 8 bits) is read and sent serially at 9600 bauds every 500 msecs.
* Created: 3/29/2016 10:30:00 PM
* Author : Ardunaut
                     ____________
               VCC--|1         14|--GND
  TPI CLK/ADC0 PA0--|2         13|--PB3 ADC7/CDC TX -->USART_Transmit()
 TPI DATA/ADC1 PA1--|3         12|--PB2 ADC6/CDC RX <--USART_Receive()
    (RESET)    PA2--|4         11|--PB1 ADC5/BUTTON
               PA3--|5         10|--PB0 ADC4
               PA4--|6          9|--PA7
      LED/ADC2 PA5--|7          8|--PA6 ADC3  <---- Potentiometer
                     \__________/
             Atmel ATtiny104 Xplained Nano
*/

#define F_CPU 1000000
#include <avr/io.h>
#include <util/delay.h>
#define LED_PIN    (1 << PA5)
#define BUTTON_PIN (1 << PB1)       // Not used in this example
#define BAUD 9600
#define MYUBRR F_CPU/16/BAUD        // +1, 0 or -1 to adjust the timming.

void PORTS_Init( void ){
  PUEB |= BUTTON_PIN;               // Enable Pull-Up function in PB1.
  PORTB |= BUTTON_PIN;              // Set Pull-Up for the Button.
  DDRA |= LED_PIN;                  // Configure LED pin as Output.
}

void USART_Init( unsigned int ubrr){  // Initialize USART
  UBRRH = (unsigned char)(ubrr>>8); // Set the baud rate
  UBRRL = (unsigned char)ubrr;
  UCSRB = (1<<RXEN)|(1<<TXEN);      // Enable Receiver and Transmitter
  UCSRC = (1<<USBS)|(3<<UCSZ0);     // Set Format: 8 data, 2 stop bit
}

void USART_Transmit( unsigned char data ){
  while ( !( UCSRA & (1<<UDRE)) );  // Wait for empty buffer.
  UDR = data;                       // Put data into buffer.
}

unsigned char USART_Receive( void ){
  while ( !(UCSRA & (1<<RXC)) );    // Wait for received data.
  return UDR;                       // Return received data.
}

void USART_Flush( void ){
  while ( UCSRA & (1<<RXC) ){};
}

void ADC_Init( void ){         // Initialize the ADC 

 //Set Voltage reference as VCC and select channel ADC3 (PA6).
  ADMUX =
            (0 << REFS1) |     // VCC as V. reference, bit 1
            (0 << REFS0) |     // VCC as V. reference, bit 0
            (0 << MUX2)  |     // use ADC3 (PA6), MUX bit 2
            (1 << MUX1)  |     // use ADC3 (PA6), MUX bit 1
            (1 << MUX0);       // use ADC3 (PA6), MUX bit 0
      
  //Set prescaler to 64 for MCU running at 8MHz (125 ksasmples/sec)
  ADCSRA = 
            (1 << ADEN)  |     // Enable ADC 
            (1 << ADPS2) |     // Set prescaler=64 (125kHz), bit 2 
            (1 << ADPS1) |     // Set prescaler=64 (125kHz), bit 1 
            (0 << ADPS0);      // Set prescaler=64 (125kHz), bit 0 
      
  // ADLAR is set to 1 (Left-shift result, bits ADC9..ADC2 in ADCH)     
  ADCSRB =  
          (1 << ADLAR);        // Left Adjustment for ADC Result Readout 
}

//-----------------------------------------------------------------------------

int main( void ){
  PORTS_Init();
  USART_Init(MYUBRR);
  ADC_Init();
  uint8_t digit,data;
  
  while(1){
    ADCSRA |= (1 << ADSC);          // start ADC measurement
    while (ADCSRA & (1 << ADSC) );  // wait till conversion complete 
  
    PORTA &= ~LED_PIN;              // Switch on the LED.
    data= ADCH;
    digit= data/100;
    USART_Transmit( '0'+digit );    // Transmit hundreds
    digit= (data % 100)/10;
    USART_Transmit( '0'+digit );    // Transmit tens
    digit= data % 10;
    USART_Transmit( '0'+digit );    // Transmit units
    USART_Transmit( 13 );           // Transmit Car Return
    USART_Transmit( 10 );           // Transmit Line Feed    
    _delay_ms(50);
    PORTA |= LED_PIN;               // Switch off the LED.
    _delay_ms(450);                 // Delay before next data acquisition.
  }

}  // End of main.

 

 

ATtiny104 Xplained NANO, experimenting.

March 24, 2016 3 comments

Atmel launches  the ATtiny104 Xplained Nano evaluation kit, a nice way of introducing this $0.40 microcontroller.

ATtiny104_img

The small and low cost evaluation board includes an on-board programmer.

After received two boards from Digikey, started downloading the Atmel Studio.

ATTINY104_X.jpg

The Atmel Studio is a “BIG” tool and takes time for the installation, but after the waiting, everything went smooth.

I spent some time drawing the Pinout Diagram, it helps me to learn more about the mini development kit.

ATtiny104_IOe.jpg

In no time the ATtiny104 was reading the PUSH BUTTON and controlling the on board LED.

Attiny104_blinking.jpg

Next thing to do:  experimenting with the serial port.

The character ‘U’ is sent every 500 milliseconds if the BUTTON is pressed.

The working code  transmitting at 9600 bauds:


 

/* ATTINY104_Serial.c 
*  Created: 3/24/2016 08:20:00 PM 
*  Author : Ardunaut
* Program to test serial transmission. 
* While the BUTTON is pressed: 
* 'U' character is transmitted every 500 milliseconds. 
* The LED blinks when a char transmitted. 
* FOSC=1000000 :"After powering up the device or after a reset the system clock is automatically 
* set to calibrated internal 8MHz oscillator, divided by 8" (Datasheet pag.32) 
* Using C functions from: http://www.atmel.com/Images/Atmel-42505-8-bit-AVR-Microcontroller-ATtiny102-ATtiny104_Datasheet.pdf 
* Note: Zeros ('0') were removed from the register names to make the code compatible. 
                 __________
           VCC--|1       14|--GND (TPI CLK)
           PA0--|2       13|--PB3 (CDC TX) -->USART_Transmit()
(TPI DATA) PA1--|3       12|--PB2 (CDC RX) <--USART_Receive()
   (RESET) PA2--|4       11|--PB1 (BUTTON)
           PA3--|5       10|--PB0
           PA4--|6        9|--PA7
     (LED) PA5--|7        8|--PA6
                \__________/
        Atmel ATtiny104 Xplained Nano
  */ 

#include <avr/io.h>
#include <util/delay.h>

#define LED_PIN    (1 << PA5)
#define BUTTON_PIN (1 << PB1)
#define FOSC 1000000
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD   //adding or subtracting 1 may be necessary.

void PORTS_init(void){  
  PUEB |= BUTTON_PIN;         // Enable Pull-Up function in PB1.
  PORTB |= BUTTON_PIN;        // Set Pull-Up for the Button.
  DDRA |= LED_PIN;            // Configure LED pin as Output.
} 

void USART_Init( unsigned int ubrr)
{
  /*Set baud rate */
  UBRRH = (unsigned char)(ubrr>>8);
  UBRRL = (unsigned char)ubrr;
  /*Enable receiver and transmitter */
  UCSRB = (1<<RXEN)|(1<<TXEN);
  /* Set frame format: 8data, 2stop bit */
  UCSRC = (1<<USBS)|(3<<UCSZ0);
  _delay_ms(250);
  PORTA |= LED_PIN;         // Switch off the LED.
  }
    
void USART_Transmit( unsigned char data )
{
  /* Wait for empty transmit buffer */
  while ( !( UCSRA & (1<<UDRE)) );
  /* Put data into buffer, sends the data */
  UDR = data;
}

unsigned char USART_Receive( void )
{
  /* Wait for data to be received */
  while ( !(UCSRA & (1<<RXC)) );
  /* Get and return received data from buffer */
  return UDR;
}

void USART_Flush( void )
{
  unsigned char dummy;
  while ( UCSRA & (1<<RXC) ) dummy = UDR;
}

/*----------------------------------------------------------------------------------------------------------------------------------------------------------*/
void main( void ){
  PORTS_init();
  USART_Init(MYUBRR);
  
  while(1){
    while(PINB & BUTTON_PIN){}  // Wait until the Button is pressed.
    PORTA &= ~LED_PIN;          // Switch on the LED.
    USART_Transmit( 'U' );
//    USART_Transmit( 85 );
    _delay_ms(50);
    PORTA |= LED_PIN;           // Switch off the LED.
    _delay_ms(450); 
  }
}

 

 

Some soldering to continue experimenting with the Xplained NANO in a breadboard.

ATtiny104_breadboard

ATtiny104_ready

Now, looking for an efficient code and low power solution to create an IoT node…

First, testing the hardware:

ATtiny104_RFcoms (1)


 

Transmitting at 300 bauds with the USART connected to the 433Mhz RF module:

ATtiny104_RF300


 

This is the code to transmit “HELLO” every second (while the button is pressed).

/* * ATTINY104_RF300.c 
* -Using the USART for 300 bauds transmission with the RF Module (433Mhz). 
* -Tree 0xAA bytes are sent as preamble of the message to balance the receptor. 
* -While the BUTTON is pressed "HELLO" message is transmitted every second aprox. 
* -Characters are separated 50 milliseconds (20 milliseconds of mark). 
* -The LED is ON during the message is transmission. 
* -FOSC= 1000000 : "After powering up the device or after a reset the system clock is automatically 
*  set to calibrated internal 8MHz oscillator, divided by 8" (Datasheet pag.32) 
* -Using C functions from: http://www.atmel.com/Images/Atmel-42505-8-bit-AVR-Microcontroller-ATtiny102-ATtiny104_Datasheet.pdf 
* -Note: Zeros ('0') were removed from the register names to make the code compatible. 
* -Created: 3/26/2016 07:25:00 PM 
* -Author : Ardunaut
                 __________
           VCC--|1       14|--GND (TPI CLK)
           PA0--|2       13|--PB3 (CDC TX) -->USART_Transmit()
(TPI DATA) PA1--|3       12|--PB2 (CDC RX) <--USART_Receive()
   (RESET) PA2--|4       11|--PB1 (BUTTON)
           PA3--|5       10|--PB0
           PA4--|6        9|--PA7
     (LED) PA5--|7        8|--PA6
                \__________/
        Atmel ATtiny104 Xplained Nano  
*/ 

#include <avr/io.h>
#include <util/delay.h>

#define LED_PIN    (1 << PA5)
#define BUTTON_PIN (1 << PB1)
#define FOSC 1000000 // Clock Speed 
#define BAUD 300     
#define MYUBRR FOSC/16/BAUD-1

void PORTS_init(void){  
  PUEB |= BUTTON_PIN;         // Enable Pull-Up function in PB1.
  PORTB |= BUTTON_PIN;        // Set Pull-Up for the Button.
  DDRA |= LED_PIN;            // Configure LED pin as Output.
} 

void USART_Init( unsigned int ubrr){
  //Set baud rate:
  UBRRH = (unsigned char)(ubrr>>8);
  UBRRL = (unsigned char)ubrr;
  //Enable receiver and transmitter:
  UCSRB = (1<<RXEN)|(1<<TXEN);
  // Set frame format: 8data, 2stop bit :
  UCSRC = (1<<USBS)|(3<<UCSZ0);
  _delay_ms(250);
  PORTA |= LED_PIN;         // Switch off the LED.
  }
    
void USART_Transmit( unsigned char data ){
  // Wait for empty transmit buffer:
  while ( !( UCSRA & (1<<UDRE)) );
  // Put data into buffer, sends the data:
  UDR = data;
}

unsigned char USART_Receive( void ){
  // Wait for data to be received:
  while ( !(UCSRA & (1<<RXC)) );
  // Get and return received data from buffer :
  return UDR;
}

void USART_Flush( void ){
  unsigned char dummy;
  while ( UCSRA & (1<<RXC) ) dummy = UDR;
}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------*/
void main( void ){
  PORTS_init();
  USART_Init(MYUBRR);
  
  while(1){
    while(PINB & BUTTON_PIN){}  // Wait until the Button is pressed.
    PORTA &= ~LED_PIN;          // Switch on the LED.
    
    USART_Transmit( 0xAA );   // To balance the receptor.
    _delay_ms(50);
    USART_Transmit( 0xAA );   // To balance the receptor.
    _delay_ms(50);    
    USART_Transmit( 0xAA );   // To balance the receptor.
    _delay_ms(50);
    USART_Transmit( 'H' );
    _delay_ms(50);
    USART_Transmit( 'E' );
    _delay_ms(50);
    USART_Transmit( 'L' );
    _delay_ms(50);
    USART_Transmit( 'L' );
    _delay_ms(50);
    USART_Transmit( 'O' );    

    PORTA |= LED_PIN;         // Switch off the LED.
    _delay_ms(650);           // delay to complete one second.
  }
}

More experiments and learning …

blogger-at-work

Battery Life of an ESP8266 design

January 11, 2016 1 comment

ESP-12_onBatt_01

The use of the relatively “new” and absolutely  “cheap” ESP8266 IC generates a rush in the apparition of new IoT  applications.

Lately I’m looking for devices that can run on batteries for months.

A device inside my mailbox twittering when the door was opened.

A moisture sensor reporting every hour from a spot in the garden, sending the data to ThingSpeak .

A tank level sensor reporting when low critical level is reached.

etc…

Working with Energía and the Texas Intrument’s Launchpads found the EnergyTrace tool.

How EnergyTrace measure power?
A software controlled DC-DC converter generates the target power supply (1.2 V-3.6 V).
The time density of the DC-DC converter charge pulses equals the energy consumption of the target microcontroller.
A built-in calibration circuit defines the energy equivalent for a single charge pulse.
The width of pulses remains constant. By counting  the charge pulses during a period of timed the average current is calculated.

BatteryLife

By removing the jumper of 3.3V to the development section and connecting my ESP8266 based device the EnergyTrace can be used to estimate the battery life.

MSP432_EnergyMonitor

 

My  approach for the battery  power is to use a LDO regulator with very low quiescent current and 3 AA alkaline batteries.

regulator4

To reduce power the power-on LED and the light sensor LDR were removed.

The RGB led and some other resistors were also removed.

ESP12_Sleep_3xAA_02

This post is in construction….

blogger-at-work

ESP-201 an ESP8266 breadboard friend

December 31, 2015 Leave a comment

To plug the ESP-201 in the breadboard is necessary to remove the 4 pin connector from the botton of the board and install it in the component side.

 

ESP-201_presenter

 

A quick pinout reference to start the wiring:

ESP-201_pinout

Note: GPIO9 and GPIO10 are connected to the FLASH memory (may be unusable). Here is a “surgery” procedure to use them: ESP-201 board modification

First experiment sending the analog value from A0 to ThingSpeak:

ESP-201_A0

 

Experimenting with different antennas:

ESP-201_Antenna_tests

Best results with the external antenna (the metal one):

ESP-201_A0_ThingSpeak

 

 

Experimenting with Deep Sleep:

In progress….SEP-201_DS_Wiring

Boot Modes

Traffic Light , Finite State Machine with Arduino

September 18, 2015 1 comment

Traffic_Light_Intersection2

This exercise with the Arduino is inspired by the course:

Embedded Systems – Shape The World

Chapter : Finite State Machines

By Dr. Jon Valvano , professor in the Department of Electrical and Computer Engineering at The University of Texas.

http://users.ece.utexas.edu/~valvano/Volume1/E-Book/C10_FiniteStateMachines.htm

-The C code was adjusted to the Arduino and checked with a simulator.

-Some changes were made in labels for better understanding.

-Most of the time was dedicated to make clear ilustrations and diagrams.

As the chapter explain, the Ojetive is:

Design a traffic light controller for the intersection of two equally busy one-way streets.

The goal is to maximize traffic flow, minimize waiting time at a red light, and avoid accidents.

Traffic light Controller using two sensors and 6 lights.

Two sensors detects the presence of cars in each direction.

Two traffic lights (Red,Yellow,Green) to control the traffic flow.

List heuristics describing how the traffic light is to operate:

-If no cars are coming, stay in a GREEN state. (which one doesn’t matter).

-To change from GREEN to RED, implement a YELLOW light of 0.5 seconds.

-GREEN lights will last at least 3 seconds.

-If cars are only coming in one direction, move to and stay GREEN in that direction.

-If cars are coming in both directions, cycle through all four states.

General System Diagram:

Traffic_Light_Controller

Definition of Inputs and Output:

Two Inputs from Switches detecting presence of cars in each direction.

Six Outputs to control the Lights ( RED, YELLOW, GREEN) in each direction.

Traffic_Input_Output7

Finite State Machine Transition Table

Four states are defined:

State O: goS    Cars going in South Direction GREEN signal, West Direction with RED signal.

State 1: waitS  Cars waiting in South Direction YELLOW signal, West Direction with RED signal.

State 2: goW    Cars going in West Direction GREEN signal, South Direction with RED signal.

State 3: waitW  Cars waiting in South Direction YELLOW signal, South Direction with RED signal.

FiniteSatateMachineTable3

Every state is described graphically in the following form:

Traffic_Machine_State

The complete Finite State Machine Diagram:

Traffic_States_Diagram2

The wiring diagram with the Arduino UNO:

Traffic_Light_Schematics4

This is the Sketch running in the simulator:

/*Traffic_Light.ino        17 SEP 2015   Arduining.com
Implementing the traffic light controller using Finite State Machines modeling.
Using Direct Port Manipulation in the Arduino UNO.
Creating a Data Structure in C.
Port B pins 0 and 1 as inputs (Arduino pins 8 and 9):
Pin 8 = North Switch
Pin 9 = East Switch
Port D pins 2 to 7 as outputs (Arduino pins 2 to 7):
Pin 2 = North Red light
Pin 3 = North Yellow light
Pin 4 = North Green light
Pin 5 = East Red light
Pin 6 = East Yellow light
Pin 7 = East Green light
Based in: Finite State MachinesFrom:http://users.ece.utexas.edu/~valvano/Volume1/E-Book/
*/
#define SENSORS  PINB      // define the ATmega328 Port to read the switches
#define LIGHTS   PORTD     // define the ATmega328 Port to drive the lights
// Linked data structure
struct State {
  int Out; 
  int Time;  
  int Next[4];}; 
typedef const struct State STyp;
#define goS   0
#define waitS 1
#define goW   2
#define waitW 3
STyp FSM[4]={
 {0x21,3000,{goS,waitS,goS,waitS}},       //State 0 (goS)   go South.
 {0x22, 500,{goW,goW,goW,goW}},       //State 1 (waitS) wait South.
 {0x0C,3000,{goW,goW,waitW,waitW}},    //State 2 (goW)   go West.
 {0x14, 500,{goS,goS,goS,goS}}};        //State 3 (waitW) wait West.
int State;  // index to the current state 
int Input; 

void setup(){
  DDRB &= B11111100;  // Port B pins 0 and 1 as inputs (Arduino pins 8 and 9)
  DDRD |= B11111100;  // Port D pins 2 to 7 as outputs (Arduino pins 2 to 7)
}

void loop(){
    LIGHTS = (FSM[State].Out)<<2;    // set lights
    delay(FSM[State].Time);
    Input = SENSORS & B00000011;     // read sensors
    State = FSM[State].Next[Input];  
}

Traffic_Simulation4

Any comment to improve this presentation will be appreciated, thanks for visiting Arduining.com

Arduino For Free, Almost.

September 9, 2015 Leave a comment

LowCostUno_000

It is amazing how the movement initiated by the Arduino Team has led to a widespread growth of both hardware and software to develop applications using microcontrolers.

This growth has allowed to offer an Arduino UNO Compatible for only $2.84 (Including shipping to many countries).

One of Massimo Banzi’s aspirations was to have an inexpensive and easy way to create devices that interact with their environment using sensors and actuators. Mission accomplished !.

?

?

I got this Arduino UNO compatible from China (Delivered in 10 days). After installing te CH340G driver, I was able to download sketches without problems.

?

?

While taking some pictures with the Magnifier Plus-HD of my Galaxy NOTE, started to realize how well designed and assembled is this development board.

?

?

Upon inspection of this Arduino compatible we can observe :

-AMS 1117-3.3, 3.3 Volts regulator, capable of providing 1 Ampere. The regulator is capable of feeding radios and telephone modems directly, specially the ESP8266 for Internet connections.

– 25V capacitors, instead of the usual 16V used in some clones.

-16Mhz Crystal, instead of a Ceramic Resonator, which allows us to measure time more precisely.

-MICRO-USB connector. Much more common and easy to find.

-Additional holes to solder headers for:

*Double connector for the whole Arduino I/O.

* Analog pins A6 and A7.

*Serial connection (GND 5V TX RX)

*I2C onnection (GND 3.3V SCL SDA)

Originally was considered the use of the DIP-28 package rather than the SMD counterpart of the ATMEGA328 in the Arduinos, now it turns out that is less expensive to replace the whole product instead of substitute the ATMEGA328. You can buy from MOUSER or DIGIKEY the microcontroller in 10 counts for a price bigger than the complete board! . This without taking into consideration that one still has to burn the bootloader in the new device.

LowCostUno_003

We are very fortunate to live in this age with plenty of sensors and controllers to experiment with.

All of this is empowering people to create new solutions to old problems, many of them will become products and prosperous business.

In honor of the enormous contribution the Arduino Team has made and continue making, I absolutely support the purchase of their products to give continuity to the great project of democratizing knowledge.

The open hardware and open software movements has allowed everyone to have access to these advances without regards of their socio-economic status.

You did it Massimo!

Categories: Uncategorized
Follow

Get every new post delivered to your Inbox.

Join 47 other followers