Battery Life of an ESP8266 design

January 11, 2016 1 comment


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.


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.


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.



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


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

The RGB led and some other resistors were also removed.


This post is in construction….


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.




A quick pinout reference to start the wiring:


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:



Experimenting with different antennas:


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




Experimenting with Deep Sleep:

In progress….SEP-201_DS_Wiring

Boot Modes

Traffic Light , Finite State Machine with Arduino

September 18, 2015 1 comment


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.

-The C code was adjusted to run in the Arduino UNO.

-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:


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.


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.


Every state is described graphically in the following form:


The complete Finite State Machine Diagram:


The wiring diagram with the Arduino UNO:


This is the Sketch :

/*Traffic_Light.ino        17 SEP 2015
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:
#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
    Input = SENSORS & B00000011;     // read sensors
    State = FSM[State].Next[Input];  


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

Arduino For Free, Almost.

September 9, 2015 Leave a comment


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.


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

NodeMCU Breathing LED with Arduino IDE

August 20, 2015 18 comments


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

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”.

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  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
    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:


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!


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

Interrupts with the TRINKET analog comparator

July 12, 2015 Leave a comment

Here I’m sharing a working example using the analog comparator of the Adafruit’s  TRINKET (ATtiny85).



/*Trinket_comp_ISR.ino 12 JUL 2015
  Sketch to test interrupts fron the analog comparator.
  2.5 Volt in ANI0 (voltage divider).
  Potentiometer input in ANI1  An interrupt is generated in every change of the comparator output (toggle).
  The interrupt set the flag  "triggered".
  The main loop program checks for the flag, if the flag is set, test the output value  of the comparator
  and modify the state of the LED according.*/

#define LED 4 

volatile boolean triggered=false;

//------------ Analog Comparator Interrupt Routine ----------------------------
  triggered = true;
void setup(){
  ADCSRB &= ~(1<<ACME);             //Set AIN1 as the negative input (Potentiometer).
  DIDR0 |= (1<<AIN1D)|(1<<AIN0D);   // disable digital inputs.
//ACSR – Analog Comparator Control and Status Register   
  ACSR = B00001000; // comparator interrupt enabled and tripped on toggle. (compare with AN1)
      //  ||||||||_ ACIS0 ACIS1, ACIS0: Analog Comparator Interrupt Mode Select
      //  |||||||__ ACIS1 10= falling edge; 11= raising edge; 00= toggle.
      //  ||||||___ ACIC  Reserved Bit
      //  |||||____ ACIE  Interrupt Enable
      //  ||||_____ ACI   Analog Comparator Interrupt Flag (R/W), write 1 to clear it.
      //  |||______ ACO   Analog Comparator Output (read only)
      //  ||_______ ACBG  1= Bandgap Select (1.1V); 0= compare with AIN0 (pin 6)
      //  |________ ACD   Analog Comparator Disable

  digitalWrite(LED, HIGH);//LED on for 100 milliseconds.
  //Put the LED status as the analog comparator output (inverted)
  if(ACSR & (1<<ACO))digitalWrite(LED, LOW);
  else digitalWrite(LED, HIGH);

void loop(){
    if(ACSR & (1<<ACO))digitalWrite(LED, LOW);
    else digitalWrite(LED, HIGH);
    triggered= false;