All posts by Cale

328P PORT MANIPULATION

#122 ATMEGA 328P Port Manipulation

Port manipulation was not important to me when I started working with microcontrollers, in fact I didn’t even know it existed and never felt the need for it especially after programming mostly in java script and PHP using functions like digitalread(pin); or digitalwrite(pin); etc. was second nature and speed or efficiency considerations were not really necessary or thought about that much.

Then after intermittently playing around with embedded ICs I got serious during the corruption virus of 2019 during lockdowns and other dilemmas while a lot of corrupt and diabolical cronies enriched themselves. I had a lot of time to focus on a few different things. There was no way I was going to let the circumstances get to me I made sure I was achieving at least one big goal each year and not get bogged down.

I took a look at the IC market and even though there was a chip shortage there were some IC’s available. This mad me try and get into efficient code and cost per IC. I came to the conclusion that I could get hold of IC’s with low memory + flash and very affordable prices. However in order to use them I would have to learn to use port manipulation, macros and interrupts in very efficient ways to the best of my ability at the time.

Now I needed to find new and comprehensive writeups of the principal and so I found the SpenceKonde
megaTinyCore
for the new at the time tinyAVR 0/1/2-series IC’s. The core has a lot explained over the years and was very helpful and easy enough to understand but took a bit of time and real world practical implementations which made learning way more fun and easy. I always need to do practical with theory when it comes to learning I find it very hard to picture learning anything without using it practically.

Now this is about port manipulation on the 328P and I’ll get into the actual implementation now without further ado.

The ATMEGA 328P has 3 ports namely B, C, D with B (digital pin 8 to 13), C (Analog input pins) and D (digital pins 0 to 7).

For the basics you only need to understand 3 types of registers you will be manipulating.

For example:

Port B
PORTBMaps to Arduino digital pins 0 to 7 Data Register – read/write
DDRBThe Port B Data Direction Register – read/write
PINBThe Port B Input Pins Register – read only

Port C
PORTCMaps to Arduino digital pins 8 to 7 Data Register – read/write
DDRC The Port C Data Direction Register – read/write
PINCThe Port C Input Pins Register – read only

Port D
PORTDMaps to Arduino digital pins 14 to 19 Data Register – read/write
DDRDThe Port D Data Direction Register – read/write
PINDThe Port D Input Pins Register – read only

So an example using DDRD (Data Direction Register) to set input and output pins:

Another example is to set digital pins 7,5,3 high:

Now you can do port manipulation with a bit shift instead of binary or hex. this makes things a bit more readable… excuse the pun. 🙂

For example to set the input or output of a single pin you could use:

Example using hexadecimal:

Now to initiate a pullup on a single pin you could use:

Example using hexadecimal:

Example using binary:

Looking at how to read a pin you can use the Input Pins Register PINB:

it is also possible to toggle a pin and there’s different ways. Some are faster then others:

You can also easily write to a pin low or high:

There’s also an atomic operation to writing low or high:

So now that we understand the basics we can get into making life easy by creating macros

Below are 4 functions made easy to iterate over all the pins in each port B, C, D

set_PIN_HIGH(pin);
set_PIN_LOW(pin);
read_PIN_STATE(pin);
toggle_PIN_STATE(pin);

This makes it easy to call the function without having to define the port each time and it’s still fast.

See the example below:

Links:

MAKING A DIY PIR SENSOR

#121 Making a DIY PIR sensor on protoboard

Front of PCB on proto board

A few years ago I wanted to use some type of sensor for security purposes in my workshop.
I just needed something to sense movement and send a radio signal to my Roboguard device.
An actual Roboguard beam was too expensive, too bulky and large and was not rechargeable… not to mention a bit overkill for my application.

So I decided to look for a low power and small sized PIR sensor. Eventually I was able to find an affordable and small PIR sensor that can operate at 3.3V. I found the MH-SR602 PIR sensor which offers an adjustable delay time with a detection distance of 0 – 3.5 Metres. it has a built in regulator with a range of 3.3 – 15VDC but adds a bit of extra current draw… since I was already using 3.3V for my IC and RF circuits I was able to de-solder the included regulator to save a bit of current. This would help me greatly especially since I was going to use deep sleep.

PCB inside the box I designed it for.
Lid closed only the PIR sensor is sticking out (Radar does not have this problem)

Now the main MCU is the ATTiny212 and the RF module used is the WL102-341… not sure on the authenticity of the module but it works range is good and the current draw is up to spec + the EN pin does disable the unnecessary current draw when pulled low. The LDO regulator I used is the classic HT7333-A.

After soldering everything and checking for shorts I was able to program the MCU and then power up the device with an recycled 550mAh vape battery. First thing I noticed the device would constantly trigger the PIR. Eventually after a long time of troubleshooting I replaced one of the ceramic capacitors used to stabilize the output of the HT7333-A and now everything worked… Now when looking at the capacitor I didn’t understand exactly why it was not working, there was no short only a really high resistance and the capacity was a bit higher than the specified uf.

Before hand I made sure the PCB would fit into a standard rectangular project box. I just needed to drill a hole for the PIR sensor to stick out. After drilling a hole for the micro USB charging port the project was complete and working.

While testing I was able to achieve a very low current draw in deep sleep mode. I did some basic calculations and the MCU should last more then a year without needing a charge. Later after leaving the device in my workshop I was able to confirm this with the device lasting over a year and a half with mild triggering whenever I was working in the workshop. making use of the ATtiny PIT for long sleep timing and interrupts for waking up the device after some code refactoring I was able to make things super efficient. I programmed everything in C. ASMM is a bit to much for low level stuff in my opinion though I would like to make time to learn it one day. Still I was able to create a program of 956 bytes using ram of 18 bytes. So under 1k was nice since the smallest ATtiny size is 2k so I’ve still got plenty of space.

Current when the MCU is in sleep mode (the PIR is always running) 121.9uA roundabouts
772.2uA of current when the MCU is running but RF is not TX’ing
13.1mA during TX

One thing I can say for sure is that this project really activated a habit in me to try make everything as small and as efficient as possible whenever I program 8-bit microcontrollers for my personal projects. I must say I was also able to save some money when buying IC’s by improving my code and what can I say it feels very good once a project is completed and as small and efficient as I could make it.

With that being said sometimes when using a microcontroller like the ESP8266 or ESP32 the resource abundance feels crazy but once I start populating all that space with HTML and bitmaps + other media I quickly realized that even that space can get used up very fast.

C.A Torino

Back of the protoboard

328P PIN CHANGE INTERRUPTS

#120 ATMEGA 328P Pin Change Interrupts

The 328P has 3 ports B, C, D you will need to know the basics of how they work to before reading this.

The 328P only has 2x external interrupts however there are also 23x pin change interrupts and I will be focusing on the latter for now.

INTx (external interrupts) can report events under four situations: low, any, falling, rising. PCINTx (pin change interrupts) can report events on only one situation: any, which is basically a change in the pin so we can categorise the 23x as change interrupts only.

Using these pin change interrupts are surprisingly easy but require a few steps. However once they are configured code size and complexity can be reduced significantly. Also never forget you paid for the IC and it’s hardware peripherals.. so USE THEM.

You can run a loop checking button states with multiple variables including timing and state variables etc. or…. you can run a similar reduced loop with the use of the pin change interrupts making your life so much easier while not compromising much resources.

So to get started you need to follow 3 steps:

  1. Turn the pin change interrupts on
  2. Choose the pins to interrupt
  3. Use the ISR for the chosen pins

STEP 1:

To turn on the pin change interrupts you will need to use the PCICR register.

Writing 1 to bit 0 will turn on the portB (PCINT0 – PCINT7)
Writing 1 to bit 1 will turn on the portC (PCINT8 – PCINT14)
Writing 1 to bit 2 will turn on the portD (PCINT16 – PCINT23)
From bit 3 onwards (from right to left) bits are ignored.

STEP 2:

Now you need to choose which pins you want to interrupt.
You will need to use a mask and the 328P has 3 masks: PCMSK0, PCMSK1, and PCMSK2
These masks are set in the same way as the PCICR register was set.

STEP 3:

Now you need to use the correct ISR for the chosen pins.
make sure to keep the ISR as fast as possible and use as little code as possible in the ISR.
Also if you have any variables in the ISR make sure to make them volatile. This tells the compiler that it could change at any time and to reload it each time instead of optimizing it.

Full Super Simple Example:

Links:

MPLABX VS ARDUINO IDE

#119 Differences and similarities between the two

Recently I wrote about the pros and cons etc. about different programmers used to program embedded devices. I mentioned a few examples like ST-Link, PicKit and standard DIY programmers. Now since programmers work hand in hand with the programming software on your PC I decided to talk a bit about the 2 IDEs I use often. (Although you can easily setup platform IO with a compiler for embedded devices I will be focusing mostly on full IDEs but will make mentions to using text editors)

So I’ll start with the Arduino IDE. it’s simple to install and to get started with. There’s many libraries and there’s a wide support on the internet. Note: at least in my experience in South Africa at the public school I went to there was basically no interest or effort to promote Arduino and similar platforms. Looking back this was very disappointing since I’m 99% sure I would have gotten into Arduino much earlier in life if certain educational departments had made an effort in promoting Science instead of supporting political nonsense like the “science must fall” movement but I digress.

That being said Arduino is not all fun and games, it’s a great learning introduction tool but can promote bad code practices and reliance on libraries for work. Arduino also heavily promotes using easy functions instead of port manipulation methods… even in advanced projects… this can be a bit annoying especially when you want to use a library in a project with a different MCU and also when you want to keep code small and efficient. This can really bloat your MCU code and as you can imagine there is really not much room “literally” when programming embedded devices. Macros for port manipulation can really help but relying on digitalwrite(pin1); to pull a pin high or low can really cause some confusion later on

There may be more pros and cons not mentioned above but really I just want to get into the stuff that I can mention off the top of my head for this article.

Now switching to the free MPLABX IDE. I can say that the learning curve is quite steep but easy to get into with repetitive use. Once you get familiar with the layout you can start seeing quite a few pros compared to Arduino. having the ability to view the entire file structure in the IDE helps a ton. Also AVR has been integrated into MPLABX for some years now so you can easily program Arduino style. The IDE promotes professional main.c files and avr-main.c files which is very cool (I’m not really into ASM programming at least for now there’s a very big learning curve but one day I’ll get into it I hope 🙂 ) MPLABX also supports a huge variety of ICs and you can easily download updates for these as well as some libraries. Another pro is the GUI MCC (MPLAB Code Configurator) ok, ok I’m not a huge fan because I always somehow bloat up my project and break things but I can totally see how it could help by providing a GUI for setting clocks and bits etc. Another cool feature is that there are options for dark mode in the ide and it uses NetBeans.

There are a few downsides to the MPLABX IDE for example when using a 4k screen the Nebeans part always has blurry visuals now you can adjust the DPI but then all the text is super small and when adjusting the text it becomes inconsistent in certain places. Like for example the IDE text is small but your code text is big. This has always been an issue for me but I guess I’m just suffering from a 4k screen 🙂 Another issue is that I have always had to use expensive dedicated programmers when using MPLABX non of my DIY CH340N etc. programmers will work with MPLABX. Also programming AVR requires an AVR programmer so you can’t just use a PicKit3 for everything. Another pro is that it’s easy to choose compilers in a list. You cans install multiple compilers without issue. Another great feature is that you can install the so called MPLABEXT extension using visual studio code so you don’t have to use the IDE but can keep compatibility. Another cool feature is the ability to read and program the fuses or (configuration bits for PIC). Once again there may be some pros and cons not mentioned but I’m just writing this off the top of my head.

An honourable mention goes to the text editor approach. This is very light weight and generally bloat free and offers a lot of flexibility which makes using visual studio code a great choice but of course it’s not really a dedicated IDE.

Now to close off I will include to code samples to show the differences between the Arduino IDE and MPLABX IDE I will be programming an AVR device the ATtiny826 in the comparison examples.

Arduino blink code example including a blink without delay and a fast blink sample I made :



MPLABX blink code example including a blink without delay and a fast blink sample I made :

DIFFERENT PROGRAMMERS

#118 A look at different types of programmers

Different programmers for different IC’s some work with others.. some don’t

Over the years I have used different programmers for PIC,AVR,ST,ESP and WCH microcontrollers from simple FTDI TX RX programming to more advanced PICKit 5 dedicated programmers.

All the different types have their pros and cons but by far the most used by me is the cheap FTDI and serial TTL types for example programmers using the CH340, PL2303 and FT232 IC’s

Though the PL2303 is outdated there’s still many floating around and certain applications require the IC like when interfacing with old radios.

PICKit3, PICKit3.5 and the new PICKit5

With that being said you may ask why do we need a dedicated programmer? Well it depends…

Using the cheap common options I just mentioned above is good enough for a hobbyist but when you design a product you want something that’s is reliable common and has a guaranteed life span with support for the foreseeable future so that you will always have parts available for projects and the programmer.

USBasp AVR Programmer using SPI
STM Programmer


WCH-LinkE programmer can also program STM IC’s

Another big point is uniformity, dedicated programmers will usually be using parts with certain thresholds and voltages + current etc. all that data will be kept constant and accurate while a cheap programmer will most likely have a huge threshold..

A very important point is the ISP + debugging. A dedicated programmer will be able to do programming and debugging easily with a few pins. While a cheap programmer will require more pins and many times 2x programmers… 1 for debugging and 1 for programming.

BUS PIRATE can program with SPI
Arduino Nano with built in CH340 IC for serial programming
A n old DIY programmer of mine based on the CH340N IC

Dedicated programmers also have full support by the IC company as long as you have an original also they offer some really useful features like the PicKit5’s blue tooth option and stand alone programming or changing of source binaries on a phone.

All you need is the programmer and a phone is optional if you need to change things like binaries but really you can give a pickit5 to any technician and they can easily update supported IC’s without needing to mess around on a desktop/laptop computer. This is a very useful feature.

Now there also are EEPROM programmers and true universal programmers that can do EEPROMS and MCU’s like the old TL866II Plus which has a list here of all the supported IC’s. These programmers work well and allow easy access for single IC’s some can also read and write when an IC is in circuit but others require de-soldering. These programmers are usually quite large with Zif sockets.

EEPROM Programmer CH341B
Universal programmer the TL866II Plus

Now I just mentioned a few examples there is much more to talk about but I’m not going to be writing books here…

DIY UPDI PROGRAMMER

#117 A DIY UPDI plug and play board

Making fast and crude but reliable programmers

When getting into the new ATtiny series (tinyAVR-0 tinyAVR-1 tinyAVR-2 IC’s) of microcontrollers a few years ago I noticed how easy it had become to program them with only a few extra components.

I started out with just a 4.7k resistor and a cheap CH340 programmer. Then 3 wire hook-ups later I could easily program my IC’s. The only downside was that I had to sacrifice the UPDI pin to the dedicated Pin gods.. so I couldn’t use that pin unless I wanted to make my life more difficult.

Well this was okey for me and ever since I always have the UPDI pin open only for programming. So all my designs incorporate this principal. If I really need more pins I would use an affordable IO expander IC.

Now with that being said it’s all good and well programming with a mini rats nest… but I wanted to create a simple plug and play DIY programmer with commonly available parts and plug and play compatibility.

So I came up with a small circuit that’s easy to build on stripboard. I created a few versions over the years. Since I was the only one using this contraption I didn’t think of creating a professionally made PCB but that will come in future..

Front of the stripboard
Back of the stripboard

How do I use this?

Basically I solder the SMD package ATtiny to a suitable breakout PCB then I plug the ATtiny breakout PCB into my programmers female headers making sure the orientation is correct and presto all I need to do is upload my firmware. Then I can just remove the PCB and Plug it into my project

Simple and to the point… plus it’s been working for years.

30W LED DAYNIGHT REPAIR

#116 Repairing A 30W Day Night LED Light.

30W LED with added 2.2UF film capacitor in series with Live wire.

Over the past year I’ve had to replace multiple LED lights with Day Night switch sensors in them. after multiple failures I decided to open one up to take a closer look at the cause for failure. Usually the LEDs are running hot and driven very hard from the factory so it’s not uncommon to see many black spots indicating burnt out LEDs in the light.

One way of extending the life of the led light is to reduce the power burning out the LEDs. This can be done easily by inserting an AC film capacitor in series with the live wire before connecting it to the light. this works great for reducing power and thus reducing the brightness of the light but it did not solve my issue.

2.2uf film capacitor helps reduce the power and strain on the LEDs.

In this case my light fails to switch on. when I opened it… The LEDs were still ok now on looking at the day night sensor I determined that the circuitry had failed. Failed how?

Well I had to investigate for a bit but eventually determined that the capacitive dropper was not supplying enough current for the transistor to swich from day to night. Why?

Day night sensor in the 30W light.


Well because the capacitor value had decreased somehow.
seems that low quality film AC capacitors are used and their capacity drops maybe they deteriorate or loose electrolytic liquid I’m not exactly sure but when I replaced the capacitor with a new one everything worked again.

Faulty capacitor should be 220nf.
Replacement and faulty. Both questionable quality…

DIY LORA MODULE

#115 Making A DIY LoRa Module

2x DIY LoRa RFM95W modules with adapter boards.

When taking a closer look at the DIY aspect of lora I wanted to test LoRa peer to peer.. E.G multiple peers to one peer (NOT LoRa WAN) I noticed that the actual radio PCB is difficult to use when going the traditional through hole way… adapter boards do exist but are few and far between at least in SA. you can make your own for manufacture but then rather create your entire product PCB for manufacture.

Even with this drawback I was able to source old adapters for one of the very firs modules the: RFM95W. In South Africa we are mostly using 868Mhz although 433Mhz modules are around I don’t see them being super common in terms of LoRa modules.

The modules I used have a footprint for adding a female SMA connecter for easy antenna connecting.

Parts used:

  • 2x RFM95W 868Mhz LoRa module transceiver
  • 2x 3.3v active buzzer
  • 2x 1 pole dip switch
  • 2x TP4056 module with protection ICs
  • 2x ATtiny404 MCU
  • 2x NCV8163 3.3V LDO
  • 2x SSD1306 128×32 OLED
  • 2x 2pin 2.5mm JST battery connector
  • 2x Headers and jumpers
  • 2x 13400 3.7V 550mAh
  • 2x 10k resistors
  • 2x 220R resistors
  • 2x SOIC to DIP adapter PCB
  • 2x BC547 transistors
  • Some 0.9mm tin plated copper wire
  • Some 0.255mm PVC insulated wire
Soldering made slightly easier…

After checking the PCB I commenced with testing the devices. Unfortunately the test area has largely mountains terrain
the signal works really well and penetrates better through foliage on the mountains terrain but once there is a full on mountain in the way the signal stops. so in this retrospect the devices are better then a radio which was quite interesting but makes sense because data is being sent and uses less bandwidth then interpreting voice audio. Also the error checking for LoRa helps a lot.

Some more pros for the LoRa is that its a transceiver out of the box with RSSI functions included. Also for increased range and/or quality the spreading factor and signal bandwidth can be adjusted.

Although there’s many pros regarding LoRa I still tend to use simple 433.92 RF modules without issue at least in my situation with rural areas under about 3KM at and given point.

So it really becomes more of a cost factor than anything else. Although I’m happy selling a custom LoRa product for compatibility with LoRa WAN or some other requirement with a similar principal. Plain old generic RF is still cool in my book.

Back of one of the PCBs

WACO GP16 REPAIR

#114 Repairing a 2 year old Waco day night switch

Inside the module I noticed the relay has no cover on it and a resistor and MOV were kind of in the way of the relay terminals…

Recently I had a Waco day night switch start to show faulty signs. Namely once dusk came the relay in the device would oscillate very fast for a few seconds effectively switching the light on/off very fast making a racket while stressing the LEDs out.

The relay would finally settle… mostly on the ON setting but sometimes it would settle on the OFF position. Then if you slightly bang the relay it would oscillate and settle on the ON position.

So I removed the switch and notices there was a lot of spider webs and critters close to the external AC connections. I cleaned the outer case and also notices a brown burn mark where the relay was located. This was due to sparks from the constant ON/OFF switching since the relay in this module had no cover.

The PCB was covered in a white powdery substance.

Now technically I did not have to open the module but I wanted to see if anything else was damaged. I opened the module and everything looked good except for the relay contacts which looked a bit beat up but were working ok..

I noticed that a resistor and a varistor were very close to the relay and could technically jam it’s opening/closing capability so I moved then away from the contacts and put everything back together again.

I now tested the module again and voila there is no more oscillations. So the grimy critters on top of the AC leads and the resister MOV combo close to the relay contacts were the likely culprits however my money is on the latter. So on re-installing I added extra tape and some silicon to seal the AC terminal blocks completely. A thin layer of silicone can still easily be removed for further maintenance and/or repair.

Testing the module to make sure the relay is getting the correct voltage etc.

MULTIMETER VOLTAGE ADJUSTMENT

#113 2011 Major Tech MTD84 Voltage Adjustment

The old meter has been through various toolbox banging battles..
Guts of the old multimeter. Very crusty with lots of through hole components and electrolytic capacitors. I replaced a damaged button as well as adjusting the voltage.

I have had the Major Tech MTD84 multimeter for 11 years now. Before I went off to another city far away for my first job my father purchased it for me as a parting gift. It was a low to mid range meter at that time and seems to be currently discontinued.

Now my meter is beat up but it reads just fine for the may DC projects I make however the voltage started to drift and has slowly become less accurate.

Luckily there is a few variable potentiometers on the PCB which can be adjusted to bring the accuracy back up to spec again. They allow for the DC, AC and Temp values to be adjusted no current option from what I could see. Even though I have a few multimeters I still can’t seem to throw this old one away…

Adjustable pots found on the back for easy access. Though they are very crusty…

So in order to set the correct voltage I had to connect a known working accurate multimeter to a battery along with the faulty one. For this I used the aneng8009 which has very good current and voltage accuracy for a cheapie.. Know I slowly adjusted the voltage variable potentiometer until the volts were mostly the same on each meter. (even though the MTD84 only has 2 decimal places it’s still close enough)

Very cool now the old multimeter is not so bad anymore with all it’s through hole resistors and electrolytic capacitors… perhaps next time I’ll have to replace a dry capacitor.

Button replacement success.