2025-09-29 Alarm MK2


Many years ago i built a simple alarm for my garage which can be found by scrolling back on the projects site. It worked fine a couple of years until some insects found the way in end tested what happens when crawling around on the MCU pins. Also one BIG goal with this projects was to create a real project with MpLab X and move away from MikroC since they stopped supporting it several years ago!


Well anyway it was time to upgrade the alarm and also its features.


-3 IOs to be able to configure depending on application

-Battery backup with real charger circuit

-4G support for external notification.

-LCD for easy communication.

-Temperature, battery voltage and input power voltage monitoring and warning if error occurs.

-Easy installation and easy match for of the shelf housing.

-Better sealing against insects and moisture.

-Internal siren


Main components and modules used:

-MCU: 18F27K42

-Charger: BQ24450

-4G: SIM7600

-Light sensor. XH-M131



Light sensor.


There is a motor driving the garage door. When the motor is activated there is a light enabled which i will use to activate/ deactivate the alarm. For this i will cheat and use a light sensor module XH-M131 that will sense if the light is on or off. The module is cheap and works well. There is 2 variants.,one for 12V and one for 5V, but the 5V variant is hard to find. Therefore i decided to buy the 12V variant and modify it for 5V. The relay was changed to one with a 5V coil voltage. Also i created a simulation file in LTspice and saw that i needed to change R9 from 10K to 4k7 to get better sensitivity of the circuit. R8 corresponds to the LDR and has a real value of 7.5K in the dark and less then 500ohms when the light is on..

Mounting the LDR sensor is easy done by just placing it in the cavity where the LED is. One thing i noticed is that the XH-M131 pulls the relay when it becomes dark and even though the output of the modeule has NO/NC connection it's a bad solution to have it pulled and let it consume current all the time except when the light is on. The best way to solve this was to changet the +- inputs of the OP and invert it's function. It doesn't look great but works!

Mounting the LDR sensor is easy done by just placing it in the cavity where the LED is. One thing i noticed is that the XH-M131 pulls the relay when it becomes dark and even though the output of the modeule has NO/NC connection it's a bad solution to have it pulled and let it consume current all the time except when the light is on. The best way to solve this was to changet the +- inputs of the OP and invert it's function. It doesn't look great but works!

       Dark: 7.06kOhm                                                                        Bright:378Ohm

Battery Charger BQ24450.


 Keeping a battery healthy over time takes more than just charging it time to time, so i decided to use a charger circuit that has several charging states and monitors the battery level to optimize its lifetime. There are quite a few calculations to design the charger. Using Mathcad express makes the calculations really well organized and easy to follow step by step.  By adding some diodes D2/D1 in the main power supply rail one can have parallel main power and battery power without them interfering each other and still  working as backup power.


The charger solution below shows the complete design. Q1 is a load switch that shuts of the entire charger once every day to measure the battery voltage by the MCU thru Batt_Con+_ADC. If this is below 10.5V the battery is bad and it will notify user by SMS. Stat1 and Stat2 is used by MCU to detect if charging is ongoing or not and to display this on the LCD with a battery symbol. 


Design parameters:

Icharge: 300mA

Vin: 17V

Float voltage:

Boost voltage:


Verifying the charger showed really good correlation between the calculations and the measurements. One issue i struggled some time with was that i had really weird voltages on almost all nodes and it refused to charge the battery.  I tested several different ideas. When i short-circuited the pass Transistor Q5 it should charge with max current thru the shunt resistor R21 but nothing. Finally i found that the brand new, never used R21 had a resistance of more than 20kOhm. it should have 0.82ohm. Changed it to one i had with wrong footprint of course, and it worked perfect.


As seen the charging current is 321mA which is damn close to the 300mA calculated. 


Charging graph Voltage


Charging graph Current


4G Module.


For notifications and alarm i use the SIM7600 module which is the best replacement for the 3G variant SIM800. There are several models of the module and they are unfortunately more expensive the 800. But the are easy to use and to communicate with. It uses UART TX/RX, 5V, gnd , and a status pin.


The status pin toggles if the module has cellular network, this is monitored by the MCU  to display on the LCD if the module has connection or not. The 4G module is mounted on 3D printed console. It's possible to access the SIM card and it makes it easy to connect he antenna. The code to send the SMS itself is really easy to implement and works well!



void SIM7600_Send(const char *cmd) {
                            while (*cmd) {
                                        UART1_Write(*cmd++);
                                        }                 
                           __delay_ms(50); // small gap
  }




void Send_SMS(int y){

                    // Send SMS (replace number!)
                   SIM7600_Send("AT+CMGF=1\r\n");
                   __delay_ms(200);
                  SIM7600_Send("AT+CMGS=\"+XXxxxxxxxxxxx\"\r\n");
                  __delay_ms(200);

                  if(y==0)
                  SIM7600_Send("Message1\x1A");
                  if(y==1) 
                  SIM7600_Send("Message2\x1A");
                  if(y==2)
                  SIM7600_Send("Message3\x1A");
                  if(y==3)
                 SIM7600_Send("ETC....!!\x1A");
  }

The output for the Netstatus monitored by the MCU had to low voltage levels so that was level shifted for 0-5Vpp with a simple NPN transistor. The measurements below show the Netstatus, TX package data, TX rise, TX fall, TX over/undershoot and the ripple on the 5V supply during transmit mode. 

Mounting & Installation. The enclosure used is a standard casing purchased in a normal hardware store, nothing fancy. For mounting purpose of the siren, backup battery, lightsensor module and main PCB i have 3D printed plastic holders. The siren is mounted with a bracket holding both siren and a ring which contains an O-ring for sealing purposes.

One design "mistake" i made was to not realize that charging the battery and powering the charger circuit requires an input DC power of more than 15V so i went for a power supply of 17.5Vdc from a laptop.


This is also output to the siren when it's enabled but the siren module is specified for 12Vdc.  To minimize the voltage to the module i connected a 22ohm/5W resistor in series with the siren.



2025-06-03 DIY Diffprobe


Sometimes you need a diffprobe to measure differential signals or to just protect your scope when you measure on non isolated circuits that can create groundloops in the equipment. I have a diffprobe that work good for really high voltages up 5.6kV but its resolution is not well suited for low voltage electronics.


The plan was to design my own diffprobe so a started to google and stumbled upon a DIY diffprobe that seemed to work well and had well documented data.


https://hackaday.io/project/169390-a-10x-100mhz-differential-probe


Also i realized that i would need to get a VNA (vector network analyzer) to characterize the impedance.

So i just used supplied schematic and gerberfiles.



The original probe had a attenuation of 10x but i wanted a 1x probe due the lower measured voltages.


So the major specs of the probe are:


BW: 0-100MHz

Att: 1x

Vin: 35Vmax Common Mode

Cost: ~65usd inc casing, usb connector and PCB's

It was really easy to build the probe and i could even find a topic on which components to replace to achieve the 1x attenuation.


The only design i did this time is the 3D Cad of the casing. Also i had a bunch of uUSB connectors left from a previous project so i designed also a tiny PCB to mount the connector on and to have that as power supply connection for the probe instead of having some big DC jack.

The major issue was when i wanted to calibrate the probe and measured really high spikes at each switch transition, i could not find what the issue was. So i contacted the designer of the probe and he was very helpful. He tried to simulate in LT spice which errors would give the really high transients and by that try to point out where to look. 


He discovered that i had probably put wrong resistor values in a divider. 3k and 36k instead of 30k and 360k. Well with that corrected i was able to calibrate the probe and with some fine tuning both with trim capacitors and some soldered pF caps it now works perfect.


Pics below show the really high transients at Ch2 and the how it looks when correct resistors are mounted.