Simple Graph With Arduino NANO and TFT Library

September 7, 2016 Leave a comment

This is a simple plotting example to experiment with the TFT Library and the LCD Display with the ST7735 controller (128×160 pixels).

simple_graph_01_4

First, a clean wiring diagram to connect the Arduino NANO and the Display. The SD Card adapter was wired  (CS and MISO) but not used in this Sketch.

tft_sd_nano_dim_wiring

Then, a wiring plan to put them together in the breadboard:

tft_sd_nano_dim_jumpers

 

tft_sd_nano_dim_assembled


 

The Sketch:

 

/*TFT_Graph.ino      Arduining 7 AUG 2016

-Arduino NANO and ST7735 LCD Display 128×160
-using TFT library
-Brighness control in pin 6
-offset is used to adjust vertical position of the graph.
*/

#include <TFT.h>
#include <SPI.h>

//pin definitions:
#define CS          10
#define DC           9
#define RST          8
#define BKLIGHT      6              // Backlight control pin

#define BRIGHTNESS 204              // Backlight intensity (0-255)
#define RANGE      100              // Vertical size of the graph in pixels.
#define WIDTH      128              // Horizontal size of Display in pixels.
#define HEIGHT     160              // Vertical size of Display in pixels.
#define PERSIST    500              // persistence of the graph (milliseconds).

TFT TFTscreen = TFT(CS, DC, RST);

int value ;
int last_value;
int x_pos= 0;                       // cursor position
int offset= 40;                     // vertical graph displacement in pixels.

void setup(){
TFTscreen.begin();                // initialize the display
TFTscreen.setRotation(0);
TFTscreen.background(0,128,0);    // dark green background
TFTscreen.stroke(255,255,255);    // white stroke
analogWrite(BKLIGHT,BRIGHTNESS);  //Set brightness.
TFTscreen.setTextSize(1);
TFTscreen.text(“Arduining.com”,50,151);
drawFrame();
clearGraph();
}

void drawFrame(){
TFTscreen.stroke(255,255,255);      // white stroke
TFTscreen.fill( 255, 255, 255 );    // White fill
TFTscreen.rect(0, 0, WIDTH, 21);
TFTscreen.setTextSize(1);
TFTscreen.stroke(0,0,0);            // black text
TFTscreen.text(“Volts: 1.0V/Div”,2,2);  // Example text.
TFTscreen.text(“Time:  10mS/Div”,2,12); // Exanple text
}
void clearGraph(){
TFTscreen.stroke(127,127,127);      // grey for the border
TFTscreen.fill( 180, 0, 0 );        // dark Blue for the background
TFTscreen.rect(0, 22, 128, 128);
TFTscreen.line(65, 23, 65, 151);    // vertical line
TFTscreen.line(1, 85,127, 85);      // horizontal line
TFTscreen.stroke(0,255,255);        // yellow stroke for plotting.
}

void plotLine(int last,int actual){
if(x_pos==0)last= actual;                     // first value is a dot.
TFTscreen.line(x_pos-1, last, x_pos, actual); // draw line.
x_pos++;
if(x_pos > WIDTH){
x_pos=0;
delay(PERSIST);
clearGraph();
}
}

void loop(){
last_value= value;
value= analogRead(A0);
value= map(value,0,1023,149,23);
value -= offset;
if(value <= 23) value= 23;         //truncate off screen values.
plotLine(last_value ,value );
delay(10);
}

 


Thanks for visiting…

Tiva C LaunchPad Base Plate, Easy Wiring

September 2, 2016 3 comments

A week ago went back to to Tiva C launchPad, this time I am interested in to refresh some concepts and try examples of RTOS (Real Time Operating System). The use of a RTOS usually means several hardware peripherals (sensors, keypads, displays .. etc). A breadboard and many jumpers was filling my imagination.

 

Tiva_Clean_Wiring

I need a Base Plate to organize the wiring, also a way of use top and bottom connectors in the launchPad. This is the story of last two days…

First thing, I need to finish the assembly of a tablesaw from Grizzly (it was resting semi-assembled in the garage).

Grizzly_tableSaw.jpg

Note:This project will be done in a couple of hours using acrylic plastic  if you have access to a laser cutter, in my case I am fan of woodworking… (the laser will come some day…).

TivaC_BasePlate_4.jpg

A serie of images will explain the project…

TivaC_BasePlate_3

While dried, we need a pin reference for the bottom:

TivaC_Launchpad_Bottom_pin_ref

TivaC_bottom_01

Time to put all together…

TivaC_BasePlate_2

TivaC_BasePlate_1

Tiva_Clean_Wiring_3

Here is how looks the wiring of the TFT Display:

Tiva-Clear_Wiring_2

This is the wiring diagram of the ST7735 used:

TIVA_ST7735_Wiring_

… thanks for visiting.

 

 

 

Wiring TFT Display to Arduino NANO

August 27, 2016 Leave a comment
 Wiring Diagram:

TFT_SD_NANO_DIM_wiring

A clean approach to attach the Display to Arduino NANO:

TFT_SD_NANO_DIM_jumpers

 All the components in place. SD Card adapter also is connected to the Arduino.

TFT_SD_NANO_DIM_assembled

Reading the analog input A0:

Test_001

Plotting the analog channel A0 using vertical lines:

Test_002

A small video to show the wiring while enjoying a Tchaikovsky’s  Waltz:

 

Testing my small library. 5×7 font in different sizes.

MicroLib_03_text

Testing drawRect() and fillRect() of the small library:

MicroLib_03_Rect_test

 Using the small library to show A0 value and his histogram:

MicroLib_A0_test_img

 

 

… this post is in progress… some code and examples…

 

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