Mosfet gives µC joy (and maybe a new µC?)
In a previous video we looked at retiring the QX5252 workhorse of the candle project in favour of a boost converter (a modified BL8530)
The problem with measuring the light (and hence determining if it is day or night) using a comparator on the PFS154 is that the solar panel voltage dips alarmingly when the BL8530 is drawing current.
Net result? The PFS154 keeps making the wrong decision re nightfall - it mistakes a drop in voltage due to the hungry supercap/boost converter duo for a drop in voltage due to the sun disappearing for the day.
What to do?
Well I went up to the ideas factory with my furry partners, and drew the following possible solution...
...then we went to the lab and tested some switching code...
/*
MOSFET Bench Test / Blink Example for PFS154
Drives PA7 HIGH/LOW to switch a low-side N-Channel MOSFET (BS170 or AO3400).
Connections:
- PFS154 Pin 2 (PA7) -> MOSFET Gate (Pin 2)
- MOSFET Drain -> LED Cathode (-) (LED Anode + goes through 330R to VCC)
- MOSFET Source -> System GND
- 100k Resistor -> Between PA7 (Gate) and System GND
*/
#include <pdk/device.h>
#include "auto_sysclock.h"
#include "startup.h"
#include "delay.h"
// Hardware bitmask for PA7 (Bit 7 / Pin 2)
#define MOSFET_GATE_BIT 0b10000000
// Helper macros for N-Channel MOSFET control
#define turnMosfetOn() PA |= MOSFET_GATE_BIT // Drive PA7 HIGH -> MOSFET ON
#define turnMosfetOff() PA &= ~MOSFET_GATE_BIT // Drive PA7 LOW -> MOSFET OFF
void mydelay(uint16_t ms) {
while (ms > 0) {
_delay_ms(1);
ms--;
}
}
void main(void) {
// Set PA7 as Output Mode
PAC |= MOSFET_GATE_BIT;
PAPH &= ~MOSFET_GATE_BIT; // Disable internal pull-up
turnMosfetOff(); // Ensure MOSFET starts OFF
while (1) {
turnMosfetOn(); // PA7 HIGH -> MOSFET ON -> LED ON
mydelay(4500);
turnMosfetOff(); // PA7 LOW -> MOSFET OFF -> LED OFF
mydelay(4500);
}
}
unsigned char STARTUP_FUNCTION(void) {
AUTO_INIT_SYSCLOCK();
AUTO_CALIBRATE_SYSCLOCK(TARGET_VDD_MV);
return 0;
}
...and finally we made the following video to explain the possible update to the candle project.
Also, please stay to the end of the video for an announcement of what might be the next iteration in this project.







