Translate

Saturday, March 2, 2019

Huzzah32 - start to play with

Huzzah32

Adafruit has many nice platforms to start to create embedded devices.
This article would help to start to play with this platform.


One of them is called Huzzah32, member of the Feather family.
The Huzzah32 is based on a dual-core ESP32 chip, 4 MB of SPI Flash, and tuned antenna since it has both WiFi and Bluetooth Classic/LE support plus some GPIO ports, I2C and SPI capabilities.

The core of the Huzzah32 is the WROOM32 processor (click here for the datasheet), a dual core CPU with integrated WiFi and Bluetooth plus GPIO, I2C and SPI as well.
Huzzah32 is an Adafruit board using that processor and there are limitations as well as pre-defined usages of some peripherals.
For example the Analog Input 13 (A13) is connected to the battery and thus can be used only to read the battery value.

The fastest way to work on it is to use Arduino IDE.
Lot of libraries already exists to handle the internal peripherals of this chip as well as many things you can connect to it.
Actually Adafruit has already many boards designed to be directly connected with the Feather family of processors, called Feather Wing.

Pinout

This is the Huzzah32 module pinout for quick reference (from Zeryinth)

Note !

  • PWM is enabled on every digital pin
  • ADC on pins D4, D12, D13, D14, D15,D25, D26 and D27 can be read only if WiFi is not started


Arduino IDE


There are many articles in Adafruit about how to set Arduino IDE for the Huzzah32 however there are some first hand experience on that.

  • It is required to download and install a special driver : SiLabs CP2104
    Do that BEFORE to install or update Arduino IDE. Better to restart the machine after installation.
    The CP2104 is used to interface the ESP32 processor with the development machine USB, thus the driver needs to be installed in the computer running Arduino IDE to allows Arduino IDE to connect with the ESP32 (Huzzah32).
    • For Linux (Debian based distro, like Ubuntu), download the tar, extract in a local directory, then:
      • make ( your cp210x driver )
      • cp cp210x.ko to /lib/modules/<kernel-version>/kernel/drivers/usb/serial
        After that it should be installed
      • insmod /lib/modules/<kernel-version/kernel/drivers/usb/serial/usbserial.ko
      • insmod cp210x.ko
    • For Mac, just download the package and install it
  • Install the latest version of Arduino IDE.
    Always better to start from a fresh installation.  Sometime updating can create problems.
    The ideal would be create a VM and install everything on that.
  • On the Tools menu, be sure to :
    • Select the board : "Adafruit ESP32 Feather"
    • Keep the default connections : 80 Mhz - 921600
    • Be sure to select the correct port - it depends about the system
    • Select the programmer as : Atmel JTAGICE3 (ISP mode)
  • The basic libraries for the Huzzah32 should be already present in the IDE.
    Just install any other needed library for devices connected to the Huzzah32

Monitoring

The Arduino monitor (via serial port) is enabled by default so is possible to see debug messages when the Huzzah32 is connected via USB.
Simply open the serial monitor windows from Arduino IDE to see serial messages.
In the code just use the Serial.print() function to print out log messages.

Peripheral management from Arduino

Bluetooth/BLE

Bluetooth management is already present in the default libraries and from the examples is possible to set up Huzzah32 in server mode, client mode, server UART mode plus other modes.
Probably the most useful and fast to use mode is to set the Huzzah32 in UART server mode.
In this mode is easy to set an application on a phone capable to "see" the Huzzah, conect to it and exchange characters.
The BLE is like a pipe where ASCII characters can be easily exchanged.

Over this is easy to build protocols to exchange data.

From File/Examples/ESP32 BLE Arduino, open the ble_uart sketch and import the BLE functionality in your sketch.
This is a basic example that set up a server with UART type of connection.

In the Setup, where is the BLE initialization, the first line is :

BLEDevice::init("ble_adv_name");

The ble_adv_name is the name showed during the scan on the client device, like a smartphone.

To test the connection is possible to use one of the many apps available out there.
I did found handy to use the BLE Scanner that has serial port (UART) capabilities.

Timer

Is relatively easy to set up a timer.
One way to do that is to use a generic timer interrupt driven function.
This function increment a local counter used mostly as a "flag", to indicate that the timer expired.
The timer interrupt really does nothing if not incrementing the interrupt counter.
Then the loop will increment a real counter every time the value of this interrupt counter is greater than 0.
In this way if the code is busy doing something else, can increment correctly the real counter when it can, without losing interrupts.

Very easily :

  • set up two global variables for the counters
    • volatile int OnTimerInterruptCounter;
      int TotalTimerCounter;    // Will contains the number of seconds from the start
  • create timer interrupt function (before Setup)
    • hw_timer_t * timer = NULL;
      portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;

      void IRAM_ATTR onTimer()
      {
         portENTER_CRITICAL_ISR(&timerMux);
         OnTimerInterruptCounter++;
         portEXIT_CRITICAL_ISR(&timerMux);
      }
  • in the Setup initialize the timer :
    • // Setup timer - interrupt will fire every second
      timer = timerBegin(0, 80, true);
      timerAttachInterrupt(timer, &onTimer, true);
      timerAlarmWrite(timer, 1000000, true);
      timerAlarmEnable(timer);
      TotalTimerCounter = 0;
  • in the Loop handle the counters
    • // Handling timer
      if (OnTimerInterruptCounter > 0)
      {
          // Timer expired - reset it - restart
          portENTER_CRITICAL(&timerMux);
          OnTimerInterruptCounter--;
          portEXIT_CRITICAL(&timerMux);

          TotalTimerCounter++;    // Increment number of seconds
      }
We end up having a 1 sec counter.

Measuring the battery


The Huzzah32 has a nice embedded LiPo charger.
When the Huzzah is connected to the USB port, the battery is charged.
But when there is no USB connection the battery is used by the Huzzah.
There is a nice way to monitor the voltage of the battery to determine if the system is dying.
The ADC connected on the pin A13 is connected to the battery.
So to measure the battery voltage, simply put in the code, where you need to know the battery level :

float BatteryLevel;
BatteryLevel = analogRead(A13) * 2/1000.00;

The BatteryLevel variable will indicate a value from 3.8 to 5.01 volts.
5.01 when USB is connected and the battery is charged.
Below 3.8 the battery is considered totally drained, very probably will be impossible to read 3.7, the system will shutdown before that.



1 comment:

  1. I'm Helga from Eltima Software.

    I came across hanixdiy.blogspot.com and this website really interested me since our company develops a number of solutions in the embedded system developments field. We would like to support your own projects and offer free license copy for any software of your choice that we developed. Please, let me know if you are interested.

    helga@eltima [DOT] com

    ReplyDelete