The "Witty" module is a convenient solution to easily program an ESP8266 with the Arduino IDE. In addition to the mandatory internal LED, there are three other built-in components available on the board: an RGB LED, an LDR, and a micro switch. The entire module consists of a dual pack, which includes a programmer with a CH340G USB-serial chip and the ESP8266 board itself, which is delivered as a stackable unit. The board is especially suitable for prototypes, as it allows you to start right away.
Microcontroller | ESP-8266 32-bit |
---|---|
Clock Frequency | 80MHz or 160MHz |
USB Converter | CH340G |
USB Port | Micro-B |
Operating Voltage | 3.3V |
Flash Memory | 4MB |
Digital I/O Pins | 11 |
Analog Inputs | 1 (A0 / 10-bit ADC) |
Supported Protocols | Serial, SPI, I²C |
WiFi | 802.11 b/g/n |
LDR | Built-in LDR with a dark resistance of 2.5kΩ forms a voltage divider with a 470Ω resistor in series, which is read through the ADC input. |
RGB LED | Built-in RGB LED with common cathode |
Not shown in the image: The micro switch is on GPIO4.
Add the following URL under "File → Preferences → Additional Board Manager URLs":
http://arduino.esp8266.com/stable/package_esp8266com_index.json
.
If another URL is already present, it can be separated with a comma.
Then, you need to add the ESP8266 board. To do so, go to "Tools → Board → Board Manager":
Here, search for "esp8266" and install the board:
Note: On Microsoft Windows, make sure to install the latest CP210x USB to UART Bridge driver for the port to be recognized.
After the installation is complete, you can select the desired board in the "Tools → Board" menu.
In my case, LOLIN(WEMOS) D1 R2 & mini worked well.
Now you can compile your sketches and upload them to the board as usual.
The first program is an extended Blink sketch that first makes the LED blink slowly and then faster. Then, a PWM signal is sent to gradually brighten and dim the LED.
#define PIN_LED 2
void setup() {
pinMode(PIN_LED, OUTPUT);
}
void loop() {
for (byte i = 0; i < 6; i++) {
digitalWrite(PIN_LED, !digitalRead(PIN_LED));
delay(500);
}
for (byte i = 0; i < 16; i++) {
digitalWrite(PIN_LED, !digitalRead(PIN_LED));
delay(50);
}
digitalWrite(PIN_LED, LOW);
for (byte i = 0; i < 255; i++) {
analogWrite(PIN_LED, i);
delay(10);
}
digitalWrite(PIN_LED, HIGH);
for (byte i = 255; i > 0; i--) {
analogWrite(PIN_LED, i);
delay(10);
}
}
Here, the internal blue SMD LED is controlled. It appears that the logic is inverted, as the LED lights up when it is set to LOW and turns off when a HIGH signal is applied.
Since the built-in RGB LED has a common cathode, each color can be turned on with a HIGH signal.
In practice, it has been found that the RGB LED slightly glows if it is not explicitly declared as OUTPUT
or if the output signals are not set to LOW.
The following program rotates the individual color components through all possible intensities using a sine function, creating the full
color spectrum of the RGB LED.
#define PIN_RED 15
#define PIN_GREEN 12
#define PIN_BLUE 13
#define ROTATE_STEP 0.05
#define COLOR_OFFSET 0.05
byte ledPins[3] = {PIN_RED, PIN_GREEN, PIN_BLUE};
float rgbValues[3] = {0.0, 0.0, 0.0};
void setup() {
for (byte i = 0; i < 3; i++) {
pinMode(ledPins[i], OUTPUT);
}
}
void loop() {
for (byte i = 0; i < 3; i++) {
rgbValues[i] += (ROTATE_STEP + i * COLOR_OFFSET);
if (rgbValues[i] > 2 * PI) {
rgbValues[i] = 0.0;
}
analogWrite(ledPins[i], 127 + round(sin(rgbValues[i]) * 127));
}
delay(20);
}
This experiment reads the values of the LDR and turns on the red component of the RGB LED once a certain threshold (here: 100) is crossed.
#define PIN_LDR A0
#define PIN_RED 15
void setup() {
pinMode(PIN_RED, OUTPUT);
}
void loop() {
if (analogRead(PIN_LDR) < 100) {
digitalWrite(PIN_RED, HIGH);
} else {
digitalWrite(PIN_RED, LOW);
}
delay(100);
}
The following sketch checks the micro switch on the Witty board. Depending on whether it is pressed, a blue or red LED is turned on. Again, the internal LED needs to be programmed with inverted logic to achieve the desired behavior.
#define PIN_INT_LED 2
#define PIN_RED_LED 15
#define PIN_BUTTON 4
void setup() {
pinMode(PIN_INT_LED, OUTPUT);
pinMode(PIN_RED_LED, OUTPUT);
}
void loop() {
if (digitalRead(PIN_BUTTON) == HIGH) {
digitalWrite(PIN_INT_LED, LOW);
digitalWrite(PIN_RED_LED, LOW);
} else {
digitalWrite(PIN_INT_LED, HIGH);
digitalWrite(PIN_RED_LED, HIGH);
}
delay(30);
}
Since the ESP8266 board has a WiFi interface, it can make HTTP requests to any web address over a local WiFi network, as shown in this experiment.
Various libraries for the ESP8266 are used here, for example:
ESP8266HTTPClient
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
ESP8266WiFiMulti WiFiMulti;
WiFiClient client;
HTTPClient http;
// WiFi settings:
const char* wifiSSID = "mySSID";
const char* wifiPassword = "myPassword";
const char* hostServer = "http://electro-base.info/";
void setup() {
Serial.begin(9600);
while(!Serial) {}
WiFi.mode(WIFI_STA);
WiFiMulti.addAP(wifiSSID, wifiPassword);
}
void loop() {
if ((WiFiMulti.run() == WL_CONNECTED)) {
Serial.printf("Connecting %s...\n", hostServer);
if (http.begin(client, hostServer)) {
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been sent and server response header has been handled
Serial.printf("GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
Serial.println(payload);
}
} else {
Serial.printf("GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.printf("Unable to connect\n");
}
}
delay(10000);
}