A Student designed ESP8266 Dev Board


This is a student-designed ESP8266 Dev board. This project came along after I challenged some final-year students at our local high school to try to design their own PCBs. They had recently assisted me in a collaboration with the Mushroom House Controller project, and while we were talking about electronics, the idea of a challenge came up, to stimulate some interaction.

One of them was extremely confident that he could do it, providing that I gave him a schematic. I complied, and he spent the next week hacking away on the EDA software. When i saw him again, he very proudly handed me a USB flash drive, containing the EDA design, but no schematic! Oh well, let us take a chance and see what happens… I mean, how bad could it be…

I took a good look at the design, making sure that it was at least electrically sound, with all the connections made to the right components, in the right way.

That part all passed, but, as we can clearly see, the layout could be very much improved. I decided to NOT change anything on the PCB, to keep the work original.

Manufacturing the PCB

The PCB, together with a stencil, arrived from PCBWay on about the 15th of this month.

I choose PCBWay for my PCB manufacturing. Why? What makes them different from the rest?

PCBWay‘s business goal is to be the most professional PCB manufacturer for prototyping and low-volume production work in the world. With more than a decade in the business, they are committed to meeting the needs of their customers from different industries in terms of quality, delivery, cost-effectiveness and any other demanding requests. As one of the most experienced PCB manufacturers and SMT Assemblers in China, they pride themselves to be our (the Makers) best business partners, as well as good friends in every aspect of our PCB manufacturing needs. They strive to make our R&D work easy and hassle-free.

How do they do that?

PCBWay is NOT a broker. That means that they do all manufacturing and assembly themselves, cutting out all the middlemen, and saving us money.

PCBWay’s online quoting system gives a very detailed and accurate picture of all costs upfront, including components and assembly costs. This saves a lot of time and hassle.

PCBWay gives you one-on-one customer support, that answers you in 5 minutes ( from the Website chat ), or by email within a few hours ( from your personal account manager). Issues are really resolved very quickly, not that there are many anyway, but, as we are all human, it is nice to know that when a gremlin rears its head, you have someone to talk to who will do his/her best to resolve your issue as soon as possible.

Find out more here

Assembly

The PCB was assembled with help of a stencil.

Technically, this PCB does not actually need it, as the components are large enough to manually place solder paste. This being a student project, I did however choose to get the stencil to try and assure the best possible chance of success..

Placing components only took a few minutes, after which the PCB was reflowed with hot air – no need for a hotplate here!

Through-hole components took only another few minutes to solder into place, and then testing could commence.

My thoughts – a very “gentle” critique

The board could be smaller, but due to the fact that this is a “very first PCB ever” and also a first SMD PCB at that, I can understand that it may still be quite difficult to understand how small the components really are, as well as lay them out properly.

Components are mostly all over the board, without a clear “group by function” kind of mindset. This once again comes back to experience.

GPIO pins are not notated. This will really make the board difficult to use.
The power supply input is in the center of the PCB. This is definitely not ideal.

In general, track sizes could have been bigger, especially on the Power and Ground lines. No ground plane was poured on either layer.

So does it work?
Yes, surprisingly it does. We shall see more of it in the near future, when I task the creator with using it to perform some task. That way, he can experience first hand the difficulties of the design, and also learn practically why certain things need to be improved.

A Simple IoT Plant Watering Solution

This is a Simple IoT Plant Watering Solution, done as another collaboration with the local High School in my Area. In this project, they took a group of purely academic students ( Language majors ) and told them that they had to design an electronics project that would have use in the real world…

This presented a very tough situation to the students since they had never even imagined that they could do something like this… (Think along the lines of a Senior Design project, like what you would give EE Students during their final year – with all the documentation, pamphlets, and explanations – i.e. lots and lots of paperwork) , and then also add on the requirement that they had to present a practical project as well! And they have only 45 days to do that as well!

I came into this picture late on a Thursday or Friday afternoon, with a group of students milling around outside the Electronics lab. They were unknown to me and seemed quite flustered… I invited them in, and eventually, they started opening up about their problem…

As it turned out, they were completely clueless, and did not know where to start with anything, not even what they wanted to do! Their initial idea went like “something that uses a camera to sort garbage by type and material” – that was never going to happen, not in 45 days, and not with the allocated budget of no more than $USD30.00 they were allowed. Lets not even go to the machine learning stuff, training of the models etc…

So, I took over, and decided that we shall do a simple IoT Plant Watering Solution. It is complex enough for Grade 11 students, and more importantly, I knew that I could teach them enough Arduino coding and basic electronics skills in the time allotted to get the project completed successfully.

What followed was a few very intensive sessions after school to get the paperwork sorted. For some reason, their teacher required ALL paperwork be completed upfront, with the entire design and code prepared before they touched the practical stuff – A funny way to design stuff if you ask me, but that is how we did it…

We settled on the ESP8266 12-E NodeMCU v3 development board, A resistive soil sensor, a DHT11 for temperature and humidity data, a small OLED I2C display and a small 5v USB-powered water pump. While they ordered the components, we started with basic coding classes, and this is where the story changes.

These kids blew me away with their level of interest, their attitude to learning, and how quickly they grasped the concepts. In no time at all, they were coding basic sketches, taking readings, learning how the different sensors worked, and pushing the envelope by adding lots more complementary components like MOSFETs and BJT transistors.


With this level of enthusiasm, I could not help but also become very excited, and thus decided to go a bit further than usual and help them design a neat PCB baseboard for the NodeMCU. That way, we could get rid of all those pesky wire connections, and maybe even produce something that looked good.


This is what we came up with, here shown just after SMD component placing. They ( the students ) did all of that by themselves as well, and it turned out to be a lot of fun for them, with some funny moments for me as well.

The Assembly ( First time ever )

The PCB, together with a stencil, arrived from PCBWay on about the 15th of this month.

I choose PCBWay for my PCB manufacturing. Why? What makes them different from the rest?

PCBWay‘s business goal is to be the most professional PCB manufacturer for prototyping and low-volume production work in the world. With more than a decade in the business, they are committed to meeting the needs of their customers from different industries in terms of quality, delivery, cost-effectiveness and any other demanding requests. As one of the most experienced PCB manufacturers and SMT Assemblers in China, they pride themselves to be our (the Makers) best business partners, as well as good friends in every aspect of our PCB manufacturing needs. They strive to make our R&D work easy and hassle-free.

How do they do that?

PCBWay is NOT a broker. That means that they do all manufacturing and assembly themselves, cutting out all the middlemen, and saving us money.

PCBWay’s online quoting system gives a very detailed and accurate picture of all costs upfront, including components and assembly costs. This saves a lot of time and hassle.

PCBWay gives you one-on-one customer support, that answers you in 5 minutes ( from the Website chat ), or by email within a few hours ( from your personal account manager). Issues are really resolved very quickly, not that there are many anyway, but, as we are all human, it is nice to know that when a gremlin rears its head, you have someone to talk to who will do his/her best to resolve your issue as soon as possible.

Find out more here

This caused quite a lot of excitement from the students since they asked me to get to school immediately, and offered to stay after school to get the assembly done. So, I packed up all the components required, got in the car, and a few minutes later, we had an assembly line running 🙂


It took them only a few minutes to understand how to read the BOM file, use that information to find the right components and take turns to place a few components at a time onto the PCB. ( I did the solder paste and stencil thing on my own, as that could potentially turn out to be a very messy and wasteful operation if I let them do it )

During this assembly, they were quite amazed at what they were doing, but also quite confused as to how we would ” make the components stick to the PCB later”. They have seen soldering on YouTube, and could not understand how this “grey paste” could “turn hard and shiny” – with some even trying to rush their friends, as the paste would dry up and become sticky…

That signalled to me that we should get the hot air ready, and show them properly, as it would stop the speculation, and give them closure on their questions, because no matter how much I tried to explain that the solder paste would be melted later, they could not understand that concept.


Once I reflowed the first few components using hot air, they all once again took turns with a small group of components on the PCB ( We assembled 2 PCB’s to make sure everybody got a chance to try everything)


Through-hole soldering of the various header pins and other components was next. Once again, I showed them an example and then stepped back while they took over and completed the task. At this time it was about 17:30 already, so I sent them all home, with a promise to return the next day and complete the rest of the build.

Final Assembly and the Enclosure


We added some copper standoffs to the bottom of the enclosure, drilled some holes and mounted everything with screws and hot glue. The some final testing was performed, and the project was placed outside, next to a potted plant, and put on a sort of soak test, to verify operation for a few days.

A discussion of the circuit

In this circuit, I made use of a very cheap resistive soil moisture probe sensor. It consists of an etched PCB probe and a separate OPAMP on an additional PCB. This assembly then sends an analog voltage back to the Microprocessor for analysis. Very easy to use, and as mentioned, extremely cheap! But this is also a big problem. As seen in many other posts online, people don’t like these probes, as they don’t seem to last very long, and/or become unreliable over time.

I believe that this is due to the fact that they run them continuously, and that, causes the electrodes to erode away over time – because the thing about it, you are basically running an electrolysis cell – sending voltage/current through a probe, that is suspended in a conductive medium ( the water in the soil contains minerals etc – that is why it is conductive, and why we can get a reading from it )

How do I plan to avoid this problem then? The probe is powered through a small N-Channel MOSFET. This allows us to power the probe on when we want a reading, and then power it off again. It will definitely not completely stop the electrodes eroding away over time, but I am sure it will extend the usable lifetime of the probe by quite a bit. On the downside, you need another GPIO pin to control that MOSFET, but that in itself is also a great learning opportunity

Code

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32


#define DHTTYPE    DHT11     // DHT 11
#define WaterRelay 2
#define A0Enable 14
#define DHTEnable 13
#define DHTPIN 12
#define WATER_DELAY 10000
#define WATER_LOW 20.00
#define SOIL_DELAY 5000

uint32_t delayMS;
float temperature = 0.00;
float humidity = 0.00;
float soilValue = 0.00;
float soilAverage = 0.00;
unsigned long previousMillis = 0;
unsigned long previousWater = 0;
unsigned long previousSoil = 0;
bool MAY_WATER = 0;
bool RUN_WATER = 0;
bool DELAY_WATER = 0;
sensor_t sensor;
const String WaterOn = "Waterpump ON";
const String WaterOff = "Waterpump OFF";
String WaterMessage = "";

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
DHT_Unified dht(DHTPIN, DHTTYPE);



void setup() {
  
  pinMode(WaterRelay,OUTPUT);
  pinMode(A0Enable,OUTPUT);
  pinMode(DHTEnable,OUTPUT);
  digitalWrite(WaterRelay,HIGH);
  //Serial.begin(115200);
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    //Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
  display.ssd1306_command(SSD1306_DISPLAYON);
  display.clearDisplay();
  display.display();
  delay(20);
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(10,10);
  display.print("Booting...");
  display.display();
  delay(2000);
  
  dht.begin();
  
  show_DHT_Params();
  
}

void loop() {
  
  
  

  //Soil Monitor Loop
  unsigned long SoilMillis = millis();
  if ( SoilMillis - previousSoil >= SOIL_DELAY) {
    previousSoil = SoilMillis;
    take_soil_reading();
  }
  //End Soil Monitor Loop

  //Water Loop
  unsigned long WaterMillis = millis();
  if (WaterMillis - previousWater >= WATER_DELAY) {
    previousWater = WaterMillis;
    if ((digitalRead(WaterRelay) == LOW) && (RUN_WATER == 1) && ( DELAY_WATER == 0)) {
      digitalWrite(WaterRelay,HIGH);
      DELAY_WATER = 1;
    } else if (DELAY_WATER == 1) {
      DELAY_WATER = 0;
      if (RUN_WATER == 1) RUN_WATER = 0;
    }
  }
  // End Water Loop
  // Main One Second Delay Loop
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= delayMS) {
    previousMillis = currentMillis;
    get_DHT_Readings();
    show_Soil_Data();
    display.display();
  }
  // End Main 1 Second Loop
  if ((soilAverage < WATER_LOW) && (RUN_WATER == 0)) {
    RUN_WATER = 1;
    MAY_WATER = 1;
  } else if (soilAverage >= WATER_LOW) {
    RUN_WATER = 0;
    MAY_WATER = 0; 
  }

  if ((digitalRead(WaterRelay) == HIGH) && (RUN_WATER == 1) && DELAY_WATER == 0) {
      digitalWrite(WaterRelay,LOW);
      WaterMessage = WaterOn;
      take_soil_reading();
  }
  if ((DELAY_WATER == 1) && (MAY_WATER == 1)) {
      digitalWrite(WaterRelay,HIGH);
      WaterMessage = WaterOff;
  }
  
}

void take_soil_reading() {
  digitalWrite(A0Enable,HIGH);
  delay(50);
  
  float tempSoil = 0.00;
  for (int i = 0; i <10 ; i++) {
    soilValue = (1024.00 - analogRead(A0));
    tempSoil = tempSoil + soilValue;
    delay(2);
  }
  soilAverage = map(tempSoil/10,0.00,1023.00,0.00,100.00);
  delay(30);
  digitalWrite(A0Enable,LOW);
}

void show_DHT_Params() {
  digitalWrite(DHTEnable,HIGH);
  
  dht.temperature().getSensor(&sensor);
  display.clearDisplay();
  display.display();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(10,1);
  display.print("Temperature Sensor");
  display.setCursor(10,10);
  display.print(F("Type: "));
  display.print(sensor.version);
  display.setCursor(10,20);
  display.print(F("Min: "));
  display.print(sensor.min_value);
  display.print(F(" Deg C"));
  display.setCursor(10,30);
  display.print(F("Max: "));
  display.print(sensor.max_value);
  display.print(F(" Deg C"));
  display.setCursor(10,40);
  display.print(F("Resolution: +/- "));
  display.setCursor(10,50);
  display.print(sensor.resolution);
  display.print(F(" Deg C"));
  display.display();
  delay(4000);
  dht.humidity().getSensor(&sensor);
  display.clearDisplay();
  display.display();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(10,1);
  display.print("Humidity Sensor");
  display.setCursor(10,10);
  display.print(F("Type: "));
  display.print(sensor.version);
  display.setCursor(10,20);
  display.print(F("Min: "));
  display.print(sensor.min_value);
  display.print(F(" % RH"));
  display.setCursor(10,30);
  display.print(F("Max: "));
  display.print(sensor.max_value);
  display.print(F(" % RH"));
  display.setCursor(10,40);
  display.print(F("Resolution: +/- "));
   display.setCursor(10,50);
  display.print(sensor.resolution);
  display.print(F(" % RH"));
  display.display();
  delay(4000);
  
  delayMS = sensor.min_delay / 1000;
}

void get_DHT_Readings() {
    
    display.clearDisplay();
    display.setTextSize(2);
    display.setTextColor(SSD1306_WHITE);
    sensors_event_t event;
    dht.temperature().getEvent(&event);
    if (isnan(event.temperature)) {
      #ifdef DEBUG-SERIAL
      Serial.println(F("Error reading temperature!"));
      #endif
    }
    else {
      display.setCursor(10,1);
     
      display.setCursor(10,1);
      display.print(event.temperature);
      display.setCursor(80,1);
      display.println(F(" C"));
      temperature = event.temperature;
    }
    // Get humidity event and print its value.
    dht.humidity().getEvent(&event);
    if (isnan(event.relative_humidity)) {
     display.clearDisplay();
    display.setTextSize(2);
    display.setTextColor(SSD1306_WHITE);
    sensors_event_t event;
    dht.temperature().getEvent(&event);
    if (isnan(event.temperature)) {
      #ifdef DEBUG-SERIAL
      Serial.println(F("Error reading temperature!"));
      #endif
    }
    else {
      
      display.setCursor(10,1);
      display.print(event.temperature);
      display.setCursor(80,1);
      display.println(F(" C"));
      temperature = event.temperature;
    }
    // Get humidity event and print its value.
    dht.humidity().getEvent(&event);
    if (isnan(event.relative_humidity)) {
      #ifdef DEBUG-SERIAL
      Serial.println(F("Error reading humidity!"));
      #endif
    }
    else {
      display.setCursor(10,20);
      display.print(event.relative_humidity);
      display.setCursor(80,20);
      display.println(F(" %"));
      humidity = event.relative_humidity;
    }
    }
    else {
      display.setCursor(10,20);
      display.print(event.relative_humidity);
      display.setCursor(80,20);
      display.println(F(" %RH"));
      humidity = event.relative_humidity;
    }
    
}

void show_Soil_Data() {
  display.setTextSize(2);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(10,39);
  display.print(soilAverage);
  display.setCursor(80,39);
  display.println(F(" %"));
  display.setTextSize(1);
  display.setCursor(10,56);
  display.println(WaterMessage);
}

Conclusion

This project was extremely interesting. It was something very basic, but it gave me a very unique opportunity to teach a group of kids something they never knew before. It also initiated a spark in many of them, who are now interested in getting involved in electronics as a hobby. The possibilities of this project, if it is improved a bit more, could also be great. For example, we did not even consider adding IoT connectivity to this yet, we all decided that it was not needed, and an unnecessary complication of an already complex issue ( to the students that is). I shall keep monitoring the operation of the device over the next few weeks, and hopefully get some answers myself as to how long that soil probe will last… As a control, we have another one that was left powered on in the same container. That one are not being used to take readings, but will definitely provide us with a good comparison against the probe that is only on then used…

Multi-Purpose IO Card

When we are working on a prototype, we always need access to pushbuttons, encoders and even displays to test our ideas in the real world. This Multi-Purpose IO Card was designed to help me do just that…

What is on the PCB?

This PCB was designed with my particular work style in mind. I use a lot of I2C devices, IO Expanders, Displays and sensors. It would thus make sense to have I2C on the PCB, to control an OLED display, as well as a PCF8574 IO expander, that is used to drive a 4×4 Matrix Keypad. Two Rotary encoders, as well as another 4 standard push buttons completes the PCB…

The features, summarised is as follows:

1x Matrix Keypad (4×4) Controlled via an PCF8574 IO expander with selectable addressing.
1x SSD1306 OLED I2C Display
4x Momentary pushbuttons, configured to be used with internal pullups – i.e pushing the button pulls the GPIO LOW
2x Rotary Encoders, with integrated Pushbutton, also configured as Active LOW

The board has all of the connectors and jumpers on the back, making it possible to mount it to an enclosure as a control panel.

I have also provided an additional I2C header to make it possible to add additional devices to the I2C bus easily

The PCB in Detail

PCB Top

Starting from left to right, we have two push-buttons, an OLED display, with two rotary encoders below the display, and another two momentary push buttons. On the Right, we have a 4×4 matrix keypad, and various pin headers for connection to a microcontroller of your choice.

On the back, we have the PCF8574 IO expander for the Matric keypad, addressing Jumpers for the IO expander, as well as the two pin headers for connections to and from a microcontroller…

The Pinouts of these are as follows:
Horizontal 15 pin 2.54mm connector
SDA I2C Data
SCA I2C Clock

GND

SW4 Momentary Push Button 4
SW3 Momentary Push Button 3
SW2 Momentary Push Button 2
SW1 Momentary Push Button 1

RE2-D Rotary Encoder 2 Push Button
RE2-B Rotary Encoder 2 Pin B
RE2-A Rotary Encoder 2 Pin A

RE1-D Rotary Encoder 1 Push Button
RE1-B Rotary Encoder 1 Pin B
RE1-A Rotary Encoder 1 Pin A

GND
VCC 3.3v to 5v DC

The Expansion header extends the I2C Bus, as well as proved access to the interrupt pin on the PCF8574. VCC and GND are also provided.

The Schematic


Manufacturing the PCB

I choose PCBWay for my PCB manufacturing. Why? What makes them different from the rest?

PCBWay‘s business goal is to be the most professional PCB manufacturer for prototyping and low-volume production work in the world. With more than a decade in the business, they are committed to meeting the needs of their customers from different industries in terms of quality, delivery, cost-effectiveness and any other demanding requests. As one of the most experienced PCB manufacturers and SMT Assemblers in China, they pride themselves to be our (the Makers) best business partners, as well as good friends in every aspect of our PCB manufacturing needs. They strive to make our R&D work easy and hassle-free.

How do they do that?

PCBWay is NOT a broker. That means that they do all manufacturing and assembly themselves, cutting out all the middlemen, and saving us money.

PCBWay’s online quoting system gives a very detailed and accurate picture of all costs upfront, including components and assembly costs. This saves a lot of time and hassle.

PCBWay gives you one-on-one customer support, that answers you in 5 minutes ( from the Website chat ), or by email within a few hours ( from your personal account manager). Issues are really resolved very quickly, not that there are many anyway, but, as we are all human, it is nice to know that when a gremlin rears its head, you have someone to talk to that will do his/her best to resolve your issue as soon as possible.

Find out more here

Assembly and Testing

The assembly of this PCB was relatively easy, as it contains only a single SMD component. I do however have to alert you to a certain caveat…

On the PCB, the I2C OLED display pinout is, from left to right,

VCC GND SDC SDA

I have however come across similar displays that swap the GND and VCC pins… and some that even have SCL and SDA swapped…

It is thus quite important that you check your display BEFORE soldering it to this PCB…

Addressing the PCF8574 is also quite easy, with the jumper towards the top is a high, and towards the bottom is a low… They are marked A2 A1 A0 and thus, counting in binary, all low will be 0x20h and all high will be ox27h

Also, note that there are NO I2C Pullup resistors on the board. My microcontroller PCB’s usually have these already, and most I2C Sensors, including the OLED Display that we use, already include as well…
You should thus check what you have on your own hardware, as it is quite impossible to cater for every situation… In a future version, I may add selectable pullup resistors onto this board as well…

Coding and Firmware

The possible uses of this board is quite broad, and the code possibilities are thus also quite extensive. Since I mainly use ESPHome or the Arduino IDE with most of my projects, I wont be including any specialised code here. I think it is enough to say that almost all of the available PCF8574 Matrix Keypad libraries available for the Arduino IDE will fork with this board…

The pinouts are important, and thus :

Row 0 – P0
Row 1 – P1
Row 2 – P2
Row 4 – P3

Col 0 – P7
Col 1 – P6
Col 2 – P5
Col 3 – P4

As far as ESPHome goes, you will need to
1) Add an I2c bus for your device
2)Add a PCF8574 component
3)Add a Matrix Keypad component, and refer the rows and columns to the pins on the PCF8574 – See below for an example of how I have done that in a previous project.

#I2C bus

i2c:

sda: 4

scl: 5

scan: true

id: I2C_Bus

#
# In my case, SDA is on GPIO4 and SCL is on GPIO5
# This is similar to the standard configuration on a NodeMCU v2 Dev board
#

#
# The next step is to configure the actual IO Expander, which in my case is located 
# at address 0x27
#

#PCF8574

pcf8574:

- id: 'pcf8574_hub'

address: 0x27

pcf8575: false


#
# Now we can add the actual keypad interface to the YAML file
# Take note of the difference from the ESP32 file above.
#
#

#KEYPAD

matrix_keypad:

id: mykeypad

rows:

- pin:

pcf8574: pcf8574_hub

# Use pin number 0

number: 0

# In the ESP32 file, we wHereould specify a pin directly like:
#
# -pin: 17
#
# That approach will not work for us.
# The reason for that is that we have to redirect the GPIO to a 
# physical pin on the PCF8574 IO expander.
#
# That is done with the following syntax
#
# - pin:
#pcf8574: pcf8574_hub -- This is the ID of the PCF8574 device -
#number: 0 -- The actual pin number

- pin:

pcf8574: pcf8574_hub

# Use pin number 0

number: 1

- pin:

pcf8574: pcf8574_hub

# Use pin number 0

number: 2

- pin:

pcf8574: pcf8574_hub

# Use pin number 0

number: 3

columns:

- pin:

pcf8574: pcf8574_hub

# Use pin number 0

number: 7

- pin:

pcf8574: pcf8574_hub

# Use pin number 0

number: 6

- pin:

pcf8574: pcf8574_hub

# Use pin number 0

number: 5

- pin:

pcf8574: pcf8574_hub

# Use pin number 0

number: 4

keys: "123A456B789C*0#D"

has_diodes: false

The Rotary encoders and momentary push-buttons can be handled in the same manner, using standard libraries in the Arduino IDE, or a rotary encoder component in ESPHome…

The OLED display would also be handled as above, with a DISPLAY component in ESPHome…

Summary and next steps

The next steps, for me at least, would be to design and CNC cut a suitable enclosure for the IO panel/Control panel in order to make it easier to use…

The panel was designed to be a tool to aid me while designing, and part of my never-ending battle getting rid of breadboards.

It does its job well, at least so far, and works as I have intended it to.

Simple Smart Light Controller

Adding a bit of automation to a certain area of the house can definitely help with saving energy. With this Simple Smart Light Controller, I aimed to do just that… Let me give you a tiny bit of context… Houses in SE Asia are built to some “questionable” standards and designs, and electrical installations are usually even more suspect… Our house is no exception. Being a rental, I do not want to go and make changes unless things are outright dangerous… Kitchens are usually a mixture of inside/outside areas, and this is where my device fits in…

The light in the outside kitchen consists of a simple bulb that the owner has routed into the house via an electrical flex cord, at least that was standard… But, due to laziness or just whatever, that cord was never terminated into a proper switch… He just added a plug. This is thus my opportunity to make life a bit easier for myself in that area. I could have opted for a standard switch, but then, automating this can take care of another problem… We constantly forget to switch that light off, as the plug is in a “strange place” that is not usually associated with the kitchen lights…


What I have thus come up with is a simple ESP8266-based solution with a single relay ( optically isolated from the board), as well as a few additional GPIO pins, just in case I want to hang some additional sensors onto this in the future.

The device should also be powered directly from the mains, as adding another external AC to DC adapter would definitely NOT do at all!

What is on the PCB


Lets look at the empty PCB, in order to understand better what is where on the board.

Starting on the Left Side, at the bottom corner, we have our mains voltage input, 220V or 110V, depending on where you live. That goes directly into U1, which is a AC-to-DC converter, providing 3.3v at 1A to the board. Note that I did not place a fuse directly on the board. I prefer to have an inline fuse on the line, which is also accessible from the enclosure.

A series of cutouts on the PCB provides additional mains isolation and also prevents mains voltage tracking towards other tracks in the event of a fault.
The Mains area also does not have a copper pour.

In the top left corner, towards the center, is a WAKE jumper. This is connected to GPIO16 and can be used to wake the ESP8266 from “deep sleep” if configured in the firmware.

Relay K1, and its screw terminal connector is in the bottom center of the board, with the relay contacts clearly labelled.

On the right of the PCB, we have the programming header, complete with Auto Flash and Reset circuitry, as well as manual Flash and Reset Buttons below that.

A 3×3 header connector follows, with access to 3.3v, Ground as well as 3 additional GPIO pins for other applications.

Finally, we have the relay control switch, with a few options to connect external switches, either on the 2.54mm header, or via wires soldered to the pads marked SW-A and SW-B


The populated PCB will thus make more sense if we look at the picture above now since we had a detailed look at it above…


The Schematic is made available at the link above.

Configuration and Software

This build was designed with ESPHome in mind, so we will focus on that there.
You can however very easily use standard Arduino/ESP8266 code to control this as well…

The YAML configuration for the device will be as follows: (note that this is quite simplified, as I am still fine-tuning the actual features that I require)

esphome:
  name: smart-switch-01
  friendly_name: SMART-SWITCH-01

esp8266:
  board: nodemcuv2
  restore_from_flash: true

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "hfYNn8KSbVq26rGkPOJo4yLj/d/WY7Hk0H3TmxlWZAU="

ota:
  password: "85ed2a8afcd61d0f4c65db7b92bdacc5"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Smart-Switch-01 Fallback Hotspot"
    password: "XovAx4n1H1qT"

captive_portal:

text_sensor:
  - platform: wifi_info
    ip_address:
      name: IP Address
    ssid:
      name: SSID
    bssid:
      name: BSSID
    mac_address:
      name: Wifi MAC
    scan_results:
      name: WiFi Scan Results


sensor:
  - platform: adc
    pin: VCC
    name: "ESP8266 Chip Voltage"
    id: mcu_voltage
    unit_of_measurement: "V"
    device_class: "voltage"
    accuracy_decimals: 2
    update_interval: 60s
    entity_category: "diagnostic"
    
  - platform: wifi_signal
    name: "WiFi Signal Sensor"
    id: wifi_strength
    device_class: "signal_strength"
    unit_of_measurement: "dBm"
    update_interval: 240s
    entity_category: "diagnostic"

  - platform: copy # Reports the WiFi signal strength in %
    source_id: wifi_strength
    name: "WiFi Signal Strength"
    filters:
      - lambda: return min(max(2 * (x + 100.0), 0.0), 100.0);
    unit_of_measurement: "%"
    entity_category: "diagnostic"


light:
 # - platform: status_led
 #   pin: GPIO13
 #   id: status_indicator
 #   name: "ID Light"
    
  - platform: binary
    name: "Kitchen Outside Light"
    output: relay_01
    id: kitchen_light
    on_turn_on:
    - light.turn_on:
        id: slow_light
        effect: "Slow Pulse"
    

    on_turn_off:
    - light.turn_off: slow_light
    

  - platform: monochromatic
    id: slow_light
    output: light_status
    restore_mode: RESTORE_AND_OFF
    effects:
      - pulse:
          name: "Slow Pulse"
          # transition_length: 1s      # defaults to 1s
          update_interval: 2s

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO5
      mode:
        input: true
        pullup: true
    id: kitchen_light_toggle
    filters:
      - delayed_on: 200ms
      - delayed_off: 200ms
    on_press:
      then:
        - light.toggle: kitchen_light
  - platform: status
    name: "Kitchen Light Controller"
     
switch:
  - platform: restart
    name: "Restart Device"

# Relay output
output:
  - platform: gpio
    id: relay_01
    pin: GPIO4
    inverted: true
  - platform: esp8266_pwm
    id: light_status
    pin: GPIO12
 

Manufacturing the PCB

I choose PCBWay for my PCB manufacturing. Why? What makes them different from the rest?

PCBWay‘s business goal is to be the most professional PCB manufacturer for prototyping and low-volume production work in the world. With more than a decade in the business, they are committed to meeting the needs of their customers from different industries in terms of quality, delivery, cost-effectiveness and any other demanding requests. As one of the most experienced PCB manufacturers and SMT Assemblers in China, they pride themselves to be our (the Makers) best business partners, as well as good friends in every aspect of our PCB manufacturing needs. They strive to make our R&D work easy and hassle-free.

How do they do that?

PCBWay is NOT a broker. That means that they do all manufacturing and assembly themselves, cutting out all the middlemen, and saving us money.

PCBWay’s online quoting system gives a very detailed and accurate picture of all costs upfront, including components and assembly costs. This saves a lot of time and hassle.

PCBWay gives you one-on-one customer support, that answers you in 5 minutes ( from the Website chat ), or by email within a few hours ( from your personal account manager). Issues are really resolved very quickly, not that there are many anyway, but, as we are all human, it is nice to know that when a gremlin rears its head, you have someone to talk to that will do his/her best to resolve your issue as soon as possible.

Find out more here

Assembly and Testing

This device does not need a stencil for assembly, but using one will definitely speed up things. I chose to do this build all by hand, from applying solder-paste, up to placing components.

Soldering was done on a hotplate, as usual, to reflow everything at the same time. TH components were then placed and hand-soldered.

Uploading the initial firmware, after adding the device to ESPHome was done with an external USB-to-UART converter. All further firmware changes were made via OTA.

Enclosure and some of the wiring

It is important to mention here that this PCB is powered by mains voltage. I chose to use an inline fuse, BEFORE the connector on the PCB. It is also notable that the relay common is connected to the live wire, BEFORE the fuse, as the lightbulb acts as its own fuse – it blows when a fault occurs.

The Lightbulb neutral will be connected to the circuit breaker, together with the device live and neutral.

This way, the fuse only acts on the actual device, and I can use a lower-rating fuse, since I do not have to accommodate the current from the lightbulb as well…

Summary

The device works as planned, with no problems…
Below is some pictures from it in Home Assistant

LED COB Controller

COB LED lights, or Chip-on-Board LED lights, are a type of LED light that has all of the LED chips mounted on a single substrate. This makes them more efficient than traditional LED lights, which have individual LED chips mounted on a circuit board. COB LED lights also produce a more uniform light output, and they can be used in a wider variety of applications.

Here are some of the benefits of using LED COB lights:

  • are very efficient, and they can produce up to 100 lumens per watt. This means that they use less energy than traditional light bulbs, and they can save you money on your energy bills.
  • produce a more uniform light output than traditional LED lights. This means that they do not create hot spots or shadows, and they provide a more comfortable light to work under.
  • are very durable, and they can last for many years. They are not as susceptible to damage from heat or vibration as traditional light bulbs, and they can withstand harsh conditions.
  • can be used in a wide variety of applications, including indoor and outdoor lighting, commercial and residential lighting, and automotive lighting.

They can however be quite a pain to power in a traditional 110v/220v AC wired house or workshop, as some of them actually required DC current to work.
I have decided to use a pair of them to provide additional, dimmable light at my electronics workbench, a place where extra light is sometimes a very necessary commodity. The ability to dim the lights will definitely aid in many scenarios as well.

Variable voltage Power Module
The Variable voltage Power module was designed specifically for this project


The project consists of two main parts, the first being the Variable Voltage Power Module, which I published a few days ago. The particular COB lights that I will be using, were scavenged from a battery-operated emergency light panel, which had some problems which were not economical to repair. The light modules themselves, however, looked good and were perfectly working as well.
The only issue was that they were 6v DC. So using straight 12v there was out of the question. 6V being an odd voltage in my lab, I designed the module above specifically to provide that.

LED COB Controller stacked on top of Variable Voltage Poser Module
LED COB Controller stacked on top of Variable Voltage Poser Module


The second part of the project consists of a simple Custom ESP-12E PCB. Why ESP12-E? Well, I have a lot of them lying in stock, and since I won’t need any advanced features, commonly found on the bigger ESP32s, I decided to design around something that I have in stock, rather than overcomplicate the design with a bigger more advanced chip. The project features a rotary encoder, to adjust light intensity, as well as push buttons to toggle the lights on or off…

All of this is of course controlled with ESPHome. This choice gives me the option of manual or fully automatic control from HomeAssistant. It also saves me a lot of coding, as everything usually just works.

What is on the PCB?

We shall focus mainly on LED COB Controller PCB.
In order to understand everything, please refer to the picture below:

LED COB controller without Power Module
LED COB controller without Power Module

The board consists of a few sections, which can be divided as follows:

ESP-12E (8266) supporting circuitry

The top area is mainly the supporting circuitry for the ESP-12E, which includes a 6-pin header to flash firmware, the classic ESP32/8266 Auto Flash/Reset circuit, and manual Flash and Reset switches. Note that the board DOES NOT contain any USB-to-Serial circuitry. I usually use those only once or twice, and update firmware OTA after that. Using an external USB-to-Serial adapter is thus sufficient for my purposes.

Power and LED Control circuitry

Power enters the board in the center, using header pins mounted on the bottom of the PCB. From left to right, these are 3.3v and then two variable voltage inputs. These all originate from the Variable voltage Power Module, mounted below the main PCB. The LED Control circuitry consists of two P-Channel Mosfets, the configuration of which was previously tested in another project, the “P-MOS MOSFET Driver Board“, also published a few weeks ago. The SI2301 P-Channel Logic Level MOSFET, used here is capable of switching up to 2.3A at 20v, and thus more than capable of handling the 300mA that the LED COB modules require.

Four cutouts are provided to access test points on the power module below, as well as the potentiometers used to set the voltage that will ultimately be sent to the LED COB Modules.

Wide copper traces connect the Mosfet’s to Screw Terminals for the LED Modules.

The Final part of the PCB is dedicated to control interfaces. A single 6-way screw terminal is provided at the bottom left corner, this is used to connect a rotary encoder, or give direct access to additional GPIO pins. On the Right hand side of the PCB, a series of header pins give access to additional 3.3v and ground connections, in addition to GPIO 4 and 5, which is usually used for I2C…

LED COB Controller stacked on top of Variable Voltage Poser Module
LED COB Controller stacked on top of Variable Voltage Poser Module

The Schematic

Configuration and Software

As mentioned above, this device was designed to be used with ESPHome. The configuration is thus a single YAML file and can be greatly customised to suit your exact needs…

With that in mind, I present here a VERY basic YAML file, that will toggle the LED lights on or off on pressing the encoder switch, as well as adjust the brightness by turning the encoder.

esphome:
  name: led-cob-controller
  friendly_name: LED_COB-Controller

esp8266:
  board: nodemcuv2
  restore_from_flash: True

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "<esphome generated>"

ota:
  password: "<esphome generated>"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Led-Cob-Controller"
    password: "<your recovery password here>"

captive_portal:

# I suggest you only copy paste from here on downwards.
# Setup the default device in ESPHome, and when it is available,
# come back and add the commands below here


    
text_sensor:
  - platform: wifi_info
    ip_address:
      name: IP Address
    ssid:
      name: SSID
    bssid:
      name: BSSID
    mac_address:
      name: Wifi MAC
    scan_results:
      name: WiFi Scan Results
     


sensor:
  - platform: adc
    pin: VCC
    name: "ESP8266 Chip Voltage"
    id: mcu_voltage
    unit_of_measurement: "V"
    device_class: "voltage"
    accuracy_decimals: 2
    update_interval: 60s
    entity_category: "diagnostic"
    
  - platform: wifi_signal
    name: "WiFi Signal Sensor"
    id: wifi_strength
    device_class: "signal_strength"
    unit_of_measurement: "dBm"
    update_interval: 240s
    entity_category: "diagnostic"

  - platform: copy # Reports the WiFi signal strength in %
    source_id: wifi_strength
    name: "WiFi Signal Strength"
    filters:
      - lambda: return min(max(2 * (x + 100.0), 0.0), 100.0);
    unit_of_measurement: "%"
    entity_category: "diagnostic"

  - platform: copy
    source_id: rcbright
    name: "LED Light Brightness"
    unit_of_measurement: "%"
    filters:
      - lambda: return (x * 100);

  - platform: rotary_encoder
    name: "Brightness Control"
    id: rcbright
    min_value: 0.00
    max_value: 100.00
    publish_initial_value: True
    restore_mode: RESTORE_DEFAULT_ZERO

    pin_a: 
      number: GPIO13
      inverted: True
      mode:
        input: True
        pullup: True
      
    pin_b: 
      number: GPIO2
      inverted: True
      mode: 
        input: True
        pullup: True

    resolution: 1
    accuracy_decimals: 2
    filters:
    - lambda: return x / 100 ;
    on_clockwise:
    - light.control:
        id: led_light1
        brightness: !lambda |-
          // output value must be in range 0 - 1.0
          return id(rcbright).state ; // /100.0;
    - light.control:
        id: led_light2
        brightness: !lambda |-
          // output value must be in range 0 - 1.0
          return id(rcbright).state ; // /100.0;
    on_anticlockwise:
    - light.control:
        id: led_light1
        brightness: !lambda |-
          return id(rcbright).state ;   
    - light.control:
        id: led_light2
        brightness: !lambda |-
          // output value must be in range 0 - 1.0
          return id(rcbright).state ; // /100.0;  

binary_sensor:
  - platform: gpio
    pin: GPIO14
    id: light_switch
    name: "Light Switch"
    device_class: light
    on_click:
      then:
        - light.toggle: led_light1
        - light.toggle: led_light2
    

light:
  - platform: monochromatic
    name: "LED1_LIGHT_TEST"
    id: led_light1
    output: output_component1
  - platform: monochromatic
    name: "LED2_LIGHT_TEST"
    id: led_light2
    output: output_component2

# Example output entry
output:
  - platform: esp8266_pwm
    id: output_component1
    pin: GPIO12
  - platform: esp8266_pwm
    id: output_component2
    pin: GPIO16

Manufacturing the PCB

I choose PCBWay for my PCB manufacturing. Why? What makes them different from the rest?

PCBWay‘s business goal is to be the most professional PCB manufacturer for prototyping and low-volume production work in the world. With more than a decade in the business, they are committed to meeting the needs of their customers from different industries in terms of quality, delivery, cost-effectiveness and any other demanding requests. As one of the most experienced PCB manufacturers and SMT Assemblers in China, they pride themselves to be our (the Makers) best business partners, as well as good friends in every aspect of our PCB manufacturing needs. They strive to make our R&D work easy and hassle-free.

How do they do that?

PCBWay is NOT a broker. That means that they do all manufacturing and assembly themselves, cutting out all the middlemen, and saving us money.

PCBWay’s online quoting system gives a very detailed and accurate picture of all costs upfront, including components and assembly costs. This saves a lot of time and hassle.

PCBWay gives you one-on-one customer support, that answers you in 5 minutes ( from the Website chat ), or by email within a few hours ( from your personal account manager). Issues are really resolved very quickly, not that there are many anyway, but, as we are all human, it is nice to know that when a gremlin rears its head, you have someone to talk to that will do his/her best to resolve your issue as soon as possible.

Find out more here

Assembly and Testing

This device does not need a stencil for assembly, but using one will definitely speed up things. I chose to do this build all by hand, from applying solder-paste, up to placing components.

Soldering was done on a hotplate, as usual, to reflow everything at the same time. TH components were then placed and hand-soldered.

Uploading the initial firmware, after adding the device to ESPHome was done with an external USB-to-UART converter. All further firmware changes were made via OTA.

The board performs well, with only slight heating of the LM317G variable voltage regulators on the power module when both LED COB modules are at 100% brightness. The current draw is within limits and seems to peak at about 600mA per COB…

Conclusion

This project took quite a while to move from idea to practical reality, mainly due to being busy with other more important stuff. In the end, I am happy that I sat down and did it, because it definitely will become a valuable tool in my work area.

An I2C Matrix Keypad

The completed I2C Matrix Keypad

In a previous post this month I introduced my 4×4 matrix keypad. That keypad was designed to be directly interfaced to a microcontroller’s GPIO pins or alternatively to an IO expander chip like the PCF8574. That design, while working very well had the problem of requiring 8 GPIO pins to function correctly.

GPIO pins on a microcontroller can be considered very precious resources, and it should then be logical to assume that we should find a way to use these GPIO pins in a more conservative way, to allow us to interface more peripherals.

I solved this problem by integrating the keypad with an IO Expander on the same PCB. That will allow us to get away with using only 2 GPIO pins, and also open up the option of adding more keypads to the I2C bus, in the event that we need that many keys for a particular project.

The Schematic

I2C 4×4 Matrix Keypad Schematic

Looking closely at the schematic, we can see that it is exactly the same basic keypad circuit that I used in the initial design. The only difference is that in this design, I have integrated a PCF8574 directly onto the PCB.

Some additional features include selectable I2C Pullup resistors ( usually my microcontroller development boards already include those) that can be activated with a jumper when needed. There are also a set of address selection jumpers, making it possible to stack keypads together into a bigger keyboard if you require something like that. Note that, in this version of the hardware, I did not include headers for stacking.

The keypad can be powered by a DC power source of 3.3v to 5v.

The PCB

I2C Keypad PCB
3D Render of the I2C Keypad

The PCB is a double-layer board of 68.8mm x 50.8mm. Male header pins provide access to the connections as well as address and pullup resistor jumpers. In my build, I have mounted these male headers on the back of the PCB. That makes it possible to mount the Keypad in an enclosure without having the jumpers “stick out” and get in the way.

The top layer of the I2C Keypad PCB
Bottom Layer

Manufacturing

I choose PCBWay for my PCB manufacturing.
This month, PCBWay is also celebrating its 9th anniversary, and that means that there are quite a lot of very special offers available.

Why?
What makes them different from the rest?

PCBWay‘s business goal is to be the most professional PCB manufacturer for prototyping and low-volume production work in the world. With more than a decade in the business, they are committed to meeting the needs of their customers from different industries in terms of quality, delivery, cost-effectiveness and any other demanding requests. As one of the most experienced PCB manufacturers and SMT Assemblers in China, they pride themselves to be our (the Makers) best business partners, as well as good friends in every aspect of our PCB manufacturing needs. They strive to make our R&D work easy and hassle-free.

How do they do that?

PCBWay is NOT a broker. That means that they do all manufacturing and assembly themselves, cutting out all the middlemen, and saving us money.

PCBWay’s online quoting system gives a very detailed and accurate picture of all costs upfront, including components and assembly costs. This saves a lot of time and hassle.

PCBWay gives you one-on-one customer support, that answers you in 5 minutes ( from the Website chat ), or by email within a few hours ( from your personal account manager). Issues are really resolved very quickly, not that there are many anyway, but, as we are all human, it is nice to know that when a gremlin rears its head, you have someone to talk to that will do his/her best to resolve your issue as soon as possible.

Find out more here

Assembly

The assembly of this PCB was quite easy and quick. A stencil is not required. All SMD components are 0805 or bigger. It would thus be quite easy to solder them all by hand with a fine-tipped soldering iron.

I have however used soldering paste and hot air to reflow the components, as it is the fastest, in my opinion, and definitely looks neater than hand soldering.

After placing SMD components onto solder paste – ready for reflow soldering
After Reflow soldering with Hot Air

The board is now ready to solder the switches and header pins in place. As already mentioned above, I chose to assemble the headers on the back of the PCB to prevent them from interfering with any enclosure that I may later use with the keypad.

Final Assembly
Note that I assembled the headers onto the back of the PCB.

Testing and Coding

Testing the keypad consisted of a few steps, the first of which was ensuring that there were no short circuits, as well as that all the momentary switches worked.
This was done with a multimeter in continuity as well as diode mode, with probes alternatively on each column and row in turn, while pressing the buttons.

The next stage was testing the I2C IO Expander. This was done with a simple I2C Scanning sketch on an Arduino Uno. It did not do a lot, but, I could see that the PCF8574 is responding to its address and that the pullup resistors work when enabled. This test was repeated with my own ESP8266 and ESP32 boards, this time with pullup resistors disabled, as these boards already have them onboard.

Coding came next, and it was another case of perspectives. It seems like all commercial keypads do not have diodes. This affects the way that they work with a given library. It seems that software developers and hardware developers have different understandings of what a row and a column is.

This meant that, due to the fact that I have diodes on each switch, and the way that the library work – which pins are pulled high and which are set as inputs -, I had to swap around my rows and columns in the software to get everything to work. On a keypad with the diodes replaced with 0-ohm links, that was not needed.

A short test sketch follows below:

Note that with was run on an ESP8266-12E, therefore the Wire.begin() function was changed to Wire.begin(4,5); in order to use GPIO 4 and GPIO 5 for I2C

Another point to note is that the keypad Layout will seem strange. Remember that this is due to the diodes in series on each switch. That forces us to swap around the Rows and the Columns in the software, resulting in a mirrored and rotated left representation of the keypad. It looks funny, but believe me, it actually still works perfectly.

#include <Wire.h>
#include "Keypad.h"
#include <Keypad_I2C.h>

const byte n_rows = 4;
const byte n_cols = 4;

char keys[n_rows][n_cols] = {
    {'1', '4', '7', '*'},
    {'2', '5', '8', '0'},
    {'3', '6', '9', '#'},
    {'A', 'B', 'C', 'D'}};

byte rowPins[n_rows] = {4, 5, 6, 7};
byte colPins[n_cols] = {0, 1, 2, 3};

Keypad_I2C myKeypad = Keypad_I2C(makeKeymap(keys), rowPins, colPins, n_rows, n_cols, 0x20);

String swOnState(KeyState kpadState)
{
    switch (kpadState)
    {
    case IDLE:
        return "IDLE";
        break;
    case PRESSED:
        return "PRESSED";
        break;
    case HOLD:
        return "HOLD";
        break;
    case RELEASED:
        return "RELEASED";
        break;
    } // end switch-case
    return "";
} // end switch on state function

void setup()
{
    // This will be called by App.setup()
    Serial.begin(115200);
    while (!Serial)
    { /*wait*/
    }
    Serial.println("Press any key...");
    Wire.begin(4,5);
    myKeypad.begin(makeKeymap(keys));
}

char myKeyp = NO_KEY;
KeyState myKSp = IDLE;
auto myHold = false;

void loop()
{

    char myKey = myKeypad.getKey();
    KeyState myKS = myKeypad.getState();

    if (myKSp != myKS && myKS != IDLE)
    {
        Serial.print("myKS: ");
        Serial.println(swOnState(myKS));
        myKSp = myKS;
        if (myKey != NULL)
            myKeyp = myKey;
        String r;
        r = myKeyp;
        Serial.println("myKey: " + String(r));
        if (myKS == HOLD)
            myHold = true;
        if (myKS == RELEASED)
        {
            if (myHold)
                r = r + "+";
            Serial.println(r.c_str());
            myHold = false;
        }
        Serial.println(swOnState(myKS));
        myKey == NULL;
        myKS = IDLE;
    }
}

Conclusion

This project once again delivered what I set out to achieve. It has some quirks, but nothing serious. Everything works as expected, both in the Arduino IDE/platform IO realm, as well as in ESPHome. It is worth noting that in ESPHome, we do not need to swap the rows and columns to use the Keypad component. Do remember to leave the has_diodes flag to false though…

A Reliable Matrix Keypad

What is a matrix keypad?

A matrix keypad is a type of keypad that uses a matrix of wires to connect the keys to the microcontroller. This allows for a smaller and more compact keypad than a traditional keypad, which uses a single row and column of wires for each key. Matrix keypads are also more reliable than conventional keypads, as they are less susceptible to damage from dirt and moisture.

How does a matrix keypad work?

A matrix keypad is made up of a number of rows and columns of keys. Each key is connected to two wires, one for the row and one for the column. When a key is pressed, it completes a circuit between the row and column wires. The microcontroller can then determine which key is pressed by checking which row and column wires are connected.

Why use a matrix keypad?

There are a number of reasons why you might want to use a matrix keypad in your project. Here are a few of the most common reasons:

  • Smaller size and footprint.
  • Reliability.
  • Cost savings.

What makes my design different from most others out there?

While the matrix keypad in its simplest form is constructed from only wires and switches, that simple approach can sometimes have some unwanted effects, especially when pressing multiple keys at the same time – a phenomenon called ghosting – where you get phantom keypresses. This is easily eliminated by adding a diode in series with each switch, usually on the row connection.

That single component fixes ghosting reliably but does not come without its own problems, the most important of these being that a keypad with diodes becomes “polarised” – current can only flow in a single direction through a switch. This can cause problems with some third-party libraries, as the designer of the keypad and the designer of the library very often has quite different ideas of what a row and a column mean in a keypad.

This is important, – here we go down the rabbit hole; in my understanding of the keypad scanning routine, a column runs from top to bottom, and a row from left to right. Keeping this in mind, the microcontroller will alternatively set each column HIGH, and configure each row as an input. When a key is pressed, current will flow from the specific column GPIO, through the switch, and into the Row GPIO, sending the input pin HIGH…

It is also possible to configure the columns as inputs, with internal pullups enabled, and have each Row pin as an output, configured to sink ( pull current to ground). This will cause the specific column to go low – thus identifying the pressed key…

These different ways of handling the problem of reading a key, and believe me, there are actually more variations, create a few unique problems. We may have to swap rows and columns as far as pin connections and firmware are concerned, as well as define a custom “keymap” to assign values to each key.

The Schematic

As we can see above, the schematic is very basic. 16 switches, 16 diodes and a single 8-way header pin. Pin 1 to 4 on the header is connected to Columns 1 to 4, and Pin 5 to 8 is connected to Rows 1 to 4.

The diodes prevent “ghosting currents from flowing into other keys in a row when multiple keys are pressed together. They also seem to help with other stray signals and interference.

The PCB

The PCB is a simple double-layer board. All components are mounted on the top layer.

To limit interference from stray signals, I have routed rows and columns on opposite sides of the PCB where possible.

Manufacturing

I choose PCBWay for my PCB manufacturing.
This month, PCBWay is also celebrating its 9th anniversary, and that means that there are quite a lot of very special offers available.


Why? What makes them different from the rest?
PCBWay‘s business goal is to be the most professional PCB manufacturer for prototyping and low-volume production work in the world. With more than a decade in the business, they are committed to meeting the needs of their customers from different industries in terms of quality, delivery, cost-effectiveness and any other demanding requests. As one of the most experienced PCB manufacturers and SMT Assemblers in China, they pride themselves to be our (the Makers) best business partners, as well as good friends in every aspect of our PCB manufacturing needs. They strive to make our R&D work easy and hassle-free.

How do they do that?

PCBWay is NOT a broker. That means that they do all manufacturing and assembly themselves, cutting out all the middlemen, and saving us money.

PCBWay’s online quoting system gives a very detailed and accurate picture of all costs upfront, including components and assembly costs. This saves a lot of time and hassle.

PCBWay gives you one-on-one customer support, that answers you in 5 minutes ( from the Website chat ), or by email within a few hours ( from your personal account manager). Issues are really resolved very quickly, not that there are many anyway, but, as we are all human, it is nice to know that when a gremlin rears its head, you have someone to talk to that will do his/her best to resolve your issue as soon as possible.

Find out more here

Assembly

This project does not require a lot of specialised equipment to assemble. The SMD diodes can easily be soldered by hand, the same with the switches and 8-way header. In my case, I chose to solder the header pins on the back of the PCB, that way, I can later use the keypad in a suitable enclosure without having wires in the way.

Testing and Coding

Testing a matric keypad can sometimes be a challenge. In my case, a multimeter with clip leads, set to diode mode, with the leads connected to each column and row in turn, while minding the polarity, and pressing each key in that row in turn, verified continuity.

With that done, it was time to put my trusted Cytron Maker Uno to work, as this Arduino Clone has the added benefit of having LEDs on each of the GPIO lines, thus making it very easy to see what is happening.

I made use of a Keypad library in the Arduino IDE, mainly to cut down on the amount of coding, but also because it is easier to use a working piece of code, and then adapt that to my keypad.

Detailed Code examples for ESPHome are available on Patreon

/* @file CustomKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates changing the keypad size and key values.
|| #

Edited by MakerIoT2020, with minor changes to make it function correctly with my custom keypad.
I have also added a simple LED blinking routine to show that the Arduino is “alive” and that the Keypad code seems to be NON-blocking – which is quite important to me.

*/
#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the symbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{‘1′,’4′,’7′,’*’},
{‘2′,’5′,’8′,’0’},
{‘3′,’6′,’9′,’#’},
{‘A’,’B’,’C’,’D’}
};
byte rowPins[ROWS] = {2,3,4,5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6,7,8,9}; //connect to the column pinouts of the keypad
/*
* Due to libraries being written by different people, and our definitions about
* what a row and a column are, is different, note that the rows in the code
* is actually the columns on my PCB. This becomes true, due to the fact that my
* PCB has Diodes on each switch, and that thus makes current flow in only one
* direction///
*
* it also has the “side effect” that keys are layout in a strange “mirrored” and
* rotated way in the firmware.
* it does however NOT affect the correct operation of the Keypad Module at all
*
*/

const int LEDPin = LED_BUILTIN;
int ledState = LOW;
unsigned long prevmillis = 0;
const long interval = 1000;

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup(){
Serial.begin(115200);
pinMode(LEDPin,OUTPUT);
}

void loop(){
unsigned long currentMillis = millis();
if (currentMillis – prevmillis >= interval) {
prevmillis = currentMillis;
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
digitalWrite(LEDPin,ledState);
}
char customKey = customKeypad.getKey();

if (customKey){
Serial.println(customKey);
}
}

This code works very well and allowed me to verify the correct operation of the keypad.

In conclusion

Making my own Keypad Module is a project that is long overdue. I have purchased a few online over the years, and as they were mostly of the membrane type, they did not last very long – it must be something to do with the ultra-cheap flexible PCB ribbon connector, since a quality membrane keypad can be quite expensive, and usually lasts quite a long time.

Having my own module available to experiment with will allow me to do some long-delayed improvements to many of my IoT modules. That code, mostly YAML for ESPHome, will be made available on Patreon.

Useful ESP12E-DEV Prototype Shield

ESP-12E Prototype Shield

In answer to quite a few requests for a prototype shield, similar to my ESP32-S Dev Prototype shield, but for use with the ESP-12E DEV board, I have decided to do a quick design, and make it available publicly

This is the MakerIOT2020 ESP12E-DEV Prototype Shield. It is similar in purpose to the above-mentioned ESP32-S Dev Prototype shield, but I have also added some additional cosmetic changes to make it a little easier to use as well.

With many of my prototype designs, I tend to sometimes leave out something, as I usually use it for my own purposes only, but with this design, as many people specifically asked for it, I took a bit more care, as it is no longer just a prototype, right?

What has changed?

The most obvious is the increased prototyping area. The initial ESP32-S version had a 60-hole breadboard-style prototyping area. The new design has 128 prototype holes.

There is also a dedicated power input header, something that I somehow left out on the ESP32-S version… The Flash and Reset push-buttons were also moved inline, and to the bottom of the shield, making it more comfortable to use.

The design retains the plated through-hole design on the prototype area with connecting tracks on both sides of the PCB to allow for a bit more current.

The big ground plane on both sides of the PCB has also been retained.

PCB Design and Schematic

Top Layer Layout
Bottom Layer Layout

The prototype shield is for all purposes a breadboard. I did thus not bother with a formal schematic. I believe that it is easy enough to understand the connections by just looking at the two images above.

Manufacturing

The PCB for this project has been manufactured at PCBWay.
Please consider supporting them if you would like your own copy of this PCB, or if you have any PCB of your own that you need to have manufactured.

You can get your own copy here

PCBWay

Some More Pictures of the PCB

ESP8266-12E in Arduino Form Factor

As a followup to my recent ESP32-S in Arduino Form Factor device, I decided to also produce a similar circuit board for the ESP8266-12E. Although some people mostly consider these as obsolete, I still find them extremely useful for small projects, and thus use quite a lot of them.

With their lower total pin count and similar low cost to the bigger ESP32 modules, these do force you to get quite creative with the available pins, as well as how and what you can interface.

ESP8266-12E in Arduino Form Factor, with optional I2C pullups as well as Wake from sleep function on GPIO16

What is on the PCB?

For this design, I have once again tried to keep it very lean, including the absolute bare minimum supporting components needed for correct operation.
This includes pull-up and pull-down resistors on the various strapping pins, as well as some decoupling capacitors.

I also did not include a USB-to-Serial converter circuit, as these are used only once or twice, and also consume power while not being needed all the time.
As most of my devices are uploaded OTA anyway, an external stand-alone USB-to-Serial adapter is perfect to upload the initial firmware.

Jumpers to control the onboard I2C pullup-resistors, as well as the Wake-up-from-deep-sleep function on GPIO16 were also added, in addition to the Reset and Flash push-buttons to place the device into initial programming mode.

A 3.3v LDO regulator was also added, for convenience mostly, when powering the device from a bench power supply unit. The recommended voltage for this LDO would in my opinion be 7v Dc, although the datasheet states it can be up to 15v DC… In my opinion, that stresses the component a bit much though, as most of that excessive voltage is dissipated as heat.

PCB Layout – Blank board, front and back

Blank ESP-12E Dev board PCB – Front
ESP-12E Dev board- Back

To try and minimise heat-related issues with the LDO regulator, I have incorporated an on-PCB heatsink for the LDO regulator, which is via-stiched together on both sides of the PCB. This type of heatsink seems to work quite well in many of my other designs, so I decided to use it here as well.

I have also made use of big copper pours on both sides of the PCB to ensure that there is a good ground plane, as well as make use of differential pairs for the routing of the UART and I2C tracks.

Schematic

ESP-12E Dev Board Schematic

PCB Design


Manufacturing

The PCB for this project has been manufactured at PCBWay.
Please consider supporting them if you would like your own copy of this PCB, or if you have any PCB of your own that you need to have manufactured.

You can get your own copy here

PCBWay

Some assembly pictures

Procedure to upload initial firmware

Due to the fact that this device does not have an onboard USB-to-UART (Serial) converter, it will be necessary to use an external device the first time that you upload firmware, or while you are using it on the bench if you do not want to use OTA…

The easiest way to do this, in my opinion, is the following:
1) Power the device from a bench power supply, set at 7V DC via the VIN and GND Pins, available on the bottom left and right of the device ( you can use either side, not both 🙂

2) Now connect your external USB-to-UART converter to the device, as follows:
UART Converter <-> ESP-12E Dev Board
Rx <- TxD
Tx -> RxD
Gnd — GnD

Do Not connect the power (VCC) from the USB-to-UART Converter!

Now start your Adruino IDE, or similar, and connect to the Serial monitor.
Press the reset button on the board, and watch for output in the serial monitor.

If you see output, at 115200 BPS, press end hold flash, and then press and let go of reset, while still holding the flash button.

Wait a few seconds, and then upload your sketch, remembering to manually reset the board after the upload has completed.

I would recommend that you upload the Arduino OTA sketch, from the examples, and modify it to connect to your local WiFi. That way, you will be able to upload all following sketches via your local Wifi, providing that you do not remove the OTA code from the sketch.



I2C IO Card for ESP-12E I2C Base Card

The I2C IO Card for ESP-12E I2C Base Card is another expander card for the ESP-12E I2C Base Card Project. This PCB is an address-selectable I2C module with two relays and six (6) GPIO pins, all driven from a single PCF8574 running at 3v. The relays are optically isolated, and generous mains isolation cutouts were provided to reduce the possibility of mains voltage tracking. A jumper to enable/disable the i2c pullup-resistors is also provided on the PCB.

The relays are powered from a single LDO regulator, accepting 12v DC from the 2x20pin female header on the bottom of the card. 3.3v and ground should also be applied to the card at the 2x20pin header.

It is worth mentioning that this circuit does not contain level converting circuitry and that the i2c bus thus runs at 3.3v to be compatible with ESP chips.

It is possible to use the card with other processors if the appropriate level converters are used on the i2c bus.

The Schematic

Manufacturing the PCB


Over the past eight years, PCBWay has continuously upgraded their MANUFACTURING plants and equipment to meet higher quality requirements, and now THEY also provide OEM services to build your products from ideas to mass production and access to the market.


The PCB for this project has been manufactured at PCBWay.
Please consider supporting them if you would like your own copy of this PCB, or if you have any PCB of your own that you need to have manufactured.

PCBWay

If you would like to have PCBWAY manufacture one of your own, designs, or even this particular PCB, you need to do the following…
1) Click on this link
2) Create an account if you have not already got one of your own.
If you use the link above, you will also instantly receive a $5 USD coupon, which you can use on your first or any other order later. (Disclaimer: I will earn a small referral fee from PCBWay. This referral fee will not affect the cost of your order, nor will you pay any part thereof.)
3) Once you have gone to their website, and created an account, or login with your existing account,

4) Click on PCB Instant Quote

5) If you do not have any very special requirements for your PCB, click on Quick-order PCB

6) Click on Add Gerber File, and select your Gerber file(s) from your computer. Most of your PCB details will now be automatically selected, leaving you to only select the solder mask and silk-screen colour, as well as to remove the order number or not. You can of course fine-tune everything exactly as you want as well.

7) You can also select whether you want an SMD stencil, or have the board assembled after manufacturing. Please note that the assembly service, as well as the cost of your components, ARE NOT included in the initial quoted price. ( The quote will update depending on what options you select ).

8) When you are happy with the options that you have selected, you can click on the Save to Cart Button. From here on, you can go to the top of the screen, click on Cart, make any payment(s) or use any coupons that you have in your account.

Then just sit back and wait for your new PCB to be delivered to your door via the shipping company that you have selected during checkout.