Give an old toy car new life…

Give an old toy car new life

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.

PCBWay

If you would like to have PCBWAY manufacture one of your own, designs, or even this particular PCB, you need to do the following…
1) Click on this link
2) Create an account if you have not already got one of your own.
If you use the link above, you will also instantly receive a $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.


Compact Remote Alarm Transceiver – Part 2

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.

The Logic Level Converter Chip

I have chosen the TXS0108E Bi-Directional 8-bit Logic-Level Voltage translator from Texas Instruments for this application.

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

Remote Alarm Transceiver PCB
The PCB

In the picture above, we can see the completed PCB (The relay and buzzer were not populated yet)

Manufacturing the PCB

PCBWay

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.

Compact Remote Alarm Transceiver – Part 1

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.

Remote Alarm Transmitter
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

LM317 Typical use circuit - Fixed Voltage

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\R1150180220240270330370390470
681.821.721.641.601.561.511.481.471.43
821.931.821.721.681.631.561.531.511.47
1002.081.941.821.771.711.631.591.571.52
1202.252.081.931.881.811.701.661.631.57
1502.502.292.102.031.941.821.761.731.65
1802.752.502.272.192.081.931.861.831.73
2203.082.782.502.402.272.081.991.961.84
2403.252.922.612.502.362.162.062.021.89
2703.503.132.782.662.502.272.162.121.97
3304.003.543.132.972.782.502.362.312.13
3704.333.823.353.182.962.652.502.442.23
3904.503.963.473.283.062.732.572.502.29
4705.174.513.923.703.433.032.842.762.50
5605.925.144.434.173.843.373.143.042.74
6806.925.975.114.794.403.833.553.433.06
8208.086.945.915.525.054.364.023.883.43
10009.588.196.936.465.885.044.634.463.91
120011.259.588.077.506.815.805.305.104.44
150013.7511.679.779.068.196.936.326.065.24
180016.2513.7511.4810.639.588.077.337.026.04
220019.5816.5313.7512.7111.449.588.688.307.10
270023.7520.0016.5915.3113.7511.4810.379.908.43
330028.7524.1720.0018.4416.5313.7512.4011.8310.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

PCBWay

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.

Special Limited Offer – Get your PCB for only $1

PCB for only $1

Every once in a while, we get an opportunity to take advantage of an exceptional deal. Recently, the friendly people at PCBPartner.com reached out to me with another great deal… “Get your next PCB for only $1!”

You will get the following:
10 FR-4 PCB, 1 or 2 layers, with Green Soldermask, in dimensions not exceeding 100mmx100mm, Manufactured and delivered to your door for only 1 (one) $USD.
This promotion will be valid until 31 May 2022, with AIRMAIL shipping included.

Promotion from PCBPartner

Who is PCBPartner.com

PCBPartner.com is owned and operated by Kinji Group, established in 1997. We have over 20 years of experience in PCB manufacturing, PCB design, component manufacturing and distribution, PCB assembly and PCB CAD software development.

While Kinji Group has 3 PCB factories in China, we have also developed strategic partnerships with more than 15 other factories around Asia. We, therefore, have a large group of specialists in PCB manufacturing, quality control, technical support and part sourcing to support your innovative ideas and products.

Our over 500 employees are spread across 8 branches in Mainland China (Shenzhen, Dongguan, Shanghai, Wuxi, Chengdu, Xiamen), Hong Kong SAR, and Taiwan. And we’re still growing!

Why Should you use PCBPartner for your next PCB order?

Variety of PCB Material

Up to 54 layers during PCB manufacturing.

FR-4 with different tg (135,150,170),
CTI 600 (Halogen free) PCB material,
Aluminium base PCB,
Flexible PCB,
Rigid-Flexible PCB,
Rogers, and many more materials.
Whatever material you need, they can deliver.

Special PCB Specifications:

Regular PCB production for these special PCB specifications

● Impedance control

● Custom-layer stack up

● 1-3 oz copper thickness for outer/inner layers

● Hard gold, ENEPIG, OSP surface finish

● Two different solder mask colours on one PCB

● Edge connector plating

● Edge plating

● Via filled with aluminium/resin

● And more…

Fast Lead Time

Turnaround is quick as prototypes usually take as little as 24 hours to produce, with a direct pickup service from their warehouse. They partner with DHL and FedEx for this to make sure you get your PCB in the shortest time possible. They also offer express production options for small and medium-sized batches so that you get your boards EVEN quicker.

Competitive Pricing

Get competitive pricing on prototypes and your future mass production. For any mass production orders, contact them so that they can find the best pricing and shipping solutions to maintain your competitiveness

Ordering boards in bulk? Save more the larger your order:

US$10 off orders of 100+

US$10 off orders of 100+

US$50 off orders of 500+

US$120 off orders of 1000+

Quality Control

After 20 years, they have built a strict standard of quality – their quality control teams ensure all PCBs pass the AOI and E-test with 100%.

Technical Support

They have an experienced PCB manufacturing and engineering team to help you detect potential mistakes in your Gerber files. They also use – and have used for 15+ years – a reliable PCB testing software solution, iPCB, to detect mistakes in designs.

Excellent Customer Service Team

They have a passionate (and patient!) customer service team to help you solve any problems while ordering, handle engineering questions, or answer any after-sales service request. They will respond to you within 1 working day, that’s their customer service guarantee.

Flexible Payment Term

If you have a large amount of PCB to order every month and want to work with them for AMS 30 payment, they will be happy to sign an agreement with you.

Promotion from PCBPartner

Easy to use CAN-BUS Module with Relay, LiPo Battery Backup

Can Relay Module running on battery power

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.

Can Relay Module Top view

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).

Can Relay Module side view

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.

Can Relay Module with LiPo cell and MH-CD42 Module

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!

Can Relay Module Schematic Page 1

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…

Can Relay Module Schematic Page 2

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…

CODE

The board has been extensively tested with Cory J Fowler’s mcp_can Arduino Library. It works very well indeed.

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.

CAN-to-LoRa gateway using RA-02, MCP2515 and ATMEGA328P

ATMEGA328P Module with integrated LoRa and CAN-BUS

INTRODUCTION

In my quest to perfect my LoRa telemetry system, I have gone through quite a few prototypes by this time. This post will focus on the next node design. Due to the fact that the area where I will deploy the system is quite large, but with roughly square boundary fence-lines, I decided to try and reduce the number of LoRa Radio nodes needed to cover the entire area. This opened up the opportunity to utilise CAN-BUS to attach sensor-only nodes to a Radio node and have them report status on exception as well as on requests from the radio node.

The device will thus function as a LoRa-to-CAN-BUS Gateway, with some local automation to control the transmission of data to the master station. This concept can also be adapted for use in other areas, such as home -automation, or an industrial setting.

At the heart of the device, I have stuck with the versatile ATMEGA328P, which, current chip shortages excluded, and current high prices excluded, are a very inexpensive chip, with lots of well-tested libraries, and a relatively low learning curve, largely due to its very wide use in the Arduino ecosystem.

The LoRa component is handled by the RA-02 or even RA-01H module, from AI-Tinker (not sponsored). This device, as we have seen in the previous prototypes, requires the use of logic level converters, due to the fact that it only accepts 3.3v logic levels. While I could get rid of those if I powered the ATMEGA328P from 3.3v, it would give rise to two problems, of which one will still force the use of level converters…

I chose to run the ATMEGA328P at 16Mhz, which basically forces me to use 5v to power the chip. The second reason is not so obvious unless you read a few datasheets very thoroughly…

The CAN-Bus component is handled by the MCP2515 Standalone SPI-to-CAN Controller, as well as the TJA1050 CAN Bus transceiver.

This is where things get interesting… The MCP2515 can operate on 3.3v, but the TJA1050 is a 5v only device. I could thus in theory use logic level converters only between the MCP2515 and the TJA1050, while running the rest of the circuit on 3.3v…

Given that I would rather run the ATMEGA328P on 16Mhz, as well as the fact that my LoRa Radio Module circuit, with its logic level converter circuitry, works extremely well, I decided not to change that, and keep the CAN Bus running at 5v all the way through, as I would still have to use a 5v regulator on the PCB anyway just for that purpose.

IO connections for LoRa and CAN BUS modules

You can order this PCB directly from PCBWay, by clicking here

Both of the two integrated components ( Lora and CAN ) are SPI devices. This means that they share common SCK, MISO and MOSI lines ( provided on the ATMEGA328P by pins D13, D12 and D11 respectively. The individual SPI device is then further selected for operation by the use of a CE pin, one unique pin per device, which is pulled low by the MCU to indicate to the device that it should pay attention to the data being transmitted on the SPI bus…

Both LoRa and CAN makes use of other pins as well, LoRa needs a Reset pin, connected to D9, a CS/CE Pin on D10 as well as a hardware interrupt pin, connected to D2. ( Note that this is for use with Sandeep Mistry’s LoRa Library. The Radiolib library would require an additional pin, usually connected to DIO1 on the LoRa module. The device does not provide access to those pins in its current layout, so you can only use it with the Sandeep Mistry library, for now at least… )

The CAN module uses a CE/CS pin at D4, with an IRQ pin on D6, which, although not a hardware interrupt pin, does have PCINT functionality.

Pins D10, D9 and D2 are not broken out for user access. although I decided to give access to D4 and D6, as well as the SPI bus, D11, D12, D13, to allow interfacing with logic analysers, or adding other SPI devices to the bus…

This brings us to a very interesting point… Does the two SPI devices actually play nice together? and what do I mean by “playing nice together”?

To answer that question, we are forced to first look at a bit of theory, as well as understand the fundamental differences between SPI and I2C…

The Difference between SPI and I2C

Most of us will be quite familiar with I2C, as it is a very common protocol used to connect sensors to a microcontroller. It consists of only two IO lines, SDA for data, and SCL for the clock. Each device on the bus has its own built-in address, like in the case of a PCF8574 IO expander, this address can be selectable between 0x20h and 0x27h. All of the devices share these common data lines, and will only respond when specifically addressed by the master controller… Unless you accidentally put two devices with the same address on the same bus, (if that would even work), there is no way that the wrong device would respond to any request for data…

SPI on the other hand, operates on a completely different principle, making it quite a few times faster than I2c, with data being simultaneously sent and received by the active device… SPI is also known as a four-wire protocol. Each device has a minimum of 4 data lines, namely SCK ( clock), MOSI ( for data transmitted FROM the Master TO the slave device ), MISO ( for data transmitted TO the master FROM the slave device) and a CE or CS ( Chip select ) pin.

SCK, MISO and MOSI are COMMON to all devices, meaning it is shared between all of them. CE/CS is a unique pin for EACH device, meaning that if you had four SPI devices on a bus, you would have to have four individual CE/CS pins!

A device will, or rather should only respond to data on the SPI-BUS IF the master pulls its respective CE/CS pin LOW. It should now very quickly become clear to you that this can become a very very complex mess, very quickly.

Let us take a very good example. the ST7789 SPI display module, has a cheap version, commonly sold on Ali-express, as we ll as other online stores. This particular module, I assume in a bid to make it easier to use, has the CE/CS pin internally pulled down to ground by default… So what about that, you may ask? What is wrong with that, as it saves you an IO pin?

It is in fact very wrong, a fact that you will very quickly discover if you ever tried using one of those displays on an SPI bus together with other SPI devices… Nothing will work, or only the display will work ( if you are lucky)

But why?

Pulling CE/CS LOW, signals to the chip that it should respond to instructions on the common SCK, MISO and MOSI lines. having the pin internally pulled LOW, thus forces that chip to always respond, even when it should not. Thus contaminating the entire SPI-BUS with garbage…

The answer to the question

After that very long-winded explanation, which is still extremely basic, it is time that we get back to our original question:

Does the Sx127x ( RA-02 ) Module and the MCP2515 Can Controller play nice on the same bus? The answer is not straightforward, as it comes down to which libraries you use…

Remember that the library must pull down the CE/CS pin of the device that it wants to communicate with. Some libraries wrongly assume that they are the only ones in use, and ignores the simple fact, that they should release the CE/CS pin AFTER EVERY transaction, to free up the bus for other devices to use it as well…

After extensive testing, I can however say that Sandeep Mistry’s LoRa Library, as well as the mcp_can library, does indeed play nice together. These two libraries do not keep the individual CE/CS pins pulled LOW, and allows the spi bus to be shared.

This is not the case with the ST7789 Module discussed above, where the hardware actually pulls the pin ow the entire time…

Taking a closer look at the PCB

Let us take a closer look at the PCB. The Ra-02 Module ( LoRa ) dominates most of the left-hand side of the PCB, with the ATMEGA328P on its right. The RA-02 is surrounded by the level converters, using the BSS138 N-Channel Mosfet, and 10k resistors (Q1 to Q6, R1, R2, R3, R4, R5, R6, R8, R9, R10, R11, R12, R13)

C1 and C2 are bypass capacitors for the Ra-02 module

In the bottom left corner, we have a hardware reset button, to reset the ATMEGA328P, with a yellow jumper (H1) next to it. This jumper controls the 120ohm ballast resistor (R17) for the CAN bus. Removing the jumper will remove the ballast. Directly below that is the CAN connecter, marked as U5, with CH as CAN-H, and CL as CAN-L ports.

U3 and U4, together with R18, R19, X2, C16, C17 make up the CAN components on the PCB. Decoupling is provided by C6, C7, C8 as well as C9 and C12 ( Includes the ATMEGA328P’s decoupling as well )

An ICSP programming header is provided above U1 ( ATMEGA328P) for use with USPASP, AVRASP or Arduino as ISP and similar.

No USB to serial converter is provided on the board, Serial upload is possible is loaded with an Arduino bootloader for the Arduino NANO ( to make use of all the analog inputs). RxD, TxD and DTR pins are broken out on opposite sides of the PCB, as well as access to 3.3v, 5v and GND pins.

A DC power socket is provided. it can accept up to 12v DC, although I would recommend not to go over 7.2 volts, to not stress the LDO regulators, at the back of the PCB ( LDO1, and LDO2) too much.

You can order this PCB directly from PCBWay, by Clicking here

in the picture above, I have connected a USB-to Serial converter, as well as CAN-BUS to the device.

Schematic Diagram

The detailed schematic diagrams are provided below:

Sheet 1 (above) takes care of the ATMEGA328p and it supporting circuitry, as well as the power supply via LDO regulators.

Sheet 2 (below) takes care of the Logic Level converters, RA-02 (Sx1278) LoRa Module, and CAN-BUS controller and transceiver circuitry.

Software and Firmware

In order to test this module, I made use of the mcp_can library by Cory J Fowler, for the CAN-Bus part,

as well as Arduino-LoRa by Sandeep Mistry

A combined example utilising both LoRa and CAN at the same time, will be released with the next part of the project, namely the CAN-Relay Module

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.

IO Shield for LoRa Base Module

INPUT And Monitor Shield for LoRa Base Module

Introduction

This is a straightforward project compared to my usual PCB projects. It is one of a series of designs towards completing my LoRa monitoring system, that I plan to use on the farm to monitor various areas, such as intrusion detection, battery levels etc.

The shield was designed to fit on top of the first LoRa Base Module. It was designed to allow easy, neat connections to Infrared Beam sensors, XYC-WB-DC Motion Detector, as well as to monitor the battery levels at the Solar power inverter (the farm is completely off-grid).

The Motion Sensor provides a +3v signal that goes high on motion detection, while the Infrared Beam Sensor provides a relay contact that can be used to provide a similar voltage signal. As I prefer to use pull-up logic on my inputs, I decided to implement a simple transistor circuit where the input will switch the base of the transistor, which in turn will pull the input to signal ground when switched on. This circuit uses the S8050 transistor, with the base pulled down to signal ground via a 10k resistor to prevent floating, and a current limiting resistor of 1k to 1k8 on the base. The collector, as well as the D7 and D8 GPIO pins on the LoRa Base Module, is pulled High to Vcc5v via a 10k resistor.

Voltage monitoring is done with the ADC on the LoRa Base Module, with each of the 4 battery levels ( 12v, 24v,36v, and 56v) being connected to the relevant ADC channel via a resister-divider network to lower the respective voltage to a level between 0 and 5v. ( This was done as the ADC on the ATMEGA328p can only handle a maximum voltage of 5.0v )

The resistors were chosen to give a slightly bigger input voltage range, to accommodate for fluctuations from the solar charger ( It can sometimes go up to 65v on a very bright sunny day ). I decided to not add any current limiting resistors directly onto the PCB, as they are definitely going to be bulky. They will instead be attached externally, to suitable heatsinks, etc…

As this PBC is still in the prototype stage, and my LoRa Monitoring device will definitely go through quite a few modifications in future, I provided access to all unused GPIO pins via separate headers on the shield. Most notably the SPI Header, used for programming the ATMEGA328P MCU, or connecting other SPI devices ( I am working on a CAN bus addon for the device, to save on the number of physical radio nodes that needs to be installed), The UART Header, as well as I2C, A6, A7 and additional Digital IO Pins (D3~, D4, D5~).

In the photo above you will notice an additional header on the UART pins, This was added to assist me with debugging on the logic analyser, as I had a slight issue with waking up the ATMEGA328P, and getting it to send events via LoRa. It would wake up, print status messages through the UART, but never do the actual LoRa part! That has now been fixed, and the header removed…

The schematic diagram for the shield is below. As you will see, it is very uncomplicated indeed.

Software and Libraries

The LoRa Base Module has been designed to use the LoRa Library from Sandeep Mistry. Further versions of this PCB will also directly support the RadioLib Library from JGromes.

A reminder :

The RA-02 Module (SX1278) connections to the ATMEGA328p is as follows:

NSS to D10 (CE)

MOSI to D11 (MOSI)

MISO to D12 (MISO)

SCK to D13 (SCK)

RESET to D9

DIO0 to D2 ( We need a hardware Interrupt pin ).

DIO1 to DIO5 was not broken out on the current version of the LoRa Base Module

There is also no direct access to pins D10, D9 and D2 either on the LoRa Base Module or on the Input and Monitor Shield.

If you do decide to hack the device and add access to any of the non broken out pins, please remember to use a logic level converter between the ATMEGA328 and the SX1278, as they operate at different voltages, and the SX1278 is not 5v tolerant on any of the IO Pins!

You can order this shield from PCBWay by clicking here

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.

Sx127x-Ra-02-Test-Module with ATMEGA328P-AU

SX127x LoRa/FSK/OOK Module with LiPo battery-backup

SX127x LoRa/FSK/OOK Prototype Radio Board

I recently had a requirement to do some automation/telemetry on the farm. Things went missing, unauthorised persons were trespassing on the property, helping themselves to eggs, chickens, ducks and produce. Something had to be done, and as there is nobody sleeping there at night, it had to be possible to get remote status updates in real-time.

The farm is also completely off-grid, with solar-powered inverters taking care of all the water and electricity needs. Power usage monitoring could thus be a great help as well…

The idea is as follows:

Have a central control station, with various remote devices to do intrusion detection, control lights and water pumps, as well as monitor the battery levels of the inverter and solar-panel system. As the area is quite large, having to pull in electrical cabling will not be feasible, nor could the inverter handle all of that.

I decided to use the RA-02 LoRa/FSK/OOK module, on a custom PCB, but with various different PCB modifications, to take care of each stage of the project. The PCB that I will present today will be mainly used as the control unit, but it could also be a remote station, depending on what options are needed.

The ATMEGA328P-AU MCU is used as the main processor on each board, with the MH-CD42 Boost Converter/LiPo battery charger module taking care of power supply requirements. This module can source up to 2A at 5.0v, More than adequate for my needs. The ATMEGA328P will be put into sleep mode, to wake on interrupt to respond to events as needed ( To help save power ). Power usage of the circuit is around 50mA in standby ( NOT SLEEP MODE ), and with a peak of 100mA on a LoRa Transmit or Receive event.

The RA-02 Module did however present some challenges, as it is a 3.3v device, with non-5v capable IO lines. This made it necessary to include a 3.3v LDO regulator, as well as logic level converting circuitry onto the PCB. To allow for the most flexibility, all DIOs on the RA-02 was also broken out via level converters, in addition to the required SPI pins ( MOSI, MISO, CE and SCK ). This amount to a total of 11 level converters onboard.

I chose the MH-CD42 Power module for the reason that it can supply current at the same time as charging the LiPo battery (in my case, I used a 18650 cell). This will help greatly, as 220v inverter power is available during the day to charge the batteries.

PCB fitted to enclosure

The PCB can be ordered at PCBWay by clicking here
This image has an empty alt attribute; its file name is Screenshot-from-2021-07-22-18-45-42.png

Another challenge was definitely the enclosure. I found some really nice enclosures online, but the mounting holes were located in a very particular pattern, making it necessary to do some very careful measuring to get the PCB to fit exactly. The enclosure also has space to accept the other PCB modules, like the USER Control panel, and other sensor devices as needed. PCBWay did a great job at manufacturing the PCB so as to fit exactly. I must admit that I had quite a few tense moments between sending the PCB off to manufacturing and receiving it, as to whether my measurements were actually accurate, and if the PCB would fit as I imagined…

As you can see in the picture above, it turned out perfectly.

The Circuit diagram is basically a standard Arduino Nano ( I needed access to all of the ADC pins ),

Connections to the RA-02 module is as follows:

RA-02ATMEGA328P
MOSI D11
MISO D12
SCK D13
CE D10
RESET D9
DIO0 D2 ( We need a hardware interrupt pin here )
DIO1 D3 ( Hardware Interrupt, enabled through jumper)
DIO2D5 ( Enabled through a jumper)
DIO3 to DIO 5 Not connected, available on a breakout header
Connections between Ra-02 (SX127x) and ATMEGA328P – Note that all IO Lines are connected through level converters 5v <-> 3.3v

On the ATMEGA328, the following pins are broken out into headers:

D3 can be connected to RA-02 with a jumper
D4
D5 can be connected to RA-02 with a jumper
D6
D7
D8
D13 ( SCK ) Broken out as a 5v logic pin
D12 ( MISO ) 5v logic pin
D11 ( MOSI ) 5v logic pin
A3,A6,A7 ADC Pins ( A0, A1, A2 is used internally to monitor VIN, VCC5v and VCC3v )
A0 = Vcc3v A1 = Vcc5v A2 = VIN
SCLI2C SCL pin OR A5
(I2C pins are at 5v logic levels )
SDA I2C SDA pin OR A4
(I2C pins are at 5v logic levels )
RESET
DTR ( connected through a 100nf Cap to Reset, used for serial uploading firmware )
D0 ( RxD ) UART Rx Pin ( 5v logic )
D1 ( TxD ) UART Tx Pin ( 5v logic )
ATMEGA328P breakout pins. All pins are at 5v logic level

The ATMEGA328P is clocked at 16Mhz through an external crystal.

3v, 5v and GND pins were broken out at two opposite corners of the PCB to allow easy connection of additional devices/ sensors as needed.

Powering the PCB

The PCB can be ordered at PCBWay by clicking here

This image has an empty alt attribute; its file name is Screenshot-from-2021-07-22-18-45-42.png

The MH-CD42 module has the capability to power a load while charging the LiPo battery. This makes it possible to do a few interesting things while supplying power to this particular PCB.

The board can be powered directly from a 5v header pin ( NOT from the DC1 input ). In this mode, a LiPo battery is not required.

5v will be directly supplied to the processor, as well as the 3.3v LDO regulator, allowing the RA-02 module to function as well.

PLEASE NOTE: THERE ARE NO 5v REGULATION – MAKE SURE YOU SUPPLY REGULATED 5v

Installing the MH-CD42 module will add the option of charging a 3.3v 18650 Lipo battery, as well as powering the board at the same time. You need to supply power through the DC plug to make use of this function. This mode is also a miniature uninterruptible power supply, with the 3.3v LiPo battery immediately taking over should the main DC input fail.

NOTE: The MH-CD42 can only accept up to 5.5v MAXIMUM as input!

Software:

The PCB can be used with LoRa libraries from Sandeep Mistry OR JGromes/RadioLib ( If you want to do FSK/OOK or other advanced stuff like LoRa of FHSS)

Sandeep Mistry LoRa Module
JGROMES/RADIOLIB

Both libraries have extensive examples showing how to use them. As our use case will definitely differ, I will not have any example here at this stage. I will add one later after the entire project is completed, with all the relevant modules.

The PCB can be ordered at PCBWay by clicking here

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.



Build your own USB-ASP Programmer Clone

Using more ATMEGA based MCU’s in my recent projects, and not using ready-made Arduino boards for any of them, it became necessary to invest in a dedicated ASP programmer to flash the MCU’s. Huge delays on electronics components, with confirmed orders being mysteriously cancelled due to sourcing issues, and the high cost of “original OEM ASP programmers”, send me on a search for a DIY style programmer, like the old “NOPPP” ( NO PARTS PIC Programmer) in the good old days…

Personal computers have changed a lot, and while ATMEGA chips are not PIC’s, I did feel that it could be done. Given the fact that you could also use an Arduino UNO as an ISP, and doing a little more digging, I finally found a very attractive alternative…

Thomas Fischl, on his website,https://www.fischl.de/usbasp, has an open-source hardware project, that seemed to be exactly what I needed. His device is capable of programming 5v devices, at various speeds, including a super slow one. He has also written firmware for the device, and although the last update was in 2011, It still seems to be working well, or at least as far as I can see…

I did however decide to add my own twist to the design and build in a logic level converter, to be able to program 3.3v devices as well…

The modified schematic is below:

Schematic Page 1
Schematic Page 2

I chose to do my own PCB layout, to incorporate the changes listed earlier, namely the logic level conversion from 5v to 3v, as well as adding a 3.3v LDO voltage regulator to provide 3.3v to the target in case it is required.

PCB Topside
PCB Bottom Side
3D Render of the PCB

The PCB is still in transit from the factory, and thus I will update this article later with pictures of the actual device.

Firmware can be flashed using any ASP programmer, Arduino as ISP as well, but with the caveat that you have to use AVRDude from the command line… More on that in the follow-up post… ( I would like to show actual screenshots of the process, and not theory.. ). The links to the firmware are available on https://www.fischl.de/usbasp, courtesy of Thomas Fischl, whom I would like to thank for making this open-source hardware project available freely, as well as for writing and maintaining the firmware.


The PCB can be ordered, or the design files downloaded ( a free download ) from my Projects page at PCBWay soon…

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.




Automate your desk- or floor-standing fan

A typical desktop oscillating fan

Desk or floor-standing fans are one of those appliances that will be present in almost every home or office. Some of the newer ones may already have remote control of some sort, while the older models won’t. It is however quite easy to do a retro-fitted controller to most of them, and at the same time, give them some (limited) intelligence.

Your typical oscillating fan does not have a lot of intelligence built-in. They normally consist of an electrical motor, with three separate windings, of varying inductance ( meaning the number of turns in the coil of wire will change the magnetic field generated, thereby changing the speed of the electric motor).

These windings have one common side, where all three of them are connected together, and the other three are separated. Normally the live wire from your mains supply (220v AC in my case) will go to this common connection. The neutral wire will go to the common of a four-position mechanical switch, with each winding going to one of positions 2,3 and 4 ( This results in a 3-speed configuration, with the first switch being off). It is also VERY important to note that this mechanical switch is hardware interlocked, meaning that ONLY one switch can be on at any given time… This is to ensure that electricity can only flow through one winding at a time. If you were to send electricity through multiple windings at the same time, the motor will still work, but not for very long…

A more modern Oscillating Fan


In order to automate an oscillating fan, we would thus need a way to switch the separate windings on and off, while preventing other windings from getting power at the same time. I chose to do this with SPST relays, as a proof of concept, and plan to design it with DPDT relays at a later stage to implement a proper hardware interlock, in addition to the software interlock implemented in the control software ( more on that later)

My requirements for the device are the following:
1) Must operate from mains power, using the existing power cord of the fan.
2) Must allow for local operation of the fan using the existing control buttons.
3) Must be able to update firmware OTA, and have WiFi connectivity for control via Home Assistant or MQTT
4) Must be capable of adding support for ESP Now protocol at a later stage
5) The fan must not have any visible modifications on the outside

The 3 speed Fan controller PCB

Taking all of my requirements into consideration, I have designed the following PCB to take care of my needs. As I do not require a lot of GPIO for this ( only 3 outputs, and 3 inputs ), I have decided to use an ESP8266-12E module from Espressif ( manufactured by AITinker, not sponsored by either company). This module is relatively cheap and has more than enough flash memory, RAM, as well as GPIO available.

Circuit Diagram – Page 1
Circuit Diagram – Page 2

As we can see, the circuit is minimal, with optical isolation on the relay drivers, a programming header, and a 3-way input for the mechanical switch.

The completed PCB, wired to the oscillating fan

As seen in the picture above, the wiring is quite simple, with the neutral wire looped to the common terminal of each relay (I had only green mains rated cable available, will replace it later with a proper white cable to keep to wiring standards). Black is live, with one wire going to the mains socket, and the other to the common of the motor coil windings. Light blue, yellow and white ( connected to the N/O terminal of the relays) corresponds to speeds 1, 2 and 3 of the fan.

At the top of the board, 3 wires go to the mechanical switch and a fourth to DC ground. (Note that there is no AC voltage on any of the switches. )

Mounting the PCB in the base of the oscillating fan

The PCB is mounted in the base of the fan while taking care to ensure that no AC cables are near the DC components. The ESP8266 chip is oriented to the side ( logo side of PCB ) to prevent interference to the WiFi signal. The mechanical switch is mounted into its original position, and its wires are routed away from any AC carrying wires to prevent interference.

It is important to note here that the firmware for the PCB was uploaded before assembly. You should NOT attempt serial uploading while the device is connected to mains power under any circumstances. ( While I have taken every precaution to ensure that AC and DC components of the circuit are separated from each other, it is just common sense to not try to upload firmware with mains connected)

The completed PCB shows the Upload port near the right top corner.

Uploading firmware:

Initial uploading of firmware can be performed by connecting a USB-to-serial adapter to the UPLOAD port and providing 5v and ground from the USB-to-serial adapter. The flash button is held down, and the board is reset, after which you can proceed with uploading, alternatively, you can also connect the DTR and RTS lines from the serial adapter to automatically reset the board and enter flash mode as needed. ( If your adapter supports this of course).

ESPHome configuration

The YAML configuration for ESPHome is listed below:

esphome:
  name: esphome-web-18df94

esp8266:
  board: nodemcuv2


# Enable logging
logger:

# Enable Home Assistant API
api:

ota:


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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esphome-Web-18Df94"
    password: "verysecurepassword"

captive_portal:

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

binary_sensor:
  - platform: gpio
    pin: 
      number: 12
      inverted: true
    name: "Fan Local Control Speed 1"
    id: "fan_local_1"
    icon: "mdi:fan-speed-1"
    filters:
      - delayed_on: 500ms
      - delayed_off: 500ms
    
    on_press:
      then:
        - switch.turn_on: speed1
    on_release:
      then:
        - switch.turn_off: speed1
  
  - platform: gpio
    pin: 
      number: 13
      inverted: true
    name: "Fan Local Control Speed 2"
    id: "fan_local_2"
    icon: "mdi:fan-speed-2"
    filters:
      - delayed_on: 500ms
      - delayed_off: 500ms
    
    on_press:
      then:
        - switch.turn_on: speed2
    on_release:
      then:
        - switch.turn_off: speed2
      
  - platform: gpio
    pin: 
      number: 14
      inverted: true
    name: "Fan Local Control Speed 3"
    id: "fan_local_3"
    icon: "mdi:fan-speed-3"
    filters:
      - delayed_on: 500ms
      - delayed_off: 500ms
    
    on_press:
      then:
        - switch.turn_on: speed3
    on_release:
      then:
        - switch.turn_off: speed3

switch:
  - platform: template
    name: "Fan Off"
    id: "fan_off"
    icon: "mdi:fan-off"
    lambda: |-
      if (id(speed1).state or id(speed2).state or id(speed3).state) {
        return false;
      } else {
        return true;
      }
    turn_on_action:
      - switch.turn_off: speed1
      - switch.turn_off: speed2
      - switch.turn_off: speed3
      

  - platform: gpio
    pin: 16
    interlock: &interlock_group [speed1, speed2, speed3]
    interlock_wait_time: 1000ms
    name: "Fan Speed 1"
    icon: "mdi:fan-speed-1"
    id: "speed1"
    inverted: true
    
    
  - platform: gpio
    pin: 5
    interlock: *interlock_group
    interlock_wait_time: 1000ms
    name: "Fan Speed 2"
    icon: "mdi:fan-speed-2"
    id: "speed2"
    inverted: true
    
  - platform: gpio
    pin: 4
    interlock: *interlock_group
    interlock_wait_time: 1000ms
    name: "Fan Speed 3"
    icon: "mdi:fan-speed-3"
    id: "speed3"
    inverted: true
    

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.