Showing posts with label Arduino. Show all posts
Showing posts with label Arduino. Show all posts

Sunday, July 10, 2016

Programming ESP-01. No cable? how 'bout some XBee Explorers

Nothing too complex or new, it's just that i spent some time hooking up an ESP8266 to Arduino, trying to use it as a programmer. Then, after 2 cups of coffee, i saw some XBee Explorers over my desk. ( FTDI chip + 3v3 Inside ;) )


TL;DR


I Had some ESP8266 modules laying around and i was thinking, since they can be programmed it might be no need to hook it up to an extra, and less powerful, Arduino board and throw some AT commands, but to load a firmware directly into the ESP8666 MCU.

The software setup is very simple. we can add support for the ESP8266 in the Arduino IDE through the Boards Manager. Instructions are in the following link, this will install libraries, configuration and boards descriptions, and the compilers/tools for Espressif/Xtensa ESP8266 boards.

https://github.com/esp8266/Arduino

Tutorials about how to program the ESP modules are all over the Internet, you just need an FTDI like 3v3 capable TTL to USB cable.

There was just one problem, i had no FTDI cable to program it. So i tried to pass through the Arduino UNO, since it has a 3v3 regulator. NO SUCCESS! (What i did was to tie RESET to GND on the UNO)

Here's how to connect the ESP-01 with Arduino UNO:
https://forum.arduino.cc/index.php?topic=283043.0

And here's where i came with the idea of using the uno as programmer, obviously it didn't woked. (although it could receive and respond to AT commands without any trouble):
http://stackoverflow.com/questions/31539424/using-arduino-as-ftdi-programmer-to-program-esp8266-esp-12

That was for ESP-12 (NodeMCU boards and the like)

There was when i saw the picture of a FTDI chip in my mind, i have a bunch of XBee Explorer Boards. all have a very decent 3v3 regulator and FTDI chip + mini usb connector (Basically a programmer)

The Explorer is too big for a half-breadboard, if you want to align it to get a row of holes on either side of the breadboard. you'll notice you can't.

However, you only need one side, the one with the pins: 3v3, GND, DIN, DOUT.



So, i made the same connections referenced above on he links to my explorer, left the RESET of the ESP-01 pulled up to 3v3 with a 10Kohm resistor and the GPIO-0 wired in the breadboard next to another jumper cable going to GND.

Use this GPIO-0 cable as a switch to put ESP-01 in programming mode when connected to ground after reset (Shortly pull down to GND the 3v3 pulled-up RESET pin, while GPIO-0 is Grounded)

It looks awful like this:



Compiles awesome like this:



Nothing too complicated, but fun if you have seen the hooking up with Arduino uno i was doing before. it had even a logic level converter for the serial line and stuff. and still didn't work

Now i need to think what to code inside this little boards. how cool!! :)

Monday, September 1, 2014

Bubble Display, Shift Register and a TMP36 (Showing temperature on a tiny led display)

A few days ago a friend of mine lend me an SparkFun ProMicro. It's a 5v, 16mhz Arduino compatible development board that fits on a breadboard.

After i soldered its legs i had to run something cool on it, so i thought it would be fun to hook up an (also small) hp 7 segment bubble led display, but it's easy to connect it directly to the atmega pins, so i got it connected with 7 instead of 12 cables thanks to an 74HC595 Shift Register, just to add a little more complexity fun on this.

So, what is a shift register? it's a kind of buffer chip on which you load the state of the n-bits (8 in the case of the 74hc595), and then you say: "ok, show me what i wrote there", and the D0-to-D7 pins on the chip get HIGH or LOW, depending on which bitmask you stored earlier.

That's perfect for the 7 segments plus dot of the LED display, with 3 pins you decide the state of the 8 segments of the display.

But this Bubble display got 4 characters, so i still need 4 pins to control the 4 common cathodes of the display, and choose where to display each character. This could be controlled with another 595, but each one use 3 pins, and there was just 4 cathodes, so there wasn't much advantage on using another chip.

Here a pic of this super tiny led display:



I got the code from luiSantos instructables tutorial, with some modifications, some of them pointed out in the comments below that tutorial, where a guy posted a modified version that works with common cathode displays, like this little bubble display.

I got the bitmasks for the characters from SparkFun's SevSeg Library. on the ".h" file you can find the definitions for the character's segments on the display.

They are written as 0bABCDEFGX, where x is the decimal dot, which the display has for every character block, and A-to-G are the segments of the display on this form:

  aaa
f        b
f        b
f        b
  ggg
e       c
e       c
e       c
  ddd   X

The display needs to be continuously updated because only one character is displayed each time. I found 7msec to be a good refresh rate for this display, lower than this makes the display blurry (with 6msec got the decimal dot jumping between the sides or appearing twice), with 8msec it gets flickery and is hard to read on it.


The parts:

4x    330 ohm Resistor
1x    74HC595 Shift register
1x    HP QDSP-6064 Bubble 7 segments LED display
1x    TMP36 Analog devices Temperature sensor
1x    SparkFun's ProMicro Arduino compatible Board
1x    Breadboard (or 2 half-sized ones)
Alot  Jumper cables


This is the assembling of what i did with the parts:



There are a lot of cables, but just a few more than when i tried the display alone with the arduino, and now got me only 7 arduino pins instead of 12, so you can for example keep rx and tx serial pins (0,1) and some PWM capable pins on the Arduino UNO free for other tasks better than show your room's current temperature.


The Code looks like this:



/*
* created by Rui Santos, http://randomnerdtutorials.com
* modified by: Raphango
* Still more changes by: rccursach 2014
* Temperature Sensor Displayed on 4 Digit 7 segment common CATHODE
* 2013
* A Small portion from SparkFun's SevSeg Libray https://github.com/sparkfun/SevSeg
* SparkFun, Nathan Seidle, 2012 (Beerware license). Based on http://arduino.cc/playground/Main/SevenSegmentLibrary Dean Reading, 2012.
*/


const int digitPins[4] = {4,5,6,7}; //4 common CATHODE pins of the display.
const int clockPin = 10;    //74HC595 Pin 11
const int latchPin = 16;    //74HC595 Pin 12
const int dataPin = 14;     //74HC595 Pin 14
const int tempPin = A0;     //tmp36 temperature sensor pin

//As seen on SparkFun's SevSeg Library
const byte digit[10] =      //seven segment digits
{
  0b11111100, // 0
  0b01100000, // 1
  0b11011010, // 2
  0b11110010, // 3
  0b01100110, // 4
  0b10110110, // 5
  0b10111110, // 6
  0b11100000, // 7
  0b11111110, // 8
  0b11110110  // 9
};

int digitBuffer[4] = {0};
int digitScan = 0;
float tempC;

void setup(){               
  for(int i=0;i<4;i++)
  {
    pinMode(digitPins[i],OUTPUT);
  }
  pinMode(tempPin, INPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT); 
  Serial.begin(9600);
}


void loop(){
  float temp_v = (float)(analogRead(tempPin) * (5.0 / 1023.0));
  tempC = (100 * temp_v) - 50;

  printDisp(tempC, 1000); //params are: Float value, msec it will be displayed
}


void printDisp(float value, int msec){
  clearDisp();
  digitBuffer[3] = (int(value) % 1000)/10;
  digitBuffer[2] = (int(value) % 10);
  digitBuffer[1] = (int(value*10) % 10);
  digitBuffer[0] = (int(value*100) % 10);
  
  //Get it displayed until msec Milliseconds passed
  long ti = millis();
  long tf = millis();
  while(tf-ti < msec){
    tf = millis();
    updateDisp();
  }
}

//writes the temperature on display
void updateDisp(){
  for(int i=0; i<4; i++){
    clearDisp();
    digitalWrite(digitPins[i], LOW); //Changed to LOW for turning the leds on.
    
    if(i==2) //Add decimal dot
      shiftOut(dataPin, clockPin, LSBFIRST, digit[digitBuffer[i]] | 0b00000001);
    else
      shiftOut(dataPin, clockPin, LSBFIRST, digit[digitBuffer[i]]);
    
    digitalWrite(latchPin, HIGH);
    digitalWrite(latchPin, LOW);
    
    delay(7); //If not delayed, digits are seen brurry, if the value is 8 you migth see the display frickering.
  }
}

void clearDisp(){
  for(byte j=0; j<4; j++) {digitalWrite(digitPins[j], HIGH);} // Turns the display off. Changed to HIGH
}


Because this code is just demostrative, now i want to make a library for this little displays to be managed with the shift register, maybe auto-refreshed with a timer. If i got some time i will write it and put the link on this post as an update.

Now this is how it looks like:




After i put on my protoboard a 5v Step-up converter and 2 AA Batteries and got it running all night just because this little red numbers look pretty cool in the dark.

And that's it!

Monday, June 30, 2014

Object Oriented Arduino

As we all noticed, Arduino is not more than a bunch of (Pretty nice organized/integrated though) libraries (Arduino, Wiring, some LCDs, etc), bootloader, and the AVR gcc compiler.

But as i was programing some sketches for arduino, i noticed that using a lot of functions to try not repeat myself on the setup() loop() of the ".ino" kind of sketches, i was building a big pile of "spaguetti code"... it was harder to follow the program execution as the code was growing.

Then just out of curiosity, intrigued by how those 2 functions and the others functions and loose variables written on a ".ino" sketch file ended into a C/C++ source, i opened my arduino installation directory, and started searching for some kind of template or something where my sketches were included/inserted.

Arduino cores source files

Here, i said "hey! 'main.cpp', let's have a look at this". this is the content of this main.cpp:


#include <Arduino.h>

int main(void)
{
init();


#if defined(USBCON)
USBDevice.attach();
#endif

setup();
 
for (;;) {
loop();
if (serialEventRun) serialEventRun();
}
     
return 0;
}


Then, if you have a look to the Arduino.h file, there is some of the avr libs, arduino constants, functions and other definitions.

This means that you can actually write plain C/C++ code and include it, just like any other C++ program you have written before.

Though that is not a secret (that you can write objects), the small code examples we get from internet (which are never OO based examples) and the way the IDE is designed, aren't of any help when you want/need to work with a big (nor medium) number of classes and files.

Now, the arduino IDE interface is kind of ugly when you try to add more files to your project, i can't remember exactly why, but you can try and shure you'll say "mmmh, isn't another ide for arduino out there?". Yes, there are.

I'm now using SublimeText with the Stino plugin:

Stino and SublimeText
Stino and SublimeText


In the last image i show you the setup() and loop() functions of a sketch that sends through XBee the value of a Sensor and the values obtained from a GPS if the data is obtained successfully from it.

In this case, every sensor implements a common interface, so i can add more sensors or exchange them without much trouble.
In the first line of loop(), the object sh calls a method named read() which returns a string of data. This public method was inherited from the implementation of this code:

/*
* sensors.h
* This is a class definition for the Sensors interface.
* The list of virtual methods are common to all sensors
* to perform basic tasks.
*
* (C) Ricardo Carrasco Cursach 2014
*
* last modified: 16-03-14
* by: rccursach
*
*/

#ifndef   SENSORS_H
#define   SENSORS_H

#include <Arduino.h>

class Sensors{
  public:
    virtual void begin() = 0;
    virtual String read() = 0;
    virtual int getRequiredPins() = 0;
    virtual int getReqSerialSpeed() = 0;
    virtual ~Sensors() = 0;
};

#endif

If you like to do the same this is what you need:

Sublime text: http://www.sublimetext.com/2
Package Control for Sublime: https://sublime.wbond.net/installation
Arduino IDE http://arduino.cc/en/Main/Software
Install Stino plugin from Package Control.

I don't need to mention how this is useful to keep your code readability, maintainability, and all the other benefits of an object oriented approach in your arduino code.