As part of my recent ESP-12E I2C Base Board project, I designed an ATMega 328P Based PWM controller card, that can be used as an add-on card with the existing project, or standalone as a custom Arduino Nano compatible development board.
What is on the PCB?
The PWM controller card contains standard Arduino Nano circuitry running at 16MHz, without the USB to Serial converter, as well as a 3v to 5v level converter on the I2C port ( A4 and A5 ), as well as another 12v to 5v level converter, with a build in resistor-divider circuit, used to drive a 12v blower with 3.3v PWM control circuitry.
All analog inputs are broken out to make attaching additional sensors easier.
All the other unused GPIO pins are also broken out, either directly to headers on the PCB (D6~,D7,D8,D9~), D11,D12,D12 (ISCP Header) and D3 ( Marked RPM on the Fan Header)
Most of these pins are also additionally broken out onto the 2x20p female header at the bottom of the card ( See schematic for more details)
The board is designed to be powered from 12v DC (via the VIN pins on the 2x20p header) which is internally regulated down to 5v via an LDO voltage regulator.
External 3.3v should also be supplied to the 2x20Pin header to enable the I2C level converters on the same header. I2C is not directly broken out onto the PCB in this version of the PCB.
A reset button, and power led, as well as the standard led on D13 is also provided.
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.
https://vimeo.com/723212010
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.
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.
Many of us have old toys laying around the house, they belong to our children or the children of our friends. In this article, I will attempt to show you how to give an old toy car new life, as well as hopefully teach a child a few interesting things with electronics.
The inspiration for this article comes from my friend’s 7-year-old boy, who, way too clever for his age, always has a lot of very interesting questions. His mother and I have thus decided to do an experiment: “Let us try to teach him Arduino programming, so he can start to make his own toys”
Obviously, the challenges in this venture are many… To name a few: The boy speaks only Thai, so English is a no-go. Soldering is out of the question, due to his age, as well as safety issues – All teaching will have to be done on a breadboard.
My challenges apart, this is a project that many people would want to attempt, so it is important to start with a bit of theory.
Controlling a DC Motor from a microcontroller
DC motors, like those found in toy cars, are inductive loads, and that means that they induce electromagnetic fields when switched on or off. These EMF fields can damage your sensitive microcontroller quite easily. Another thing to remember is that your typical microcontroller can only source or sink in the region of 25mA to 50mA of current, not quite enough to drive a motor, let alone a toy car.
Directional control of the motor
In our toy car, we would definitely want the driving motor to be able to change direction, meaning spin forwards or backwards, thereby changing the direction that the car is travelling. This is achieved by using a circuit called an H-Bridge. In this circuit, four transistors, either BJTs or MOSFETs are arranged in a particular way to allow us to change the direction that the motor spins by changing certain logic signals.
Implementing an H-Bridge with switches
In the picture above, we simulate the H-Bridge circuit using slide switches in order to explain the method of operation. It should be clear that the direction is changed by switching on diagonally opposite switches.
Driving a motor with a transistor
In the picture above, we implement a simple, one-directional motor control circuit using a single transistor. This circuit still has the limitation that the motor can only spin in a single direction.
Half of an H-Bridge
In the circuit above, we combine the two motor driver circuits (with PNP and NPN transistor ) to complete one half of the H-Bridge circuit. This circuit still has the limitation that we can only spin the motor in a single direction.
The completed H-Bridge circuit
In the picture above, we added another half H-Bridge to complete the circuit. We will thus have 2 PNP and 2 NPN transistors, which form the completed circuit. This circuit will give us full bi-directional control of the motor. We can also control the speed of the motor if we apply a suitable PWM signal to the bases of the NPN transistors – we do need to be careful of SHOOT THROUGH and shorts though.
My proposed Motor Driving Circuit
the inside of the toy car, without the old broken circuit-board
In the picture above, we can clearly see that there is not a lot going on inside this toy car. An On-Off switch is connected to the battery compartment, and two wires go to the drive- and steering motor.
Interfacing this car to a microcontroller is thus going to require two separate H-Bridge circuits. One for the drive motor, and the second for the steering.
Dual H-Bridge Circuit diagram
I have designed the circuit above to control both motors of the toy car, the control signals are simplified to 2 per H-Bridge, and a common PWM signal to control speed.
Manufacturing the PCB
The PCB for this project is currently on its way from China, after having 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 be manufactured.
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.
There are many CAN-Bus modules available for purchase to the DIY Electronics Enthusiast and the Maker community. Our Level Converted CAN-BUS module is different. Where the standard modules are all 5v devices, ours are level converted, allowing you to interface it with 3v and 5v microcontrollers, the choice is yours…
Level converted CAN-Bus ModuleLevel Converted CAN-BUS Module together with standard CAN-BUS breakout for comparison.
The Schematic
How does it work? / How do I use it?
The Level Converted CAN-BUS Module is based on the MCP2515 CAN Controller from Microchip, with the TJA1050 CAN Tranceiver used for communicating with the CAN-Bus. These two chips are extremely cheap and easy to get hold of, but they are also one of the main reasons for the redesign of the module.
While the MCP2515 is useable with a voltage range of 2.5v to 5v, the TJA1050 is not. When using the commercially available CAN-Bus modules, this limits you to using 5v microcontrollers, or for the more informed, using level converters in-between to translate back and forth to the desired logic levels.
The MCP2515 is an SPI device, and in my opinion, having long wires on an SPI bus is not always the best way of doing things, due to ringing and other undesirable interference. Having to add a level converter module into this already questionable setup, can add a lot of other undesirable effects.
I have thus decided to design and manufacture my own module, with 5 level converters directly on the PCB, thus reducing the length of connecting wires, as well as reducing complexity.
Using the device is now as easy as providing a 5v voltage source, as well as an additional 3v source if you need the level converters, and connecting your microcontroller to the appropriately marked logic side of the module.
A jumper at H1 can be set/unset to enable the 120ohm ballast resistor that is needed on the CAN-Bus for very short distance connections.
Example connection to an Arduino
Use the 5v logic side, and power the module with 5v and ground. You do not need a 3v power source. Connect the pins as follows:
CS pin to Arduino Pin 10 SO to the MISO pin on the Arduino Pin 12 SI to the MOSI pin on the Arduino, Pin 11 SCK to the SCK pin on the Arduino, Pin 13 INT to an interrupt capable pin on the Arduino, usually pin 2 or 3
Example connection to an ESP32 module
Provide a 5v as well as 3v power source with a common ground connection. Connect your logic to the 3v logic side of the PCB Module.
CS pin to GPIO2 SO to the MISO pin, GPIO19 SI to the MOSI pin, GPIO23 SCK to the SCK pin, GPIO18 INT to an interrupt capable pin on the ESP32
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.
In part one of this series, I took a look at some of my experiments using different voltage regulators, to design and build the Remote Alarm Transceiver prototype, and also mentioned that I will be looking at a single chip logic converter solution. In this (hopefully short) post, I will take a detailed look at that logic converter chip, as well as show you how it is used.
Some of the features of the device is listed below:
• AEC-Q100 Qualified for Automotive Applications – Device Temperature Grade 1: –40°C to 125°C – Device HBM ESD Classification Level 2 – Device CDM ESD Classification Level C6 • No direction-control signal needed • Maximum data rates – 110 Mbps (push pull) – 1.2 Mbps (open drain) • 1.4 V to 3.6 V on A port and 1.65 V to 5.5 V on B port (VCCA ≤ VCCB) • No power-supply sequencing required – either VCCA or VCCB can be ramped first • Latch-up performance exceeds 100 mA per JESD 78, Class II • ESD protection exceeds JESD 22 (A Port) – 2000-V human body model (A114-B) – 1000-V charged-device model (C101) • IEC 61000-4-2 ESD (B port) – ±8 kV contact discharge – ±6 kV Air-gap discharge
Datasheet description:
This device is an 8-bit non-inverting level translator that uses two separate configurable power-supply rails. The A port tracks the VCCA pin supply voltage. The VCCA pin accepts any supply voltage between 1.4 V and 3.6 V. The B port tracks the VCCB pin supply voltage. The VCCB pin accepts any supply voltage between 1.65 V and 5.5 V. Two input supply pins allows for low Voltage bidirectional translation between any of the 1.5 V, 1.8 V, 2.5 V, 3.3 V, and 5 V voltage nodes. When the output-enable (OE) input is low, all outputs are placed in the high-impedance (Hi-Z) state. To ensure the Hi-Z state during power-up or power-down periods, tie OE to GND through a pull-down resistor. The minimum value of the resistor is determined by the current-sourcing capability of the driver.
Typical Application:
Reference Design / Typical Application for the TXS0108E
My Thoughts:
I really like the tri-state (high impedance) mode of the chip, as it allows for isolation between the different voltage level circuits, for example, If I were to communicate on a 5v SPI bus, to another device, I can for instance put the chip in Tri-state mode, and not worry about stray signals interfering from the 3v side.
On the downside, the chip is very small, which makes it a real challenge to solder by hand. On the speed side, It is also not quite as fast as my usual MOSFET based circuitry. It does however do the job it was designed for quite well.
Updated Circuit
Integrating the chip into the existing Remote Alarm Transceiver circuit is very easy, allowing us to replace almost all of the Mosfet-based Logic level converters. We do still need a few of them, as we have only 8 bidirectional channels on the TXS0108.
Schematics
Some Notes on the schematics:
A battery level monitor is connected through a voltage divider, with a MOSFET as a switch to the A0 pin. The voltage divider is set up for a 12v DC input source. The MOSFET is controlled from the D6 Pin.
The reason that I did this is, that I found some parasitic voltage leakage through the A2D converter in a previous design, reducing battery life. My hope is that by only reading battery level when the MOSFET is on, there can be an increase in battery life ( Taking into consideration that the Voltage regulators are not very efficient, it won’t really amount to a big gain unless I switch to an SMPS in the future. )
The PCB
The PCB
In the picture above, we can see the completed PCB (The relay and buzzer were not populated yet)
Manufacturing the PCB
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.
As part of my experiments with LoRa and the easy to use ATMega328P, I have recently designed quite a few LoRa based projects. In this final 2 part series, I will look at two additional projects, part of a Remote Alarm Transceiver, where I experimented with a changing a few things:
– Using LM317G adjustable voltage regulators. – Replacing my standard N-MOS based logic level converters with a dedicated chip.
LoRa Remote Alarm Transmitter – with Onboard Relay putout and two sensor inputs
How does this differ from my other LoRa Based projects?
The PCB presented above does in fact not really differ a lot from any of my existing LoRa based projects. However, there are a few subtle changes, mainly experimental changes, brought on by factors such as component availability and an attempt to reduce component counts and board size.
The first of these changes is using the LM317G voltage regulator, in the place of my usual LM1117 3.3 and 5.0 LDO regulators.
The LM317 is an old device, It has been on the market for a long time. It can supply up to 1.5A of current, and a single device can be configured to supply a wide range of different voltages by just changing two resistors. This seemed quite attractive to me, as it is getting quite difficult to reliably get quite a few components on time, and with decent pricing in the post-Covid-19 world.
The second major change would be moving away from my existing N-Mos based Logic converter setup, where I used the BSS138 and 10K resistors as logic converters. This setup works perfectly, but it has the drawback of requiring quite a lot of components. for example:
To provide logic conversion to an RA-02 module, with access to all the IO Lines (GPIO0-5 included) required 12 BSS138 Mosfets and 24 10k resistors. This is quite a lot of components. A dedicated logic converter chip would thus be a much more attractive solution.
Driver circuitry for sensor Inputs, consisting of a simple transistor input, and an optically isolated Relay output completes the circuit.
Using the LM317
The output voltage of the LM317 is typically set using two resistors, with a suitable current rating, using the following Formula
VOUT = 1.25 * ( 1 + R2/R1 )
It is also common to use a variable resistor at R2, to have fine control over the output voltage. This is due to the fact that stock resistor values do not always give you the exact voltage you require. You should also take into account that using a 5% resistor will be less accurate than a 1% resistor.
The grid below is a list of common stock resistor values for R1/R2, with the resulting voltage produced.
R1 vs R2 Grid for use in selecting fixed output voltage
R2\R1
150
180
220
240
270
330
370
390
470
68
1.82
1.72
1.64
1.60
1.56
1.51
1.48
1.47
1.43
82
1.93
1.82
1.72
1.68
1.63
1.56
1.53
1.51
1.47
100
2.08
1.94
1.82
1.77
1.71
1.63
1.59
1.57
1.52
120
2.25
2.08
1.93
1.88
1.81
1.70
1.66
1.63
1.57
150
2.50
2.29
2.10
2.03
1.94
1.82
1.76
1.73
1.65
180
2.75
2.50
2.27
2.19
2.08
1.93
1.86
1.83
1.73
220
3.08
2.78
2.50
2.40
2.27
2.08
1.99
1.96
1.84
240
3.25
2.92
2.61
2.50
2.36
2.16
2.06
2.02
1.89
270
3.50
3.13
2.78
2.66
2.50
2.27
2.16
2.12
1.97
330
4.00
3.54
3.13
2.97
2.78
2.50
2.36
2.31
2.13
370
4.33
3.82
3.35
3.18
2.96
2.65
2.50
2.44
2.23
390
4.50
3.96
3.47
3.28
3.06
2.73
2.57
2.50
2.29
470
5.17
4.51
3.92
3.70
3.43
3.03
2.84
2.76
2.50
560
5.92
5.14
4.43
4.17
3.84
3.37
3.14
3.04
2.74
680
6.92
5.97
5.11
4.79
4.40
3.83
3.55
3.43
3.06
820
8.08
6.94
5.91
5.52
5.05
4.36
4.02
3.88
3.43
1000
9.58
8.19
6.93
6.46
5.88
5.04
4.63
4.46
3.91
1200
11.25
9.58
8.07
7.50
6.81
5.80
5.30
5.10
4.44
1500
13.75
11.67
9.77
9.06
8.19
6.93
6.32
6.06
5.24
1800
16.25
13.75
11.48
10.63
9.58
8.07
7.33
7.02
6.04
2200
19.58
16.53
13.75
12.71
11.44
9.58
8.68
8.30
7.10
2700
23.75
20.00
16.59
15.31
13.75
11.48
10.37
9.90
8.43
3300
28.75
24.17
20.00
18.44
16.53
13.75
12.40
11.83
10.03
As you can see from the table above, using stock resistors, the output voltage is reasonably accurate, but it is quite obvious that you will need a potentiometer to get exact values. Another issue will definitely be heat dissipation. In my PCB design, I have used the SOT-223 package of the component, with a PCB heatsink, built directly into the layers. With the LM1117 LDO regulators, these work extremely well.
Logic Level Conversion
In this design, I used my standard Logic Level conversion circuit, comprised of a BSS138 N-Mos with two 10 k resistors. This circuit, although a bit cumbersome with lots of components if you need many logic converters, is very stable, and functions extremely well.
Conclusion
This circuit was designed as a two-part prototype, with the goal of experimenting with different voltage regulators, and in part 2, with a single chip 8 channel logic converter. As such, I do not feel comfortable releasing the full schematics to you at this stage, do so anyway in the interest of learning. The circuit works, but there are many issues with the regulators:
– Overheating at input voltages above 8.0v The PCB heatsink will have to be improved, or even a different package for the LM317 with the possibility to attach an external heatsink.
– The voltages do not seem stable, especially on the 3.3-volt side.
Manufacturing the PCB
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.
CAN-Bus allows us to add a lot of devices to a single bus (theoretically up to 127, with a practical limit of about 110). It was logical to decide to use CAN-BUS for communication between my LoRa-CAN Module and remote nodes in my ongoing farm telemetry system. In a previous project, I introduced the LoRa-CAN Gateway, which will be used to send/receive messages from the master control unit, the SX127x-RA-02-Module.
Today, I will focus on the design of the node device, an Arduino compatible CAN Relay Module PCB, with a built-in, CAN Controller and Transceiver ( MCP2515 and TJA1050 ), and the option to be powered from a LiPo battery (18650 or Single 3.7v Lipo cell), with charging provided by an MH-CD42 Module, similar to that used in the SX128x project mentioned above. The Module can also be powered directly from up to 12v DC by placing Jumper H2 in the V-REG position…
As mentioned in a previous project, the MH-CD42 module can provide up to 2A of current to charge a battery and provide 5V DC on a direct bypass circuit to power the rest of the circuit while the battery is being charged. This feature makes it ideal for my intended use, as I would only be needing battery power at night when the off-grid solar powered inverter is not actively charging the main batteries, and providing 220v AC.
The CAN Relay Module PCB was designed to be as compact as possible, with a total footprint of the standard Arduino UNO. As space was at a premium, and to ensure that there are the absolute minimum additional components that will consume power when operating from the battery, I have not included any USB-to-Serial converters onboard. Firmware can be uploaded with an AVRASp, USBASP, Arduino as ISP or even an external USB-to-Serial converter ( providing that you load a bootloader into the Atmega Chip).
All of the unused GPIO pins on the ATMEGA chip is broken out into headers, with the exception of D10 ( which is used as CE/SS on the MCP2515 CAN controller), D9 which is connected to the Interrupt from the MCP2515, and D4, which is used to control the onboard Relay.
The MH-CD42 Module, and LiPo or 18650 Battery are completely optional, The device can function without these, by moving the H2 jumper to the VREG position as already mentioned above. This will divert the DC Voltage(Up to 12v) from the DC input adapter to the build-in 5v LDO voltage regulator to power the device.
If you place the H2 Jumper in the VBAT position, you need to install the MH-CD42 module to provide power to the rest of the PCB, as well as keep the LiPo battery or 18650 Cell charged…
Please NOTE:
When the MH-CD42 module is in use, the total DC input voltage through the DC input adapter SHOULD NOT exceed 5.5v DC! This is a limitation in the operating parameters of the MH-CD42 module. Not paying attention to this will result in damage to the MH-CD42 Module.
You could also power the module with REGULATED 5v DC, directly from any 5v header pin. Please note that in this case,
1) The battery won’t be charged.
2) The LDO regulator will not be in operation.
The Microcontroller
The CAN Relay Module can use any of the ATMEGA8a/88/168/328 AVR microcontrollers, as the pinout is identical. I believe this is an advantage, especially with the current chip prices, where my last quote for an ATMEGA328P-AU was 69$USD!!!! This is in comparison to the 4 to 5 USD each for an ATMEGA8a or ATMEGA168 ( We must also remember though that the 328p is very well known, and thus have higher demand. The 8a/88 or 168 are less well known, have much less memory and flash area, and will thus be cheaper. A word of action though, I had strange issues with I2C on the Atmega8a with Mini core, to such an extent that I2C does not work at all?
If you plan to use I2C on this board, install a 328p right from the start, and save yourself a lot of headaches!
This is the Relay driver circuit. As you can see, it is optically isolated, and active LOW. This means that you have to pull D4 LOW to energise the relay. Also note that, although the relay is optically isolated from the microcontroller, the coil is NOT galvanically ISOLATED. The load, which is magnetically switched, will be truly galvanically isolated, providing of course that you don’t do something silly like using a common ground to the PCB as a common on the relay…
This is the Processor and Power-supply schematic. The circuit is basically a standard Arduino Nano, with modifications for the CAN Controller on the next schematic page. Note that the MH-CD42 is not shown on the schematic. The VBAT net connects directly to the VIN pin on the unit, with the BAT net connecting to the positive of the battery. VOUt-5v from the module goes directly to the 5v net. All grounds are commoned.
This is the CAN Controller and Transceiver circuitry. The MCP2515 connects directly to the SPI bus on the microcontroller via D13, D12 and D11, with D10 as CE/SS and D9 as Interrupt or IRQ. It is important to note that although the MCP2515 is a 3v capable device, the TJA1050 Can transceiver is 5v only. This prevents us from running the PCB at 3v unless, of course, we change the TJA1050 out for a 3v capable device…
As I am currently working on the final integration of the two modules, I am not yet ready to publish my final code, showing the operation between the CAN-Bus and LoRa-to-CAN Gateway device in this post. Once I am happy that all issues are definitively sorted out, I will publish my code.
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.
After quite a few experiments, and playing with a lot of ideas, we have finally produced and tested an almost final prototype for the MCU-8266-12E IoT Controller Port Extender Card. While the baseboard already has quite a lot of free GPIO pins for additional sensors and devices, It did however have quite a few issues, namely a lack of sufficient Power outputs, difficulty access to the I2C bus, as well as only 2 relay outputs. Granted that you do have access to unused pins on the PCF8574 Port Extender, We nonetheless decided that an add-on card would definitely make sense to allow this device to really be more useable.
While looking at various ideas for this card, the most flexible seemed to be the APE Protocol device as documented in ESPHome. They used a standard Arduino board for that, but we decided that, after testing it with an Arduino Nano, since it seems to work well, let us just design a dedicated PCB. It also looks much better as well 🙂
The beginning of the APE (Arduino Port Extender) Device. MCU-8266-12E with Arduino Nano (left) and MCU-8266-12E with the newly designed APE Card (right)
Some Features (Aside from being a fully functional Arduino clone as well)
1). Dedicated LDO Regulators for 5v and 3.3v (800mA each), with jumpers to switch them on or off (receive power only from the IoT Motherboard). 2.) Dedicated Logic Level Converter on the I2C Bus ( This is sort of very much needed 🙂 The Atmega 328P-AU is running at 5v on this device, to enable it to run at 16Mhz.. and the ESP8266 on the Motherboard is a 3.3v device..
There are also 3x 3.3V I2C Headers, complete with 3.3v and Ground, as well as a single 5v I2C header 3). 8 Analog Inputs ( While practically you can only use 6 of these if you use I2C ) 4). Voltage Divider provided on A0 to measure VIN ( to be safe, we calculated the resistors for 22v) 5). 100R current limiting resistor on A1 and A2, to measure 5v and 3.3v as well… Analog inputs A0, A1 and A2 can be switched back to normal operation by changing the jumper at J2,J3 or J4 from On to Off. 6). 12 Digital Inputs/Outputs (14 if you use D0 and D1 as well), as well as a Jumper to remove the LED on D13. 7). Full access to the PCF8574 and ESP8266 Pins from the motherboard below.
Pictures of the PCB
Side by side MCU-8266-12E (left) 8266-12E-Port Extender (right)
Side by side MCU-8266-12E (left) 8266-12E-Port Extender (right)
Side by side MCU-8266-12E (left) 8266-12E-Port Extender (right) [ with unpopulated PCB at bottom]
Blank PCB 8266-12E-Port Extender
Blank PCB MCU-8266-12E IoT Controller
Top view 8266-12E PE
8266-12E-PE side view { from bottom)
Right side view – 8266-12E-PE
Left side view – 8266-12E-PE
Stacked on-top-of MCU-8266-12E
Stacked – Top Side view
Stacked – Right side view
Stacked – Left side view
Pictures of the PCB, alone and with the MCU-8266-12E IoT controller
Circuit Diagram
ATMega328P-AU Circuit diagram with LDO Regulators, headers and supporting circuitry.Analog measuring circuitry, level converters and supporting circuitry and headers
Uploading Code to the ATMega328P
Uploading code to the device requires the use of either an ISCP programmer ( Arduino as ISP works well ) or in the case of a pre-boot loaded chip, a USB-to-Serial converter. We did not find it necessary to add a dedicated USB-to-Serial converter onto the PCB. It is quite easy enough to do any flashing with the tools mentioned above.
Make sure that the PCB is not stacked when doing this. ( This will prevent excessive current use of other components when you supply 5v to the PE card.
Procedure to upload using ICSP
During assembly, you are required to solder a single 90-degree bend pin header on the bottom side of the PCB, in the same hole as the board side edge of the RESET push-button. This will serve as the RESET Pin for the ISCP. Other connections are as follows:
H2 Header <- > ICSP Programmer MOSI (E11~) – MOSI ( or Pin 11 on Arduino as ISP ) MISO (E12 ) – MISO ( or Pin 12 on Arduino as ISP ) SCK (E13) – D13 (or Pin 13 on Arduino as ISP ) RESET – D10 (or Pin 10 on Arduino as ISP )
5v and Ground from Arduino as ISP or ISCP Programmer to any 5v and ground pin on the PE Card
Please note the description above for assembly of the RESET pin header
Procedure to upload using USB-to-Serial converter
H1 Header
E0/Rx <- to Tx of USB-to-Serial converter E1/Tx -> to Rx of USB-to-Serial converter
H2 Header
DTR <-> to DTR of USB-to-Serial converter [ This connection is needed for successful uploading. Don’t leave it out ]
5v and Ground from the USB-to-Serial converter to any 5v and ground pin on the PE Card
Testing with ESPHome APE protocol and the MCU-8266-12E IoT controller
The following Arduino Sketch needs to be uploaded to the device. It will allow the device to function as a custom I2C device. Feel free to change the I2C address in the sketch as you choose, but remember to use the same address in your ESPHome YAML configuration file
The following C header file needs to be uploaded to your Home Assistant ESPHome folder.
// Must disable logging if using logging in main.cpp or in other custom components for the
// __c causes a section type conflict with __c thingy
// you can enable logging and use it if you enable this in logger:
/*
logger:
level: DEBUG
esp8266_store_log_strings_in_flash: False
*/
//#define APE_LOGGING
// take advantage of LOG_ defines to decide which code to include
#ifdef LOG_BINARY_OUTPUT
#define APE_BINARY_OUTPUT
#endif
#ifdef LOG_BINARY_SENSOR
#define APE_BINARY_SENSOR
#endif
#ifdef LOG_SENSOR
#define APE_SENSOR
#endif
static const char *TAGape = "ape";
#define APE_CMD_DIGITAL_READ 0
#define APE_CMD_WRITE_ANALOG 2
#define APE_CMD_WRITE_DIGITAL_HIGH 3
#define APE_CMD_WRITE_DIGITAL_LOW 4
#define APE_CMD_SETUP_PIN_OUTPUT 5
#define APE_CMD_SETUP_PIN_INPUT_PULLUP 6
#define APE_CMD_SETUP_PIN_INPUT 7
// 8 analog registers.. A0 to A7
// A4 and A5 not supported due to I2C
#define CMD_ANALOG_READ_A0 0b1000 // 0x8
// ....
#define CMD_ANALOG_READ_A7 0b1111 // 0xF
#define CMD_SETUP_ANALOG_INTERNAL 0x10
#define CMD_SETUP_ANALOG_DEFAULT 0x11
#define get_ape(constructor) static_cast<ArduinoPortExpander *>(constructor.get_component(0))
#define ape_binary_output(ape, pin) get_ape(ape)->get_binary_output(pin)
#define ape_binary_sensor(ape, pin) get_ape(ape)->get_binary_sensor(pin)
#define ape_analog_input(ape, pin) get_ape(ape)->get_analog_input(pin)
class ArduinoPortExpander;
using namespace esphome;
#ifdef APE_BINARY_OUTPUT
class ApeBinaryOutput : public output::BinaryOutput
{
public:
ApeBinaryOutput(ArduinoPortExpander *parent, uint8_t pin)
{
this->parent_ = parent;
this->pin_ = pin;
}
void write_state(bool state) override;
uint8_t get_pin() { return this->pin_; }
protected:
ArduinoPortExpander *parent_;
uint8_t pin_;
// Pins are setup as output after the state is written, Arduino has no open drain outputs, after setting an output it will either sink or source thus activating outputs writen to false during a flick.
bool setup_{true};
bool state_{false};
friend class ArduinoPortExpander;
};
#endif
#ifdef APE_BINARY_SENSOR
class ApeBinarySensor : public binary_sensor::BinarySensor
{
public:
ApeBinarySensor(ArduinoPortExpander *parent, uint8_t pin)
{
this->pin_ = pin;
}
uint8_t get_pin() { return this->pin_; }
protected:
uint8_t pin_;
};
#endif
#ifdef APE_SENSOR
class ApeAnalogInput : public sensor::Sensor
{
public:
ApeAnalogInput(ArduinoPortExpander *parent, uint8_t pin)
{
this->pin_ = pin;
}
uint8_t get_pin() { return this->pin_; }
protected:
uint8_t pin_;
};
#endif
class ArduinoPortExpander : public Component, public I2CDevice
{
public:
ArduinoPortExpander(I2CBus *bus, uint8_t address, bool vref_default = false)
{
set_i2c_address(address);
set_i2c_bus(bus);
this->vref_default_ = vref_default;
}
void setup() override
{
#ifdef APE_LOGGING
ESP_LOGCONFIG(TAGape, "Setting up ArduinoPortExpander at %#02x ...", address_);
#endif
/* We cannot setup as usual as arduino boots later than esp8266
Poll i2c bus for our Arduino for a n seconds instead of failing fast,
also this is important as pin setup (INPUT_PULLUP, OUTPUT it's done once)
*/
this->configure_timeout_ = millis() + 5000;
}
void loop() override
{
if (millis() < this->configure_timeout_)
{
bool try_configure = millis() % 100 > 50;
if (try_configure == this->configure_)
return;
this->configure_ = try_configure;
if (ERROR_OK == this->read_register(APE_CMD_DIGITAL_READ, const_cast<uint8_t *>(this->read_buffer_), 3))
{
#ifdef APE_LOGGING
ESP_LOGCONFIG(TAGape, "ArduinoPortExpander found at %#02x", address_);
#endif
delay(10);
if (this->vref_default_)
{
this->write_register(CMD_SETUP_ANALOG_DEFAULT, nullptr, 0); // 0: unused
}
// Config success
this->configure_timeout_ = 0;
this->status_clear_error();
#ifdef APE_BINARY_SENSOR
for (ApeBinarySensor *pin : this->input_pins_)
{
App.feed_wdt();
uint8_t pinNo = pin->get_pin();
#ifdef APE_LOGGING
ESP_LOGCONFIG(TAGape, "Setup input pin %d", pinNo);
#endif
this->write_register(APE_CMD_SETUP_PIN_INPUT_PULLUP, &pinNo, 1);
delay(20);
}
#endif
#ifdef APE_BINARY_OUTPUT
for (ApeBinaryOutput *output : this->output_pins_)
{
if (!output->setup_)
{ // this output has a valid value already
this->write_state(output->pin_, output->state_, true);
App.feed_wdt();
delay(20);
}
}
#endif
#ifdef APE_SENSOR
for (ApeAnalogInput *sensor : this->analog_pins_)
{
App.feed_wdt();
uint8_t pinNo = sensor->get_pin();
#ifdef APE_LOGGING
ESP_LOGCONFIG(TAGape, "Setup analog input pin %d", pinNo);
#endif
this->write_register(APE_CMD_SETUP_PIN_INPUT, &pinNo, 1);
delay(20);
}
#endif
return;
}
// Still not answering
return;
}
if (this->configure_timeout_ != 0 && millis() > this->configure_timeout_)
{
#ifdef APE_LOGGING
ESP_LOGE(TAGape, "ArduinoPortExpander NOT found at %#02x", address_);
#endif
this->mark_failed();
return;
}
#ifdef APE_BINARY_SENSOR
if (ERROR_OK != this->read_register(APE_CMD_DIGITAL_READ, const_cast<uint8_t *>(this->read_buffer_), 3))
{
#ifdef APE_LOGGING
ESP_LOGE(TAGape, "Error reading. Reconfiguring pending.");
#endif
this->status_set_error();
this->configure_timeout_ = millis() + 5000;
return;
}
for (ApeBinarySensor *pin : this->input_pins_)
{
uint8_t pinNo = pin->get_pin();
uint8_t bit = pinNo % 8;
uint8_t value = pinNo < 8 ? this->read_buffer_[0] : pinNo < 16 ? this->read_buffer_[1] : this->read_buffer_[2];
bool ret = value & (1 << bit);
if (this->initial_state_)
pin->publish_initial_state(ret);
else
pin->publish_state(ret);
}
#endif
#ifdef APE_SENSOR
for (ApeAnalogInput *pin : this->analog_pins_)
{
uint8_t pinNo = pin->get_pin();
pin->publish_state(analogRead(pinNo));
}
#endif
this->initial_state_ = false;
}
#ifdef APE_SENSOR
uint16_t analogRead(uint8_t pin)
{
bool ok = (ERROR_OK == this->read_register((uint8_t)(CMD_ANALOG_READ_A0 + pin), const_cast<uint8_t *>(this->read_buffer_), 2));
#ifdef APE_LOGGING
ESP_LOGVV(TAGape, "analog read pin: %d ok: %d byte0: %d byte1: %d", pin, ok, this->read_buffer_[0], this->read_buffer_[1]);
#endif
uint16_t value = this->read_buffer_[0] | ((uint16_t)this->read_buffer_[1] << 8);
return value;
}
#endif
#ifdef APE_BINARY_OUTPUT
output::BinaryOutput *get_binary_output(uint8_t pin)
{
ApeBinaryOutput *output = new ApeBinaryOutput(this, pin);
output_pins_.push_back(output);
return output;
}
#endif
#ifdef APE_BINARY_SENSOR
binary_sensor::BinarySensor *get_binary_sensor(uint8_t pin)
{
ApeBinarySensor *binarySensor = new ApeBinarySensor(this, pin);
input_pins_.push_back(binarySensor);
return binarySensor;
}
#endif
#ifdef APE_SENSOR
sensor::Sensor *get_analog_input(uint8_t pin)
{
ApeAnalogInput *input = new ApeAnalogInput(this, pin);
analog_pins_.push_back(input);
return input;
}
#endif
void write_state(uint8_t pin, bool state, bool setup = false)
{
if (this->configure_timeout_ != 0)
return;
#ifdef APE_LOGGING
ESP_LOGD(TAGape, "Writing %d to pin %d", state, pin);
#endif
this->write_register(state ? APE_CMD_WRITE_DIGITAL_HIGH : APE_CMD_WRITE_DIGITAL_LOW, &pin, 1);
if (setup)
{
App.feed_wdt();
delay(20);
#ifdef APE_LOGGING
ESP_LOGI(TAGape, "Setup output pin %d", pin);
#endif
this->write_register(APE_CMD_SETUP_PIN_OUTPUT, &pin, 1);
}
}
protected:
bool configure_{true};
bool initial_state_{true};
uint8_t read_buffer_[3]{0, 0, 0};
unsigned long configure_timeout_{5000};
bool vref_default_{false};
#ifdef APE_BINARY_OUTPUT
std::vector<ApeBinaryOutput *> output_pins_;
#endif
#ifdef APE_BINARY_SENSOR
std::vector<ApeBinarySensor *> input_pins_;
#endif
#ifdef APE_SENSOR
std::vector<ApeAnalogInput *> analog_pins_;
#endif
};
#ifdef APE_BINARY_OUTPUT
void ApeBinaryOutput::write_state(bool state)
{
this->state_ = state;
this->parent_->write_state(this->pin_, state, this->setup_);
this->setup_ = false;
}
#endif
The file should be named “arduino_port_expander.h”
Make the following changes to your ESPHome YAML configuration file for the MCU-8266-12E device
esphome:
name: mcu-8266-12e-01
platform: ESP8266
board: nodemcuv2
includes:
- arduino_port_expander.h
# Note the include file - This loads the APE Header
# Enable logging
logger:
# Enable Home Assistant API
api:
ota:
password: "<your password will be different - dont change it>"
wifi:
ssid: <your ssid>
password: <your password>
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "MCU-8266-Hotspot"
password: "password"
captive_portal:
i2c:
# PCB Prototype
sda: GPIO5
scl: GPIO4
# PCB Rev 1.5 or higher, comment the above 2 lines
# and uncomment
#sda: GPIO4
#scl: GPIO5
#################### - IMPORTANT ###########
scan: true
id: i2c_bus_a
pcf8574:
- id: 'pcf8574_hub'
address: 0x22 # Set at 0x22, feel free to change to your liking, Remember to set the chip to the address you choose as well
pcf8575: false
time:
- platform: sntp
id: ha_time
timezone: "Etc/GMT+7"
status_led:
pin:
number: GPIO16
inverted: true
#Define the APE as a custom component, taking care to ensure that:
#1). The I2C Bust ID is the same as the one you have defined in the I2C: Section
#2). The address of the APE is the same as the one you set in the sketch
custom_component:
- id: ape
lambda: |-
auto ape_component = new ArduinoPortExpander(i2c_bus_a, 0x08,true);
return {ape_component};
sensor:
- platform: custom
lambda: |-
return {ape_analog_input(ape, 0), // 1 = A1
ape_analog_input(ape, 1),
ape_analog_input(ape, 2)};
#We define 3 analog inputs (A0,A1,A2) to monitor voltages on the card
#Note that you MUST define them in the sensors section below as well AND
#THAT THEY MUST BE IN THE SAME SEQUENCE THAT YOU DEFINED THEM IN ABOVE HERE
#
#ALSO NOTE THAT YOU CAN "NOT" use A4 and A5, as they are used for I2C !
#
# As an example, of adding another 3 analog inputs, your definition above will change to:
#
# return {ape_analog_input(ape, 0),
# ape_analog_input(ape, 1),
# ape_analog_input(ape, 2),
# ape_analog_input(ape, 3),
# ape_analog_input(ape, 6),
# ape_analog_input(ape, 7)};
#
#
# Now define the sensors connected to these analogs below:
sensors:
- name: "PE Card VIN"
id: analog_a0
device_class: "voltage"
unit_of_measurement: "v"
accuracy_decimals: 2
filters:
- lambda: return x * (22.00/1023.0);
- throttle: 60s
# We use a lambda to scale the value of VIN - Our Voltage divider was designed around 22 volt
# thus we need 22 volt here in the calculation as well to make it accurate
#
- name: "PE Card 5v"
id: analog_a1
device_class: "voltage"
unit_of_measurement: "v"
accuracy_decimals: 2
filters:
- lambda: return x * (5.02/1023.0);
- throttle: 60s
- name: "PE Card 3v"
id: analog_a2
device_class: "voltage"
unit_of_measurement: "v"
accuracy_decimals: 2
filters:
- lambda: return x * (5.02/1023.0);
- throttle: 60s
# The ATMega328P 's Analog Reference is set to 5v internally, thus we need to also scale the
# 3v input with a maximum of 5v ...
# In case you enabled the other 3 Analog Inputs above, you need to add the following
#
# - name: "Analog 3"
# id: analog_a3
# filters:
# - throttle: 60s
# - name: "Analog 6"
# id: analog_a6
# filters:
# - throttle: 60s
# - name: "Analog 7"
# id: analog_a7
# filters:
# - throttle: 60s
#
#
- platform: adc
pin: VCC
name: "ESP8266 Chip Voltage"
id: mcu_voltage
unit_of_measurement: "V"
device_class: "voltage"
accuracy_decimals: 2
update_interval: 60s
- platform: wifi_signal
name: "WiFi Signal Sensor"
id: wifi_strength
device_class: "signal_strength"
unit_of_measurement: "dBm"
update_interval: 240s
#Digital outputs function the same
output:
- platform: custom
type: binary
lambda: |-
return {ape_binary_output(ape, 2),
ape_binary_output(ape, 3),
ape_binary_output(ape, 4),
ape_binary_output(ape, 5),
ape_binary_output(ape, 6),
ape_binary_output(ape, 7)};
outputs:
- id: ape_output_p2
inverted: false
- id: ape_output_p3
inverted: false
- id: ape_output_p4
inverted: false
- id: ape_output_p5
inverted: false
- id: ape_output_p6
inverted: false
- id: ape_output_p7
inverted: false
- platform: gpio
id: relay_1
pin:
pcf8574: pcf8574_hub
number: 0
mode: OUTPUT
inverted: true
- platform: gpio
id: relay_2
pin:
pcf8574: pcf8574_hub
number: 1
mode: OUTPUT
inverted: true
- platform: gpio
id: led_status_1
pin:
pcf8574: pcf8574_hub
number: 2
mode: OUTPUT
inverted: true
- platform: gpio
id: led_status_2
pin:
pcf8574: pcf8574_hub
number: 3
mode: OUTPUT
inverted: true
binary_sensor:
- platform: gpio
id: push_button_1
name: 'Relay1 Pushbutton'
device_class: ''
pin:
pcf8574: pcf8574_hub
number: 4
mode: INPUT
inverted: true
on_press:
then:
- switch.toggle: switch_relay1
- platform: gpio
id: push_button_2
name: 'Relay2 Pushbutton'
device_class: ''
pin:
pcf8574: pcf8574_hub
number: 5
mode: INPUT
inverted: true
on_press:
#min_length: 50ms
#max_length: 500ms
then:
- switch.toggle: switch_relay2
filters:
- delayed_on_off: 50ms
switch:
- platform: output
id: switch_relay1
name: "Relay No. 1 (#0)"
output: relay_1
on_turn_on:
- output.turn_on: led_status_1
on_turn_off:
- output.turn_off: led_status_1
- platform: output
id: switch_relay2
name: "Relay No. 2 (#1)"
output: relay_2
on_turn_on:
- output.turn_on: led_status_2
on_turn_off:
- output.turn_off: led_status_2
- platform: restart
id: reboot_switch
name: "Reboot Me"
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.
Welcome to the final instalment of my 8 DI Optically Isolated Arduino Shield. Today I will show you some of the assembly pictures, as well as look at the coding to use this shield. I will also provide you with a link to the manufacturing files, in case you want to make your own.
You can order your own version of this board for just $5 USD if you click here
PCBWay makes it quite easy to order prototypes for your PCB’s… Just upload the Gerber files on their website, select your desired options for the PCB and order. The turn-around time is great. I received these boards, ordered together with a stencil for SMD assembly, in exactly 5 days, shipping from China to Thailand 🙂 That is super fast, as it arrived 4 days faster than the components that were ordered locally from Bangkok! Be sure to consider using their services next time you need a PBC made…
Top and bottom layout of completed Shield
Bottom of Shield
Top Layout
Some notes on assembly: The reset switch will seem misplaced, and indeed, it is 🙂 The reason for this is that I could not get any 4 pin tactile switches 🙁 So I had to either leave it unpopulated or use a two-pin tactile switch. As I will be using these shields myself, I decided that although it doesn’t look perfect, the two pin switch will still provide me with the functionality that I want.
On the bottom of the board, you can still see some blobs of flux, as the pictures were taken right after assembly, and have not been cleaned up yet. Some solder joints have also not been cleaned up yet.
The top of the unpopulated PCBThe bottom of the PCB
Testing and Coding
The testing of the board is quite straightforward. I first checked all the power rails with a multimeter to make sure there are no open circuits of shorts. Then I checked connections to all the chips and other components, yes, it takes a while to do that, but rather safe than sorry. After assembly, I repeated this process, making sure that all the components receive the correct power level, and that all switches ( like for addressing and the reset button ) actually do what I intended them to do. The next tests were the individual inputs with the optocouplers. This is done by connecting an input source (between 5.5v and 32v) to each individual input and then physically testing on the pins of the optocoupler in question, for the correct voltage input.
The shield is then powered from 5v and the input test is repeated while checking with a multimeter that the input signal does indeed get transferred by the optocoupler to the PCF8574 chip. I found that with the particular batch of PCF8574 chips that I got, that the IC would only respond reliably with a voltage between 5.5v and 32v. The original design was for 3.0v to 32v. I found that the Optocoupler EL357N seems to be unable to switch itself on at the low current allowed through the resistor divider at the input. This can be fixed by lowering the value of R1, R5, R9, R13, R17, R21, R25, R27 from 4k7 to whatever value you need. Note that that will reduce the top-level input voltage that you can safely use. For my application, however, 5.5v to 24v will be perfect, so I will leave it as is.
The shield is now connected to an Arduino with DuPont Wires, to test the I2C addressing of the PCF8574. The chip address is changed with the 3-way dip switch at SW1. All eight addresses are available. It should be noted that I have used a pull-up configuration on the address lines. That will reverse your logic.. Switching the dip switch on will pull the pin to GND, not to VCC as you would normally expect. Thus as an example, all switches off will give an address 0f 0x3f, while all on will give 0x38.
Coding
You can use the standard Arduino IDE with the Wire.h library to code the shield, or you can use one of the many PCF8574 libraries that are available. I coded my tests with the Embeetle IDE, as it gives me much better control over my code. I will show you a short, interrupt enabled sketch, in Arduino C++ below
#include <Wire.h>
byte _portStatus = 0b00000000;
boolean _readI2C = false;
void MyISR() { // Interrupt service routine
//Serial.println("Interrupt Occured on Pin2");
if (_readI2C != true) {
_readI2C = true;
}
}
void setup() {
// put your setup code here, to run once:
pinMode(2,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2),MyISR,FALLING);
Serial.begin(115200);
Wire.begin();
Wire.beginTransmission(0x20);
Wire.write(0xFF); // set all pins to 1, needed to make them inputs
Wire.endTransmission();
}
void loop() {
// put your main code here, to run repeatedly:
byte _data;
if (_readI2C == true) {
_readI2C = false;
Wire.requestFrom(0x20,1);
if (Wire.available()) {
_data = Wire.read();
}
}
if (_portStatus != _data) {
Serial.print("Port Data Changed : 0xb");
Serial.print(_portStatus,BIN);
Serial.print(" changed to : 0xb");
Serial.println(_data,BIN);
_portStatus = _data;
delay(50);
} else {
_portStatus = _portStatus;
}
}
Conclusion
This turned out to be a very interesting and fun project to do. From designing the circuit to getting it manufactured and hand assembling it myself was a very satisfying experience. I would like to take this opportunity to thank Wendy Wu, from PCBWay‘s Marketing department, for her assistance with the manufacturing of the board. The speed and efficiency with which she handled this project were fantastic.
In part 1 of this article, I introduced my new I2C 8DI Optically Isolated Arduino Shield. Today, I will show you how the full design, as well as the circuit diagram.
This shield was designed to allow an input of between 3.0v and 32.0v to be applied to the various inputs. This will be completely galvanic isolated from the rest of the circuitry on the shield, and thus also from your Arduino, or other micro-controller if you choose to use another one 🙂 Yes, This is possible, as long as you power the shield with 3.0v to 5.0v. You will also have to connect your I2C bus to the SCL and SDA Lines marked on the shield.
Please note that, if you decide to do that, the other Arduino specific pins, as broken out on the shield headers, will have no connections to anything else. :), an obvious fact, but it should be stated, it seems 🙂
The Circuit Diagram
Circuit Diagram, Page 1 of 2
As you can see on page 1, each optically isolated input has a voltage divider resistor network in front of the Opto Coupler. This resistor network also limits the current that can be used by the infrared LED inside of the EL357N chip to 5mA at 32v DC. ( The chip can accept up to 50mA, but it should not be driven so hard 🙂 ) A diode provides reverse-polarity protection to each input as well.
Another voltage divider on the output side limits the current to the PCF8574 Chip. This can also only source or sink 25mA per IO.
Note that there is NO common ground between the input and output sides of this circuit. That means that you have to provide another ground, usually from your external device… This ensures that galvanic isolation between the two circuits is maintained.
Circuit Diagram, Page 2 of 2
On page 2, we can see the various net connections to the connectors, PCF8574 chip, as well as various jumper headers, to select the interrupt pin [H3] ( For Arduino, D2 or D3, other micro-controllers: you are free to select any GPIO to connect to the D2 or D3 header pin ).
You can also select to enable or disable the pull-up resistors on the I2C bus, by shorting the jumper on [H2]. This is usually only needed on the first shield, or in other words, you need one pair of pullup resistors per i2c bus, not one pair per device!
Device addressing is selected with SW1. 8 addresses are available but switching this switch as per the table on the back of the PCB. It is worth mentioning that depending on the version of the PCF8574 chip that is on the shield, there are 8 addresses available, with 0x20h to 0x27h being common on the PCF8574, and 0x38h to 0x3fh being used on the PCF8574A/T version.
Typical Connection
Typical connection of input. Note that there are no common ground between the two devices
The PCB
The shield is built on a double-sided PCB or 71.12mm x 61.72mm. This is only slightly bigger than the standard Arduino Uno. All resistors, capacitors and LED’s are of 0805 sizes. ( smaller than that is a bit hard on my eyes, although it can be done, just takes longer 🙂 ). A ground plane is provided on both sides of the PCB.
PCB design file
All Arduino pins are broken out on a double row of 2.54mm headers. This allows you to use either the outside row with stack-able male-female headers, like on most shields, or you can use dedicated male and female headers, in a zig-zag pattern to stack the shields.
I have done the same with the ICSP header, as on many other commercially available shields, there is only a single female 3×2 header on the bottom, making it quite annoying to use on another shield.
PCB Topside
PCB Bottom side
Manufacturing
This shield is currently being manufactured by PCBWay.
PCBWay provides a rapid and affordable PCB manufacturing service. They also provide PCB assembly, and even a 3D printing and CNC service. This can really help to bring new electronic prototypes to market quickly, as PCBWay can provide you with a complete turn-key solution to bring your product to market. The process to order a PCB is also completely automated, and you can easily do it online in a few minutes. Just upload your completed Gerber Files onto their system via their web interface, and you will get a quote in seconds. You can then pay and place your order immediately from the same page. They also have various shipping methods available, That really helps, as no one wants to pay for excessively expensive shipping, or be stuck having to use only one company.
The design files for this project will be made available as a project on the PCBWay website soon after the release of part 3, which will cover the assemble, testing and programming of the shield.