Archive

Archive for the ‘Internet of Things’ Category

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 2 comments

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

ATtiny104_ADC_example2

Pinout Diagram:

ATtiny104_Pinout_03


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 RX <--USART_Receive()
 TPI DATA/ADC1 PA1--|3         12|--PB2 ADC6/CDC TX -->USART_Transmit()
    (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

NodeMCU Breathing LED with Arduino IDE

August 20, 2015 18 comments

Breathing_LED

The first sketch used to test an Arduino compatible board is Blink.
After run Blink in my new NodeMCU Development Kit,
Started experiments with the WiFi connection:
-Mini servers to control leds, servos, etc..
-Send light intensity and temperature to ThingSpeak, Xively, Plotly etc..
-Serial data bridges
-etc.

The blue led in the board was used to signal the execution of a particular procedure.
Because the GPIO16 (DO) limitation to use the analogWrite() and looking to see my LED doing something else…

Inspired by the Apple’s “breathing” pattern used for the sleep indicator.
Here is the code to generate a “breathing LED”.

NOTE:
The Arduino code runs inside a multitasking application, the ESP8266 is running at the same time TCP and WiFi stacks.
The sketch can breake the multitasking execution if your code runs in a blocking section.
The delayMicroseconds() is one of those blocking functions, delay(0)  was used to prevent watchdog reset inside the PWM loops.

/*LED_Breathing.ino Arduining.com  20 AUG 2015
Using NodeMCU Development Kit V1.0
Going beyond Blink sketch to see the blue LED breathing.
A PWM modulation is made in software because GPIO16 can't
be used with analogWrite().
*/

#define LED     D0        // Led in NodeMCU at pin GPIO16 (D0).
 
#define BRIGHT    350     //max led intensity (1-500)
#define INHALE    1250    //Inhalation time in milliseconds.
#define PULSE     INHALE*1000/BRIGHT
#define REST      1000    //Rest Between Inhalations.

//----- Setup function. ------------------------
void setup() {                
  pinMode(LED, OUTPUT);   // LED pin as output.    
}

//----- Loop routine. --------------------------
void loop() {
  //ramp increasing intensity, Inhalation: 
  for (int i=1;i<BRIGHT;i++){
    digitalWrite(LED, LOW);          // turn the LED on.
    delayMicroseconds(i*10);         // wait
    digitalWrite(LED, HIGH);         // turn the LED off.
    delayMicroseconds(PULSE-i*10);   // wait
    delay(0);                        //to prevent watchdog firing.
  }
  //ramp decreasing intensity, Exhalation (half time):
  for (int i=BRIGHT-1;i>0;i--){
    digitalWrite(LED, LOW);          // turn the LED on.
    delayMicroseconds(i*10);          // wait
    digitalWrite(LED, HIGH);         // turn the LED off.
    delayMicroseconds(PULSE-i*10);  // wait
    i--;
    delay(0);                        //to prevent watchdog firing.
  }
  delay(REST);                       //take a rest...
}

NodeMCU ESP-12 Development Kit V1.0

August 15, 2015 Leave a comment

Here is my Pinout Diagram of this wonderful board for IoT development:

NodeMcuDevkit_v1.0_io

With added circuitry to manage the RESET and FLASH from the USB Serial Port, this board is  great for development.

After some LUA scripts and trying the Arduino IDE with it…  This Board is awesome!

NodeMcu_orange

I’ll continue experimenting with this one for a while…