While my recent ESP32-S Dev Board really does the trick to help my development cycle along, I very quickly ran into another obstacle, in the sense that, after doing stuff on the breadboard, moving those components onto a more permanent location, either as a next stage prototype or more likely that the project is so small and insignificant not to warrant the effort actually to design a PCB for it. This could be rectified by using another one of my recent designs, an SMD breadboard PCB, but that would not always do either.
MakerIoT SMD Prototype development PCB
That got me thinking, and while staring at the ever-present Arduino Uno on its corner of the work-bench, I suddenly remembered that I have once seen an Arduino Prototype Shield, like a plug-on breadboard, with breakouts of all the pins etc…
While I do not personally own a lot of commercial Arduino Shields, as I tend to build my own or design a custom-purpose PCB instead, it did not take me long to settle on a new design, that could potentially solve my problem, and hopefully, someone else’s as well…
So what is on this PCB?
To start off, the PCB is in the same form factor as the ESP32-S Dev Board, namely the Arduino Uno form factor. There are however a few changes, mainly in the number of pins in the headers. This is mainly to accommodate as many of the ESP32-S’s gpio’s as possible. ( Actually, they are all broken out, EXCEPT for the 6 gpio’s that are usually used with the internal Flash memory.)
The PCB is designed to be stacked either on top of, or even below, the ESP32-S Dev Board, depending of course on the type of headers that you decide to solder onto the PCB.
In order to make connecting to the gpio pins easier, each header row is in fact a double row, with solderable pads in parallel for each gpio on the header row.
Flash and Reset buttons are available on top of the shield, they can be fitted of left off, depending on personal preference, as well as how the shield will ultimately be used.
The prototyping area in the centre has been slightly reduced from the standard 5-pin-spacer-5-pin column of the traditional breadboard to a 3-pin-gnd-3v-3-pin column layout. the prototyping holes are at a standard 0.1″ or 2.54mm pitch.
In total, 60 prototype holes, divided into rows of 10, 3 columns deep, are provided, labelled A-F and 1-10.
3.3v and ground are provided in the centre-two rows, to make power easily accessible.
ESP32-S DEV Prototype ShieldESP32-S Dev Prototype Shield – Back
The PCB Design
As this design is basically just two rows of header pins, with a few switches, and a big unconnected prototype area, I did not bother to do a formal schematic for this PCB, but instead jumped straight into the PCB design software and manually designed and routed the tracks and pads that make up this shield.
PCB – Top Layer PCB – Bottom Layer – Note that this is a “TOP-Down” view, and should be mirrored for actual production
Note that there are big copper pours on both top and bottom layers, in an attemp to reduce electrical noise and provide better shielding.
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.
Top viewTop view – Not assembledBottom view – AssembledBottom view – Not assembledSide-by sideSide by SideStacked – Side viewStackedStacked – top viewStacked – from front
Conclusion
Some final thoughts on the completed PCB. While definitely useful, I have made a purpuseful design flaw on this board, by not including a breakout for the VIN pin. My reasoning at that stage was that I would always be powering the device directly from 3.3v, and would therefor not need access to the VIN pin for power.
Upon completion of the device, and while testing it in a stacked configuration, I realised that that VIN pin would have been quite nice to have access to.
Not a big problem though, as if is very easy to add a 2-pin connector to the power rails, or even solder a wire directly to VIN. Ugly, but totally doable, as this is in fact still a prototype, and it can grow and be fine-tuned to my exact requirements over time.
The ESP32-S is, at least in my opinion, one of the most versatile microcontrollers available to the Maker at this moment. It ticks almost all of my boxes for features required in a microcontroller, with a lot of gpio’s, WiFi, and Bluetooth, as well as a lot of storage space for code.
I do however have an issue with it, which I usually get around by designing a custom circuit board with a specific purpose. This is great for a project, but as most projects do not start on a custom-built circuit board, I am usually required to use a breadboard module. This is where my problems start. These modules are cumbersome to fit on a breadboard, to say the least, taking up a lot of space, and leaving very little space to connect to its pins with anything else.
Some of these modules do not even fit on the breadboard, making it necessary to hang one side off the breadboard or use two breadboards with a gap in the middle. I am quite sure many people can relate to this problem.
My second issue is that when you have done your breadboarding, and want to go to a permanent project, which does not always need a dedicated PCB, you are now required to either live with things on a breadboard, scary to say the least or have a “spider” with many modules and wires, in a box or partly on protoboard etc…
My Solution, the ESP32-S DEV Board, in Arduino Uno form factor
While not the most elegant, personally I really like the size, and layout of the humble Arduino Uno, with its standardised pinouts, and a large number of addon shields available for the platform. This made me think, sure, there are already ESP32-based boards in this form factor available commercially, but why not make my own instead, as well as a few of my most used modules in a standardised shield form, to make my life just that little bit easier?
The picture above shows my attempt, with most of the GPIO broken out onto female header pins (except for the 6 gpio that are connected to the internal flash chip on the module).
The Blank PCB ( front )PCB- Back
The PCB explained…
Power: The board can be powered in two ways, either via the VIN pin ( at an optimal 7.0v DC – the LDO regulator can handle up to 15v, but I personally find that to stress it a bit hard ), which will use the onboard LDO voltage regulator to provide the needed 3.3v or from an external 3.3v PSU, which can provide a bit more current if needed…
There are also plenty of 3.3v and ground connections on the two 20-way headers to connect to other sensors.
Strapping Pins All the required strapping pins are pulled up or pulled down, as per the datasheet, to 3.3v or ground respectively.
GPIO Pins All GPIO pins are clearly labelled on the silkscreen to make it easier to use. I did however not stick to the Arduino labelling convention, as I don’t always use the Arduino IDE, and the actual GPIO numbers are in my view, more useful then.
Flashing code to the board
It will be quite obvious that I did not include any USB-to-serial converter on the board, the reason for this being that, in my opinion, 1) it wastes space on the board 2) it is not actually necessary, as we can upload with an external uart adapter, or use OTA ( which I actually do most of the time ) 3) In an actual project, that USB port is going to attract problems, especially if you give it to someone else to use…
A simple Arduino OTA sketch is available in the examples section of the Arduino IDE. It is easy to use and modify and does not need a lot to make it useable with your own sketch…
Antenna Cutout
As recommended by the manufacturer , I have chosen to place the chip inside a cutout on the top of the PCB, with no tracks nearby.
Figure 17: Keepout Zone for ESP32 Module’s Antenna on the Base Board
Although this is not the ideal “best position”, I found that this position worked well with previous designs, and have thus kept it at that.
General comments
As this board is mainly designed for prototype development, I did not bother with dedicated power connectors etc. I did however add proper wide tracks for all the power connections, an on-PCB-heatsink for the voltage regulator, as well as proper ground planes on both sides of the PCB, connected together with via-stitching where needed.
It is also very important to note that this is a 3.3v device. If you need to use sensors or peripherals that operate at other voltages, you will have to use external level converters.
Some assembly pictures
After Solderpaste applicationBefore ReflowAfter ReflowAfter final assembly
Schematic
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.
This project is the result of a lot of prototyping, using different MCUs and wanting to find a way to get a standard interface to all the devices.
The idea is to eventually create similar card-type MCU breakout boards, with similar pins in the same position on the 2x20p breakout header,
for example, power, i2c bus, reset and flash will always be in the same position on the female header…
Step 2 from here on would be to design a baseboard, that is capable of providing power, as well as access to the various GPIO pins. I am thinking along the way of a PC motherboard style interface, with “slots” at regular intervals. These “slots” will have access to the SPI, and I2C bus, as well as various other GPIO.
Step 3 would be a series of commonly used input and output “cards” that will plug into the “slots”…
If successful, I plan to design various MCU cards, with various different processors, with the obvious criteria that they are 3v powered.
This could result in a very flexible development platform, where it is possible to reuse the base-board and IO “cards” with any one of the various MCU “cards”.
The Schematic
As seen on the schematic, almost all of the ESP32-S’s pins are broken out, with the exception of those used for internal flash. Reset and Flash circuitry is provided on the PCB, as well as on the 2x20pin female header.
It is worth noting that I did not include any UART to USB circuitry on the card. Flashing should be performed with an external USB-to-UART converter. It will however be included in the base-board.
There is also no power supply circuitry onboard. This was also intentional, as the card is intended to be powered from the base-board. It is however perfectly acceptable to power only the card from a suitable 3.3v DC power supply unit through the 3v and gnd pins on the 2x20pin header.
Where can I get my own version of this module?
This module will be exclusively available from PCBWay for the foreseeable future. Click on this link to order your own, and help support a great company that produces very high-quality PCBs for a very affordable price.
This PCB was manufactured at PCBWAY. The Gerber files and BOM, as well as all the schematics, will soon be available as a shared project on their website. 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 $5USD 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.
Most Makers and electronics enthusiasts may already know of the RA-02 LoRa Module. Many of them might own an RA-02 Breakout module or two… For those who do, they will surely know about the problems encountered with using this particular breakout module…
The RA-02 module, in itself, is a great piece of kit, and when used on a custom PCB, which was designed with all the little secrets of this module taken into consideration, is a pleasure. Using the RA-02 breakout module, in its existing form factor, does however present quite a few unique challenges, which, if you are unaware of them, can cause quite a few frustrating moments, or even result in permanent damage to the module…
1) The module is based on the SX1278 chip from Semtech and is a 3v device. The IO pins are NOT 5v compatible but seem to work for a few hours or so when used with 5v… This causes many people, especially on Youtube, to assume that it is ok to send 5v logic signals to this module…
I have still not seen any Youtube video telling viewers to at least use a resister divider or logic converter… People just don’t know, and those that know seem to be keeping quiet!
Adding logic converters is in fact specified by the datasheet.
2) Adding logic converters means adding additional wiring, and for a breadboard based project, that adds to the complexity.
3) You have a total of 4 ground pins that need to be connected. not connecting all of them, causes funny things to happen, from overheating down to failure… ( My personal experience while researching this project)
4) The existing breakout module is not breadboarding compatible, resulting in a floating assembly with wires going everywhere, which results in unstable connections etc…
Basically something similar to the picture below:
In this picture, I have an existing RA-02 Breakout Module, with an 8 channel Logic converter and an Arduino Uno clone, along with all the needed wiring to make this setup possible… Quite a lot of wires indeed…
My solution:
I design and use quite a few LoRa PCBs and on all of them, I implement logic conversion using the BSS138 N-MOS Mosfet and 10k resistors. It is a cheap and reliable solution, but it can take up quite a lot of space on a PCB, as this means 11 Mosfets and 22 10k resistors if I were to provide level conversion to all of the RA-02’s GPIO and IO pins…
I also have the constant problem of many unnecessary wires, many of which sometimes fail straight out of the box, when prototyping something. I partly solved that by designing a few dedicated PCB solutions, but that is not always ideal,
Using a dedicated Logic Converter IC, and Mosfet based converters to make up the difference, on a breadboard compatible module, seemed like a good idea, so I went ahead and designed the following solution:
The breakout board module is breadboard compatible, and also has clearly marked pins to indicate the 3v and 5v sides of the module.
For my first test, I decided to test with an Arduino Uno Clone, since that is what most Makers and students will have access to. I used Cytron’s Maker Uno platform, which is equipped with some added goodies, in the form of diagnostic LED etc to make prototyping a lot easier.
As we can clearly see, It is only necessary to connect to the 5v logic side of the module, as well as provide 3v and 5v + GND to the module
In this test, I used Sandeep Mistry’s LoRa Library, with the Arduino IDE to do a quick test sketch.
As we can see, you need quite a lot more wires to make this work. It is also worth noting that we have only 8 level converters on this ATMEGA328P PCB, in order to use all of the RA-02’s GPIO, we will need to add an additional external logic converter as well.
For my second test, I decided to be a bit brave, and try to use the new Raspberry Pi Pico ( RP2040 Microprocessor ). I have quite a few of them lying around and have never really done a lot with them, due to the fact that I do not really like using MicroPython or CircuitPython, and also because the recently released Arduino Core for the RP2040 still being quite new… I decided to use a development board that I recently bought from Cytron, the Maker Nano RP2040, as it has all the added diagnostic features to make my life a bit easier, I will also include a test with an original Pi Pico board, to make it more accessible to everyone out there.
Once again, I used Sandeep Mistry’s LoRa Library, with the exact same Arduino sketch, used for the Maker Uno test. (I obviously needed to change the pin numbers though, as the RP2040 uses different pins for its SPI interface).
Maker Nano RP2040 RA-02 Breakout Module
NSS 17
MOSI 19
MISO 16
SCK 18
RST 9
DIO0 8
In this case, we DO NOT need the OE pin, as the RP2040 is a native 3v device. The level converter can thus stay disabled, with its pins in tri-state ( high impedance ) mode.
If we look at the code, it is similar to the Maker Uno’s code, with only the Pin declarations needing a change
#include <SPI.h> // include libraries
#include <LoRa.h>
const int csPin = 17; // LoRa radio chip select
const int resetPin = 9; // LoRa radio reset
const int irqPin = 8; // change for your board; must be a hardware interrupt pin
byte msgCount = 0; // count of outgoing messages
int interval = 2000; // interval between sends
long lastSendTime = 0; // time of last packet send
// Note that SPI has different names on the RP2040, and it has 2 SPI ports. We used port 0
// CIPO (Miso) is on pin 16
// COPI (Mosi) is on pin 19
// SCK is on pin 18
// CE/SS is on pin 17, as already declared above
I did not use a breadboard, in order to make things as easy as possible.
To make things a bit easier, without having to resort to using a breadboard, I decided to do the Original Pi Pico test using the Maker Pi Pico PCB. This PCB is basically a big breakout module, with detailed pin numbers and some diagnostic LEDs, but it also uses a native Pi Pico, soldered directly to the PCB, by means of the castellated holes… So, While technically not being a true standalone Pico, It makes my life easier and was thus used for the test, as I can be sure that the pins are labelled exactly the same as on the original Pico.
The code used for the Maker Nano RP2040 works perfectly, with no changes required.
This post is getting quite long by now, so I have decided not to include my tests of the ESP-12E ( NodeMCU ) or ESP32 development boards here as well… They also function as expected.
In Summary
When I started this project, I set out to solve a problem ( personal to me ), that could potentially help a lot of other people use the RA-02 Module for more projects and tasks. The Breakout module in its current form can also be used with the RA-01h module (915Mhz Module) without any changes. All GPIO pins are broken out, and accessible through full logic converted pins on both sides of the breakout module.
I hope that this will be useful to someone. I am also not releasing the full schematics at this stage, as I may decide to make some minor cosmetic changes in the near future.
The PCB can however be ordered from PCBWay in its current form and works 100% as expected. The BOM file is available with the ordered PCB as usual.
This PCB was manufactured at PCBWAY. The Gerber files and BOM, as well as all the schematics, will soon be available as a shared project on their website. 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 $5USD 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.
So many people asked me which Lora Module I use for my projects. In this part of the series, I will show you, as well as shed some light on another module, that although seemingly cheap, is, unfortunately, according to me, a complete waste of time and money.
Heltec LoRa 32 v 2 – The good stuff ( according to me at least)
Technical SpecsElectrical SpecificationsPinout
Installation in Arduino IDE:
Installation of the libraries into the Arduino IDE is quite easy, just follow the link to heltec…
The Bad ( according to me )
The following module, is, according to me, an absolute waste of time and money. Documentation is impossible to find, and that that you do find, as often incorrect. The pin-outs are wrong, with no definite standard.
I am talking about the TTGO Lora V1 or V2 or what ever ??? can seem to even find that answer reliably. I was initially attracted to this module, as it was allegedly compatible with the heltec version, and did not have the oled screen, which, to be honest, is not always needed in every project. It was also about 25% cheaper, and could be sourced locally, without enriching the greedy shipping companies 😉 (I just have to rant about this, as 25USD to ship 100g worth of stuff is a ripoff. Either that or 60 to 90 days of guess-if-it-will-arrive mail is not on ( and even that is 10 USD!)
So, having high hopes, I ordered one of these boards, hoping to use it together with my heltec boards… It arrived, and that was well the top came of.. I could immediately see that the quality of the PCB was quite bad. Documentation was missing, and even the supplier sent me to a heltec pinout, which, after a quick test were definitely not correct…
Google turned up mixed results, and eventually I found a sort of accurate pinout …
Alleged pinout for TTGO LoRa device
This pinout also turned out to be only about 50% correct, and after manually trying to map out the pins, I was sort of confident enough to test it further…
Further problems arose, LoRa does not work, I2C does not work, SPI does not work shall I continue…? 🙂 It now seems clear that the board that I bought was a clone of a clone, and a very bad one at that … I will post a picture of the actual board below, in the interest of education, to inform others not to get duped as well. Likewise, If I am the mistaken party, and you have had success with this board, please give me a message/yell and lets share some knowledge
The Front (Top Side) of the Module
Front (Top) of the moduleBack (Bottom) of the module
I hope that you found this useful and that I will see you for part 3 of the series, where I will get into the actual coding.
When designing IoT solutions, we all encounter the problem of connecting our device(s) to each other, either directly, or through the internet. In Urban areas, it is quite easy to use WiFi or even GSM to achieve this, but these solutions often come with additional costs in the form of subscriptions. Although it is possible to run your own WiFi network free of charge, you will soon run into issues with the range…
Enter LoRa (short for Long Range) Radio communication. LoRa is a radio technology derived from chirp spread spectrum technology. It uses an ISM band, meaning it is unregulated in most countries, if you use the correct frequency for your country, that is.
It is also extremely low power, making it ideal for use with battery-powered devices. The technology is available in Node-to-Node, as well as Node-to -Gateway modes.
In this series, I will show you how to use a few of the existing LoRa Modules available on the market.
Ai-Tinker Ra-02 (Sx1278)
Ra-02 Lora Module, with spring antenna, by Ai-Tinker
This Module is conveniently broken-out onto a breakout board. It is sort of bread-board friendly (depending on the size of your bread-board) and is nicely labelled. It is also extremely cheap ( around $USD5 each, depending on where you buy from).
Caveats
There are quite a few important things that you should know about these modules before you start using them.
Disclaimer: The caveats listed below are by no means complete, or even valid. They are the result of experimentation by myself, with the intent to destroy a few modules, to see how hardy they are. Also take into mind, that living in SE Asia, it is quite common to buy something from a shop, where the seller has no or only a very limited idea of what he or she is selling, and are thus usually quite unable to provide any technical support.
To summarise: USE YOUR HEAD. If I did leave out something, it is quite possible that I forgot, or decided not to include it on purpose. This is a general guide, and you should ideally do your own research as well. That is the best way to learn.
1) Always connect an Antenna. This may seem like a logical one, but it is extremely important. The module is capable of quite a lot of transmission power, and operating it without an antenna will quickly damage the module, permanently.
2) ONLY use 3.3v, even on the control lines (the module uses SPI). This is quite important, as it is not very clearly stated by the suppliers, and will result in very short-lived component operation 😉 If you absolutely have to use 5v, use a level converter. (There are examples available on the internet, where they use this chip directly from an Arduino Uno. I can confirm that that approach does work, BUT, not for very long. I have purposely sacrificed a pair of transceiver modules so that you don’t have to. You can also adjust the SPI frequency, in the event that your level converter is not capable of running at a high SPI frequency.
3)Make sure that you connect ALL the ground pins on the device. This is another area that is not fully explained by the user manual and does “unexplainably” result in damaged modules.
4) Use short, good quality cables, and if possible, keep the module off the breadboard. While testing the modules, I found that the usual DuPont wires, as everyone should know by now, are quite unreliable. Combine that with a bread-board that has seen its share of use, and it is a definite recipe for headache 🙂
5) LoRa Antennas are polarised, make sure you have your antennas in the same orientation. Although this will not prevent it from working over short distances, it makes sense to just do it correctly. Good RF practices never hurt anybody 🙂
Connecting to Arduino
A Note on Power: It is important to power this module from a decent dedicated 3.3v power-supply. The Arduino Uno does sometimes have a 3.3v regulator on-board. From my tests, it is however not always up to the task, as the module may spike up to 120mA when transmitting. It is thus also recommended to have a nice fat capacitor across the power lines (decoupling cap) to soak up any spikes.
As mentioned above, a level converter is mandatory for a 5v Arduino. You may do without it if you use a 3.3v Arduino, but once again, your mileage will vary 🙂
Both the transmitter and receiver uses the same connections, which are listed below:
LoRa SX1278 Module
Arduino Board
3.3V
–
Gnd
Gnd
En/Nss
D10
G0/DIO0
D2
SCK
D13
MISO
D12
MOSI
D11
RST
D9
Connections to the Arduino from a LoRa RA-02 Module
Remember that you NEED a Level converter between the LoRa Module and the Arduino.
Software Library
The software library that we will use in our example is the excellent library from Sandeep Mistry. We will just include this into the Arduino IDE, and then use a slightly modified version of the examples for our experiment. It is also important to note that we will use Node-to Node communication, NOT LoRaWan. This means that all your communications will essentially be unencrypted, and not addressed. This does however allow you the flexibility to design and implement your own addressing scheme.
LORA code for Transmitting Side
#include <SPI.h>
#include <LoRa.h>
int counter = 0;
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println("LoRa Sender");
if (!LoRa.begin(433E6)) { // Set the frequency to that of your //module. Mine uses 433Mhz, thus I have set it to 433E6
Serial.println("Starting LoRa failed!");
while (1);
}
LoRa.setTxPower(20);
}
void loop() {
Serial.print("Sending packet: ");
Serial.println(counter);
// send packet
LoRa.beginPacket();
LoRa.print("hello ");
LoRa.print(counter);
LoRa.endPacket();
counter++;
delay(5000);
}
LORA code for Receiver Side
#include <SPI.h>
#include <LoRa.h>
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println("LoRa Receiver");
if (!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
}
void loop() {
// try to parse packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
// received a packet
Serial.print("Received packet '");
// read packet
while (LoRa.available()) {
Serial.print((char)LoRa.read());
}
// print RSSI of packet
Serial.print("' with RSSI ");
Serial.println(LoRa.packetRssi());
}
}
Where to from here?
If all went well, you will see packets being received in the serial monitor of the Arduino IDE, connected to the receiver module. You will also see that the data from this example is sent as a string… It is however also possible to send binary data, by using the LoRa.write() function.
In the next part of this series, I will show you how to use LoRa with the ESP32/ESP8266, as well as a working example with binary data transmission and an addressing scheme in part 3.
In a previous post, I have shown you how to use the MCP23017 16 Port I2C I/O Port extender with the standard Wire library, as supplied with the Arduino IDE. In this post, I will have a quick look at using Adafruit’s library for this IC. I believe that this library brings a lot of ease-of-use to the part, making it possible to obscure some of the complexity of I2C.
I do however prefer to use the native Wire library myself, as it is slightly faster.
When using single pin operations such as pinMode(pinId, dir) or digitalRead(pinId) or digitalWrite(pinId, val) then the pins are addressed using the ID’s below. For example, for set the mode of GPB0 then use pinMode(8, …).
Physical Pin #
Pin Name
Pin ID
21
GPA0
0
22
GPA1
1
23
GPA2
2
24
GPA3
3
25
GPA4
4
26
GPA5
5
27
GPA6
6
28
GPA7
7
1
GPB0
8
2
GPB1
9
3
GPB2
10
4
GPB3
11
5
GPB4
12
6
GPB5
13
7
GPB6
14
8
GPB7
15
Some examples, directly from the library, all code belongs to Adafruit, and was not written by me.
1. A Button Example
#include <Wire.h>
#include "Adafruit_MCP23017.h"
// Basic pin reading and pullup test for the MCP23017 I/O expander
// public domain!
// Connect pin #12 of the expander to Analog 5 (i2c clock)
// Connect pin #13 of the expander to Analog 4 (i2c data)
// Connect pins #15, 16 and 17 of the expander to ground (address selection)
// Connect pin #9 of the expander to 5V (power)
// Connect pin #10 of the expander to ground (common ground)
// Connect pin #18 through a ~10kohm resistor to 5V (reset pin, active low)
// Input #0 is on pin 21 so connect a button or switch from there to ground
Adafruit_MCP23017 mcp;
void setup() {
mcp.begin(); // use default address 0
mcp.pinMode(0, INPUT);
mcp.pullUp(0, HIGH); // turn on a 100K pullup internally
pinMode(13, OUTPUT); // use the p13 LED as debugging
}
void loop() {
// The LED will 'echo' the button
digitalWrite(13, mcp.digitalRead(0));
}
2. An Interrupt Example
// Install the LowPower library for optional sleeping support.
// See loop() function comments for details on usage.
//#include <LowPower.h>
#include <Wire.h>
#include <Adafruit_MCP23017.h>
Adafruit_MCP23017 mcp;
byte ledPin=13;
// Interrupts from the MCP will be handled by this PIN
byte arduinoIntPin=3;
// ... and this interrupt vector
byte arduinoInterrupt=1;
volatile boolean awakenByInterrupt = false;
// Two pins at the MCP (Ports A/B where some buttons have been setup.)
// Buttons connect the pin to grond, and pins are pulled up.
byte mcpPinA=7;
byte mcpPinB=15;
void setup(){
Serial.begin(9600);
Serial.println("MCP23007 Interrupt Test");
pinMode(arduinoIntPin,INPUT);
mcp.begin(); // use default address 0
// We mirror INTA and INTB, so that only one line is required between MCP and Arduino for int reporting
// The INTA/B will not be Floating
// INTs will be signaled with a LOW
mcp.setupInterrupts(true,false,LOW);
// configuration for a button on port A
// interrupt will triger when the pin is taken to ground by a pushbutton
mcp.pinMode(mcpPinA, INPUT);
mcp.pullUp(mcpPinA, HIGH); // turn on a 100K pullup internally
mcp.setupInterruptPin(mcpPinA,FALLING);
// similar, but on port B.
mcp.pinMode(mcpPinB, INPUT);
mcp.pullUp(mcpPinB, HIGH); // turn on a 100K pullup internall
mcp.setupInterruptPin(mcpPinB,FALLING);
// We will setup a pin for flashing from the int routine
pinMode(ledPin, OUTPUT); // use the p13 LED as debugging
}
// The int handler will just signal that the int has happen
// we will do the work from the main loop.
void intCallBack(){
awakenByInterrupt=true;
}
void handleInterrupt(){
// Get more information from the MCP from the INT
uint8_t pin=mcp.getLastInterruptPin();
uint8_t val=mcp.getLastInterruptPinValue();
// We will flash the led 1 or 2 times depending on the PIN that triggered the Interrupt
// 3 and 4 flases are supposed to be impossible conditions... just for debugging.
uint8_t flashes=4;
if(pin==mcpPinA) flashes=1;
if(pin==mcpPinB) flashes=2;
if(val!=LOW) flashes=3;
// simulate some output associated to this
for(int i=0;i<flashes;i++){
delay(100);
digitalWrite(ledPin,HIGH);
delay(100);
digitalWrite(ledPin,LOW);
}
// we have to wait for the interrupt condition to finish
// otherwise we might go to sleep with an ongoing condition and never wake up again.
// as, an action is required to clear the INT flag, and allow it to trigger again.
// see datasheet for datails.
while( ! (mcp.digitalRead(mcpPinB) && mcp.digitalRead(mcpPinA) ));
// and clean queued INT signal
cleanInterrupts();
}
// handy for interrupts triggered by buttons
// normally signal a few due to bouncing issues
void cleanInterrupts(){
EIFR=0x01;
awakenByInterrupt=false;
}
/**
* main routine: sleep the arduino, and wake up on Interrups.
* the LowPower library, or similar is required for sleeping, but sleep is simulated here.
* It is actually posible to get the MCP to draw only 1uA while in standby as the datasheet claims,
* however there is no stadndby mode. Its all down to seting up each pin in a way that current does not flow.
* and you can wait for interrupts while waiting.
*/
void loop(){
// enable interrupts before going to sleep/wait
// And we setup a callback for the arduino INT handler.
attachInterrupt(arduinoInterrupt,intCallBack,FALLING);
// Simulate a deep sleep
while(!awakenByInterrupt);
// Or sleep the arduino, this lib is great, if you have it.
//LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF);
// disable interrupts while handling them.
detachInterrupt(arduinoInterrupt);
if(awakenByInterrupt) handleInterrupt();
}
I hope that this shows you another way of using this versatile IC,
In a future post, I will show you how to do interrupts, using the native Wire library, as well as point out a few things about why interrrupts sometimes does not seem to be working, as well as a workaround for that.
While working on a recent project, using a custom version of the ESP32, I had a particular need to be able to update the firmware of the device, as well as allow the end-user to set various configuration options on the device. This need can best be described as follows below:
1. The device firmware needs to be updated periodically, as new features are needed, or when bugs are discovered by the end-user, that needs fixing. The problem encountered was that the physical device will be quite far away from me, so travelling, or sending the device to and fro via post was not really an option.
Sending uncompiled firmware to the customer is a possibility, but also risky and prone to errors, as everyone is not always inclined to learn how to update firmware via USB port from an IDE, and accidental changes to code can render it unable to compile and upload in the first place.
The device also needs to run on battery power, in remote areas without connectivity. Conserving battery power is thus also a very important issue, one that prevents me from having a permanent OTA server running via WiFi.
2. The second issue was that there are certain setup parameters that needed to be set by the end-user, these include addresses and others. My initial thoughts were to implement a simple UI via UART, but that I also quickly saw that that, although very useable to me, would not appeal very much to an end-user.
These two issues, OTA and Configuration, both done through WiFi, seemed mutually exclusive as far as all the research that I have done online seemed to be concerned. To complicate it even further, the WiFi HAD to stay off when not in use.
I believe that I have developed a very neat, workable solution, that I would like to share with you today, in the hope that it will solve some problems for somebody out there, that maybe having a similar problem.
My solution makes use of a “flag” set in the ESP32/ESP8266 device’s EEPROM. This “flag” can then be set to a “Normal” Run mode, a Firmware Update Mode, and a Configuration Mode.
Please Note: The code below has been tested, and works perfectly. You will however have to make modifications to use it for your own needs.
The code is also very long, so I will explain the operation here:
When first started, the chip will run your standard code. When you quickly press the PGM button, 5 times after each other, a flag will be set to 0x0F. This will then activate the ESP Soft IP. You can then connect to this AP, and telnet to 192.168.4.1 on port 23
You will be presented with a very brief menu, where you can SET Firmware Mode, Exit or see the menu again.
This can be expanded to suit your needs
On setting Firmware Mode, the Flag 0xFF is written to EEPROM, and the ESP is restarted. upon startup, you once more connect to the generated SoftIP, and then browse to http://192.168.4.1, log in with username admin, and password admin.
You can now load and upload a .bin firmware file to the ESP. on successful upload, the flag 0x00 is once more written to EEPROM.
After restarting, the device will once more run your standard code.
Let us look at how I have done this:
The actual code will be commented with details, please read that for more info.
#include "EEPROM.h"
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <Update.h>
#define EEPROM_SIZE 64 // This can be smaller or larger, adjust to //your needs as well as the chip capabilities
int wifi_ap_on = 0; // the address for the "flag"
// Wifi for Firmware Update and Configuration Server(s)
const char* host = "ESP32/8266";
char* ssid = "--- your desired ssid ---";
char* password = "--- your password ---";
// Define two types of Servers, they will however not run at the same // time
WebServer OTAserver(80); // OTA Server Port
WiFiServer ConfigServer(23); // Configuration Server Port
String header; // HTML requests will be stored in here
// HTML Pages for Firmware Updater
/*
* These are the standard OTA examples that came with Arduino IDE
* Modify and enhance as see fit, keeping in mind that my application
* would not have access to the internet
*/
/*
* Login page
*/
const char* loginIndex =
"<form name='loginForm'>"
"<table width='20%' bgcolor='A09F9F' align='center'>"
"<tr>"
"<td colspan=2>"
"<center><font size=4><b>ESP Firmware Update Page</b></font></center><br>"
"<br>"
"</td>"
"<br>"
"<br>"
"</tr>"
"<td>Username:</td>"
"<td><input type='text' size=25 name='userid'><br></td>"
"</tr>"
"<br>"
"<br>"
"<tr>"
"<td>Password:</td>"
"<td><input type='Password' size=25 name='pwd'><br></td>"
"<br>"
"<br>"
"</tr>"
"<tr>"
"<td><input type='submit' onclick='check(this.form)' value='Login'></td>"
"</tr>"
"</table>"
"</form>"
"<script>"
"function check(form)"
"{"
"if(form.userid.value=='admin' && form.pwd.value=='admin')"
"{"
"window.open('/serverIndex')"
"}"
"else"
"{"
" alert('Error Password or Username')/*displays error message*/"
"}"
"}"
"</script>";
const char* serverIndex = "<form method='POST' action='/update' enctype='multipart/form-data'><input type='file' name='update'><input type='submit' value='Update'></form>";
// End HTML Pages
// Other variables
int Btn = 0; // The PGM button, Usually connected to GPIO0
int wifi_ap = 0; // The Running MODE
int buttonState;
int buttonValue = 0;
int lastButtonState = LOW;
int buttoncount = 0;
void setup()
{
Serial.begin(115200);
EEPROM.begin(EEPROM_SIZE); initiate EEPROM
/*
* Upload the sketch via USB the first time, and let chip run for
* a few seconds. We need to set the EEPROM location to a known
* initial value. Then comment the following 3 lines, to prevent
* the chip from overwriting the EEPROM location on next startup
*/
pinMode(Btn,INPUT);
EEPROM.write(wifi_ap_on,0x00); // comment this line after first run
EEPROM.commit();// comment this line after first run
delay(50);// comment this line after first run
wifi_ap = EEPROM.read(wifi_ap_on); // read EEPROM value into "flag"
if (wifi_ap == 0x00) {
// This will be executed on normal startup
// Place all your normal initialisation stuff here
// For example other pin configurations and
// peripheral initialisations
} else if (wifi_ap == 0xFF) { // Firmware Update MODE
// Setup for OTA Webserver.
// I have used SoftAP mode, as internet would not
// be available anyway.
WiFi.softAP(ssid,password);
IPAddress IP = WiFi.softAPIP();
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (!MDNS.begin(host)) { //http://esp32.local
Serial.println("Error setting up MDNS responder!");
while (1) {
delay(1000);
}
}
server.on("/", HTTP_GET, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/html", loginIndex);
});
server.on("/serverIndex", HTTP_GET, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/html", serverIndex);
});
/*handling uploading firmware file */
server.on("/update", HTTP_POST, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
ESP.restart();
}, []() {
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
Serial.printf("Update: %s\n", upload.filename.c_str());
if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
/* flashing firmware to ESP*/
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_END) {
if (Update.end(true)) { //true to set the size to the current progress
/*
* NOTE : We clear the EEPROM Flag after a sucessful Firmware Update
* That way, the chip will stay in OTA mode on a failure.
*/
EEPROM.write(wifi_ap_on,0x00);
EEPROM.commit();
delay(5);
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
} else {
Update.printError(Serial);
}
}
});
OTAserver.begin();
} else if (wifi_ap == 0x0F) { // End of Mode 0xFF
//This is Configuration Mode
//This code will normally not be executed,
//As the device exits Config mode on restart, and
//We dont write Mode 0x0F to EEPROM
} // End of Mode 0x0F
} // End of Void Setup
void loop()
{
CountButton(); // Count Button Presses
if (buttoncount == 5) { // Activate Configuration Mode
buttoncount = 0;
StartSoftAP(); // Start a SoftAP
}
if (wifi_ap == 0x0F) { // Code for the Config Server (TELNET Style)
WiFiClient client = ConfigServer.available(); // Start Config
if (client) {
String currentLine = "";
client.println();
client.println("Telnet Server - Welcome");
client.println();
client.println("Press <Enter> to Login");
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n') {
if (currentLine.length() == 0) {
} else {
// Process Commands
if (currentLine == "Master123abc") {
telnetLogin == true;
client.println();
client.println();
client.println("****** MENU ******");
client.println();
client.println("MENU - SHOW THIS MENU");
client.println("RUF - Enable Firmware Update Mode");
client.println("EXIT - Exit Menu and RESTART");
client.println();
client.print("Command> ");
currentLine = "";
}
// Show MENU
if (currentLine == "MENU") {
client.println();
client.println();
client.println("****** MENU ******");
client.println();
client.println("MENU - SHOW THIS MENU");
client.println("RUF - Enable Firmware Update Mode");
client.println("EXIT - Exit Menu and RESTART");
client.println();
client.print("Command> ");
}
// EXIT AND RESTART
if (currentLine == "EXIT") {
client.println();
client.println("Restarting in 5 Seconds");
delay(5000);
client.stop();
ESP.restart();
}
// Upgrade Frmware Mode
if (currentLine == "RUF") {
client.println();
client.println("Enable Firmware Update Mode");
client.print("Connect to SoftAP :");
client.println(ssid);
client.println("Browse to http://192.168.4.1 ");
client.println("Login with Username admin, Password admin");
EEPROM.write(wifi_ap_on,0xFF); // Set Flag in EEPROM
EEPROM.commit();
delay(5000);
client.stop();
ESP.restart(); // Restart Device
}
// End Process commands
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
} // End if client Available
} // End While Client Connected
client.stop();
} // END Client
} // END MODE 0x0F
if (wifi_ap == 0xFF) {
OTAserver.handleClient(); // Handle OTA Server
delay(1);
}
if (wifi_ap == 0x00 )
// Normal code goes here , to be executed every cycle
{
} // End of Void Loop
void StartSoftAP() // Start SoftAP, for Configuration Server
{
WiFi.mode(WIFI_OFF);
WiFi.softAP(ssid,password);
IPAddress IP = WiFi.softAPIP();
ConfigServer.begin();
wifi_ap = 0x0F; // Set Flag to Config Mode
// Note that we dont write it in EEPROM
}
void CountButton() // Counts Button Presses, to enable Config Mode
{
//int buttonState;
//int buttonValue = 0;
//int lastButtonState = LOW;
//int buttoncount = 0;
//unsigned long lastDebounceTime = 0;
//int debounceDelay = 50;
buttonValue = digitalRead(Btn);
if (buttonState != lastButtonState) {
if (buttonValue == LOW) {
buttoncount++;
if (buttoncount > 10) buttoncount = 0;
}
}
delay(50);
lastButtonState = buttonValue;
// End Debouncing
if (buttoncount == 5) digitalWrite(LED,HIGH);
}
This concludes a very long piece of code, question are welcome. Thank you
Today I will show you another useful IO Expander chip, The MCP23017. This chip, although similar to the PCF8475, which I have already covered in a previous article, has many additional features that may make it a very attractive solution when you need some more extra GPIO pins for a big project…
Features
Let us look at some of the features of this chip
16-Bit Remote Bidirectional I/O Port:
I/O pins default to input • High-Speed I2C Interface (MCP23017):
100 kHz
400 kHz
1.7 MHz • High-Speed SPI Interface (MCP23S17):
10 MHz (maximum) • Three Hardware Address Pins to Allow Up to Eight Devices On the Bus • Configurable Interrupt Output Pins:
Configurable as active-high, active-low or open-drain • INTA and INTB Can Be Configured to Operate Independently or Together • Configurable Interrupt Source:
Interrupt-on-change from configured register defaults or pin changes • Polarity Inversion Register to Configure the Polarity of the Input Port Data • External Reset Input • Low Standby Current: 1 µA (max.) • Operating Voltage:
1.8V to 5.5V @ -40°C to +85°C
2.7V to 5.5V @ -40°C to +85°C
4.5V to 5.5V @ -40°C to +125°C
MCP23017 Pinout Diagram
The sixteen I/O ports are separated into two ‘ports’ – A (on the right) and B (on the left. Pin 9 connects to 5V, 10 to GND, 11 isn’t used, 12 is the I2C bus clock line (Arduino Uno/Duemilanove analogue pin 5, Mega pin 21), and 13 is the I2C bus data line (Arduino Uno/Duemailnove analogue pin 4, Mega pin 20).
External pull-up resistors should be used on the I2C bus – in our examples we use 4.7k ohm values. Pin 14 is unused, and we won’t be looking at interrupts, so ignore pins 19 and 20. Pin 18 is the reset pin, which is normally high – therefore you ground it to reset the IC. So connect it to 5V!
Finally we have the three hardware address pins 15~17. These are used to determine the I2C bus address for the chip. If you connect them all to GND, the address is 0x20. If you have other devices with that address or need to use multiple MCP23017s, see figure 1-2 in the datasheet.
You can alter the address by connecting a combination of pins 15~17 to 5V (1) or GND (0). For example, if you connect 15~17 all to 5V, the control byte becomes 0100111 in binary, or 0x27 in hexadecimal.
It is also available on a convenient breakout PCB, for about $USD0.80 from AliExpress
MCP23017 on Breakout PCB – Back
MCP23017 on Breakout PCB – Front
Please Note: THIS BREAKOUT PCB IS NOT SUITED FOR USE ON A BREADBOARD. YOU WILL SHORT OUT VCC AND GROUND AS WELL AS ALL THE IO PINS IF YOU TRY TO USE IT ON A BREADBOARD.
As you can see, the pins are however very clearly labelled, and thus easy to use. I have also purposely soldered my header pins “the wrong way round” to prevent using it on a breadboard, as this will short out Vcc to Ground!
Having interrupt outputs is one of the most important features of the MCP23017, since the microcontroller does not have to continuously poll the device to detect an input change. Instead an interrupt service routine can be used to react quickly to an input change such a key press…
To make life even easier each GPIO input pin can be configured with an internal pullup (~100k) and that means you won’t have to wire up external pull up resistors for keyboard input. You can also mix and match inputs and outputs the same as any standard microcontroller 8 bit port.
Addressing
The 23017 has three input pins to allow you to set a different address for each attached MCP23017.
The above corresponds to a hardware address for the three lines A0, A1, A2 corresponding to the input pin values at the IC. You must set the value of these hardware inputs as 0V or (high) volts and not leave them floating otherwise they will get random values from electrical noise and the chip will do nothing!
The four left most bits are fixed a 0100 (specified by a consortium who doles out address ranges to manufacturers).
So the MCP23017 I2C address range is 32 decimal to 37 decimal or 0x20 to 0x27 for the MCP23017.
Please note: The addresses are the same as those for the PCF8475. You must thus be careful if you use these two devices on the same i2c bus!
MCP23017 Non interrupt registers
IODIR I/O direction register
For controlling I/O direction of each pin, register IODIR (A/B) lets you set the pin to an output when a zero is written and to an input when a ‘1’ is written to the register bit. This is the same scheme for most microcontrollers – the key is to remember that zero (‘0’) equates to the ‘O’ in Output.
GPPU Pullup register
Setting a bit high sets the pullup active for the corresponding I/O pin.
OLAT Output Latch register
This is exactly the same as the I/O port in 18F series PIC chips where you can read back the “desired” output of a port pin whether or not the actual state of that pin is reached. i.e. consider a strong current LED attached to the pin – it is easily possible to pull down the output voltage at the pin to below the logic threshold i.e. you would read back a zero if reading from the pin itself when in fact it should be a one. Reading the OLAT register bit returns a ‘one’ as you would expect from a software engineering point of view.
IPOL pin inversion register
The IPOL(A/B) register allows you to selectively invert any input pin. This reduces the glue logic needed to interface other devices to the MCP23017 since you won’t need to add inverter logic chips to get the correct signal polarity into the MCP23017.
It is also very handy for getting the signals the right way up e.g. it is common to use a pull up resistor for an input so when a user presses an input key the voltage input is zero, so in software you have to remember to test for zero.
Using the MCP23017 you could invert that input and test for a 1 (in my mind a key press is more equivalent to an on state i.e. a ‘1’) however I use pullups all the time (and uCs in general use internal pullups when enabled) so have to put up with a zero as ‘pressed’. Using this device would allow you to correct this easily.Note: The reason that active low signals are used everywhere is a historical one: TTL (Transistor Transistor Logic) devices draw more power in the active low state due to the internal circuitry, and it was important to reduce unnecessary power consumption – therefore signals that are inactive most of the time e.g. a chip select signal – were defined to be high. With CMOS devices either state causes the same power usage so it now does not matter – however active low is used because everyone uses it now and used it in the past.
SEQOP polling mode : register bit : (Within IOCON register)
If you have a design that has critical interrupt code e.g. for performing a timing critical measurement you may not want non critical inputs to generate an interrupt i.e. you reserve the interrupt for the most important input data.
In this case, it may make more sense to allow polling of some of the device inputs. To facilitate this “Byte mode” is provided. In this mode, you can read the same set of GPIOs using clocks but not needling to provide other control information. i.e. it stays on the same set of GPIO bits, and you can continuously read it without the register-address updating itself. In non-byte mode, you either have to set the address you read from (A or B bank) as control input data.
Now to examine how to use the IC in our sketches.
As you should know by now most I2C devices have several registers that can be addressed. Each address holds one byte of data that determines various options. So before using we need to set whether each port is an input or an output. First, we’ll examine setting them as outputs. So to set port A to outputs, we use:
Wire.beginTransmission(0x20);
Wire.write(0x00); // IODIRA register
Wire.write(0x00); // set all of port A to outputs
Wire.endTransmission();
Then to set port B to outputs, we use:
Wire.beginTransmission(0x20);
Wire.write(0x01); // IODIRB register
Wire.write(0x00); // set all of port B to outputs
Wire.endTransmission();
So now we are in void loop() or a function of your own creation and want to control some output pins. To control port A, we use:
Wire.beginTransmission(0x20);
Wire.write(0x12); // address port A
Wire.write(??); // value to send
Wire.endTransmission();
To control port B, we use:
Wire.beginTransmission(0x20);
Wire.write(0x13); // address port B
Wire.write(??); // value to send
Wire.endTransmission();
… replacing ?? with the binary or equivalent hexadecimal or decimal value to send to the register.
To calculate the required number, consider each I/O pin from 7 to 0 matches one bit of a binary number – 1 for on, 0 for off. So you can insert a binary number representing the status of each output pin. Or if binary does your head in, convert it to hexadecimal. Or a decimal number.
So for example, you want pins 7 and 1 on. In binary that would be 10000010, in hexadecimal that is 0x82, or 130 decimal. (Using decimals is convenient if you want to display values from an incrementing value or function result).
For example, we want port A to be 11001100 and port B to be 10001000 – so we send the following (note we converted the binary values to decimal):
Wire.beginTransmission(0x20);
Wire.write(0x12); // address port A
Wire.write(204); // value to send
Wire.endTransmission();
Wire.beginTransmission(0x20);
Wire.write(0x13); // address port B
Wire.write(136); // value to send
Wire.endTransmission();
A complete Example
// pins 15~17 to GND, I2C bus address is 0x20
#include "Wire.h"
void setup()
{
Wire.begin(); // wake up I2C bus
// set I/O pins to outputs
Wire.beginTransmission(0x20);
Wire.write(0x00); // IODIRA register
Wire.write(0x00); // set all of port A to outputs
Wire.endTransmission();
Wire.beginTransmission(0x20);
Wire.write(0x01); // IODIRB register
Wire.write(0x00); // set all of port B to outputs
Wire.endTransmission();
}
void binaryCount()
{
for (byte a=0; a<256; a++)
{
Wire.beginTransmission(0x20);
Wire.write(0x12); // GPIOA
Wire.write(a); // port A
Wire.endTransmission();
Wire.beginTransmission(0x20);
Wire.write(0x13); // GPIOB
Wire.write(a); // port B
Wire.endTransmission();
}
}
void loop()
{
binaryCount();
delay(500);
}
Using the pins as inputs
Although that may have seemed like a simple demonstration, it was created show how the outputs can be used. So now you know how to control the I/O pins set as outputs. Note that you can’t source more than 25 mA of current from each pin, so if switching higher current loads use a transistor and an external power supply and so on.
Now let’s turn the tables and work on using the I/O pins as digital inputs. The MCP23017 I/O pins default to input mode, so we just need to initiate the I2C bus. Then in the void loop() or other function all we do is set the address of the register to read and receive one byte of data.
// pins 15~17 to GND, I2C bus address is 0x20
#include "Wire.h"
byte inputs=0;
void setup()
{
Serial.begin(9600);
Wire.begin(); // wake up I2C bus
}
void loop()
{
Wire.beginTransmission(0x20);
Wire.write(0x13); // set MCP23017 memory pointer to GPIOB address
Wire.endTransmission();
Wire.requestFrom(0x20, 1); // request one byte of data from MCP20317
inputs=Wire.read(); // store the incoming byte into "inputs"
if (inputs>0) // if a button was pressed
{
Serial.println(inputs, BIN); // display the contents of the GPIOB register in binary
delay(200); // for debounce
}
}
Other Libraries
You can also download and install the MCP23017 Library from Adafruit for the Arduino IDE. This library will make using this chip even easier… I will discuss this library in another post
Using a matrix keypad is a very easy way to add multiple control buttons to a project, be it to enter a password, or to control different devices. These keypads do unfortunately have some serious flaws (in my view anyway)
1) They are usually of extremely low quality ( especially some of the membrane types from China). This means they dont last very long. 2) A typical 4×4 Matrix keypad will require 8 of your precious IO pins for itself.
These two flaws can however easily be solved, if we use a bit of technology, and are willing to to a bit of simple circuit construction by ourselves.
What does this mean ? Most of us makers will inevitably have a piece of proto-board or strip-board lying around, as well as a few momentary push-button switches. These can easily be used to make out own, much more reliable keypad. Let us look at the circuit
Circuit diagram for a 4×4 Matrix Keypad
As we can see, to build a 4×4 matrix keypad, we will need 16 momentary switches. These are connected together as shown above. You can then interface it with your favourite micro-controller to read the key(s) pressed…
This definitely solves the first of my problems, but we still need 8 pins to control this keypad… or do we? No, we don’t, we need only 2 pins. That is to say if we use one of those PCF8574 I2C IO port expander modules. They are much more reliable, as well as quite cheap as well. all depending on where you buy them from, and how long you are willing to wait for shipping 🙂
Let us see how to connect the keypad to the I2C Module
a 4×4 Membrane Matrix Keypad with PCF8574 I2C port expander moduleConnecting the two together, note that we do not connect the INT pinConnect Power (VCC, GND and I2C lines Connect to Arduino or your preferred microcontroller. We have used Arduino Uno, Note that you can also connect the I2C to A4 (SDA) and A5(SCL) if you prefer.
Now, we need to install some libraries
The first one is the actual Keypad library, you can download it from the link below
#include <Key.h>
#include <Keypad.h>
#include <Keypad_I2C.h>
#define I2CADDR 0x26 // Set the Address of the PCF8574
const byte ROWS = 4; // Set the number of Rows
const byte COLS = 4; // Set the number of Columns
// Set the Key at Use (4x4)
char keys [ROWS] [COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// define active Pin (4x4)
byte rowPins [ROWS] = {0, 1, 2, 3}; // Connect to Keyboard Row Pin
byte colPins [COLS] = {4, 5, 6, 7}; // Connect to Pin column of keypad.
// makeKeymap (keys): Define Keymap
// rowPins:Set Pin to Keyboard Row
// colPins: Set Pin Column of Keypad
// ROWS: Set Number of Rows.
// COLS: Set the number of Columns
// I2CADDR: Set the Address for i2C
// PCF8574: Set the number IC
Keypad_I2C keypad (makeKeymap (keys), rowPins, colPins, ROWS, COLS, I2CADDR, PCF8574);
void setup () {
Wire .begin (); // Call the connection Wire
keypad.begin (makeKeymap (keys)); // Call the connection
Serial.begin (9600);
}
void loop () {
char key = keypad.getKey (); // Create a variable named key of type char to hold the characters pressed
if (key) {// if the key variable contains
Serial.println (key); // output characters from Serial Monitor
}
}
Upload this to your Arduino device and enjoy. This sketch can also be adapted for 1×4, and 4×3 keypads, and with a little modification, will also work perfectly on ESP32 or ESP8266 as well…