WeMos D1 (esp8266): i2c shield to manage encoder, multiple buttons, and LEDs
For fast prototyping, I will create a set of shields for the WeMos D1 mini; the first is a service board with integrated pcf8574 IC, an encoder, and five other pins to use as input or output.
Naturally, I start my work from the example I created for my library in this article “PCF8574 i2c digital I/O expander: Arduino, esp8266 and esp32, rotary encoder“.
The basic breadboard schema is quite simple, and It’s better explained in the library article.
Here is the video with the code at work.
PCB
You can find the updated version of the PCB on PCBWay for a few dollars.
WeMos D1 mini pcb with pcf8574 and encoder PCBWay
Here is the PCB schema. It’s become quite complex because I’m going to add the switches to change the address of pcf8574 some jumper to activate buttons or use the pins as output.
Start milling to get the prototype
Materials
N° | Object | Descr. |
---|---|---|
1 | DIP SWITCH | package dipswitch-03 |
12 | 10kΩ Resistor | |
4 | Pushbutton | package [THT] |
2 | Ceramic Capacitor | package 100 mil [THT, multilayer]; capacitance 100nF |
6 | Generic male header – 2 pins | package THT; form ♂ (male) |
1 | PCF8574 | package DIP16; type PCF8574 |
1 | Rotary Encoder | package THT; Tipo ALPS ec11e |
1 | WeMos D1 Mini |
You can find WeMos D1 mini on WeMos D1 mini - NodeMCU V2 V2.1 V3 - esp01 - esp01 programmer
You can find rotary encoder on AliExpress
You can find pcf8574 on AliExpress
Assembly video
And now all assembled.
Here the shield used in a project.
And here is the code for putting the shield in working condition; as you can see, the parameters are
esp8266 | pcf8574 |
---|---|
SDA | D2 (PullUp) |
SCL | D1 (PullUp) |
INT | D7 (PullUp) |
and the encoder
pcf8574 | Encoder |
---|---|
P0 | PIN A |
P1 | PIN B |
P2 | BUTTON |
/*
* PCF8574 Shield encoder and buttons
* https://mischianti.org
*
* PCF8574 ----- WeMos
* SDA ----- D2(PullUp)
* SCL ----- D1(PullUp)
* INT ----- D7(PullUp)
*
* PCF8574 ----------------- Encoder
* P0 ----------------- ENCODER PIN A
* P1 ----------------- ENCODER PIN B
* P2 ----------------- ENCODER BUTTON
*
*/
#include "Arduino.h"
#include "PCF8574.h"
int encoderPinA = P0;
int encoderPinB = P1;
#define INTERRUPTED_PIN D7
void ICACHE_RAM_ATTR updateEncoder();
// initialize library
PCF8574 pcf8574(0x38, INTERRUPTED_PIN, updateEncoder);
volatile long encoderValue = 0;
uint8_t encoderButtonVal = HIGH;
void setup()
{
Serial.begin (9600);
delay(500);
// encoder pins
pcf8574.encoder(encoderPinA, encoderPinB);
// encoder button
pcf8574.pinMode(P2, INPUT_PULLUP);
// Start library
pcf8574.begin();
}
bool changed = false;
// The loop function is called in an endless loop
void loop()
{
if (changed){
Serial.print("ENCODER --> ");
Serial.print(encoderValue);
Serial.print(" - BUTTON --> ");
Serial.println(encoderButtonVal?"HIGH":"LOW");
changed = false;
}
}
bool valPrec = LOW;
void updateEncoder(){
changed = pcf8574.readEncoderValue(encoderPinA, encoderPinB, &encoderValue);
// int vale = pcf8574.readEncoderValue(encoderPinA, encoderPinB);
// if (vale!=0){
// changed = true;
// }
// encoderValue = encoderValue + vale;
bool val = pcf8574.digitalRead(P2);
if (val!=valPrec){
changed = true;
valPrec = val;
encoderButtonVal = val;
}
}
Thanks
- WeMos D1 (esp8266): i2c shield to manage encoder, multiple buttons, and LEDs
- WeMos D1 (esp8266): relay shield
- WeMos D1 (esp8266): Ebyte LoRa shield (e32, e22 and e220)