Tutorial
Simulate a microcontroller with its analog circuit
In BetterSPICE the MCU part runs a short program, written on its Code tab, in simulated time, and its pins are real circuit nodes. The program drives an LED, reads a pot and sets a PWM duty while the analog circuit around it is solved at the same time. This tutorial starts with the smallest program, an LED blink, and ends with a pot-controlled fan.
28 Nov 2026 · Arwid Vasilev
Steps
- Open the example MCU blink (Examples, Digital control). It is loaded in the viewer below: a 3.3 V supply, the MCU U1, a 330 Ω resistor and an LED.
- Run it (Space in the viewer). V LED pin goes between 0 V and 3.19 V every 500 ms. The pin sags below 3.3 V because an output has a drive resistance. I LED is about 4 mA while it is high.
- Check the current by hand. The LED drops about 1.8 V, so R1 has 3.19 − 1.8 = 1.4 V across it, and 1.4 V / 330 Ω is about 4 mA.
- Open the Code tab of U1. The program is
pin LED out, thenloop,LED toggle,wait 500ms,end. Change500msto100msand the LED blinks five times a second. - Close the analog loop in the desktop app: open MCU pot to PWM fan speed. The program reads the pot with the ADC and sets a 25 kHz PWM duty,
FAN = POT / 3.3V, which switches a MOSFET and a 12 V fan with a flyback diode. Move the pot and the duty and fan current follow. - If the status line under the code shows a warning, open Diagnostics (Alt+6). A floating input or a missing pull-up warns, as on a bench. The language is in chapter 42.
Questions people ask
Which microcontrollers can I use?
The generic MCU part has the pins you declare in its code. You can also pick a real package variant, such as ATmega328P, ATtiny85 or STM32F103C8, and then the pin names are the chip's own.
Does it run my real firmware?
No. It runs a small pin language written for the simulator, in simulated time. The Code tab can export an Arduino sketch or an STM32 HAL file as a starting point, which is not finished firmware.
Do the pins behave like real pins?
Yes. An input can float, an output has a drive limit and a resistance, and I²C needs pull-up resistors. A warning says what to add.
Try it yourself
The viewer and the full app run in your browser. The desktop app adds the vendor model library and ngspice.