Hello friends, I hope you all are doing great. Today, I am going to share the new SW-520D Tilt Switch Library for Proteus Simulation. The SW-520D is the simplest orientation sensor there is: two small metal balls in a tube that connect two pins while it stands upright and roll away when it tips over. On its common module it gets a comparator, two LEDs and a clean digital output. With this library you can run a full Proteus simulation of that module, tilt it, bump it, watch the balls in a pop-up and read it with an Arduino - including the bounce that every real tilt switch has.
SW-520D Tilt Switch Library for Proteus
Latest version: V1.0 (December 2026, this tutorial)
Related parts: SW-420 Vibration Sensor · MPU6050 Accelerometer · Active Buzzer
Helping Tools
- Serial Monitor Advance: a serial terminal window for the UART of your board
- Serial Monitor Expert: the serial monitor with extra debugging tools
- LCD Library for Proteus V3.0: 16x2, 20x4 and I2C LCDs to show your readings
The demo is a cash box tilt alarm: an Arduino UNO V4.0 watches the module inside a box, ignores a knock on the table, sounds a buzzer when somebody tips the box over and re-arms once it has stood upright again for three seconds.
NOTICE: Our team has put a lot of effort into these small parts: the bodies, the animation and the pop-ups are all our own design, and you get them free. Your feedback is the fuel that keeps us going, so please tell us what you think in the comments below or in the accelerometer board of our forum. And if our free libraries help you, you can buy us a coffee. So, let's get started with the SW-520D Tilt Switch Library for Proteus Simulation:
What Is the SW-520D Tilt Switch?
The SW-520D is a ball switch: a metal tube 5.2 mm across and about 12 mm long, closed at one end (the cap) and sealed at the other, where two gold-plated pins come out 2.4 mm apart. Inside are two small balls. While the pin end points down, the balls rest on the pins and bridge them - the switch is closed, under 10 ohm. Turn the cap end down and they roll to the cap - open, over 10 Mohm.
- Rated up to 12 V and 20 mA - a signal switch, not a power switch.
- About 100,000 operations.
- It senses one axis only: tilting it sideways, across the tube, hardly moves the balls.
- No moving contact to wear out in the usual sense, and it is sealed - but the balls bounce on every knock.
The Trip Angles
The datasheet describes the tube lying level: drop the pin end by about 10 degrees and it closes; drop the cap end by about 10 degrees and it opens. Measured from upright - the cap straight up - that means closed below 80 degrees and open above 100 degrees. In between, near level, the balls stay where they are and the switch keeps its last state. Mount it upright and it reads "closed" until the thing it is fixed to has really tipped past level.
The Module
On the common blue module, 14 x 32 mm, the tube lies along the board with its pins soldered at the far end and the tube sticking out past the edge. The circuit is short:
- VCC through 10 k to the switch node; the switch from the node to GND; 100 nF on the node.
- An LM393 comparator compares the node with the wiper of a trimmer.
- DO is the comparator's open-collector output with a 10 k pull-up.
- A power LED, and a DO LED fed from VCC through 1 k into DO.
So DO is LOW while the switch is closed - upright - and the DO LED lights; tipped over, the node rises to VCC and DO goes HIGH. (Some tutorials say the opposite; every source that names an LED agrees that the DO LED is on while upright, and with the LED wired into DO that means LOW.) The trimmer hardly matters: the node swings between 0 V and VCC, so any setting in the middle works - only turned fully to one end does DO stick.
The Bounce
Knock on the table and the balls jump. A closed switch opens for a millisecond or two, several times, before they settle. The 100 nF capacitor smooths the shortest flicks but not all of them, so DO flickers too. A sketch that switches an alarm on every edge of DO would go off at every knock - which is why the demo debounces.
Features of the SW-520D Library
- A real-looking module: the board at its true size with the gold tube, the trimmer, the LM393, the hole and the header; the PWR and DO LEDs light at Run.
- The switch of the datasheet: closed below 80, open above 100 degrees, the hold band between, and the balls' roll time.
- The chatter: a bump opens a closed switch four times within about 30 ms.
- The module circuit: the 10 k / 100 nF node, the LM393 against the trimmer, the open-collector DO with its pull-up.
- A pop-up: the module seen from the side at its angle with the two balls, the trip angles, the tilt and DO over the run; graphs, an event log and a check page.
- A complete package: about 2.7 MB with the library, our I2C LCD, resistors, LEDs and active buzzer, our Arduino UNO V4.0 and the demo, without the source code of the model.
The SW-520D in Proteus
The device is TILTSW520DTEP in TEPSW520D.LIB, under Sensors > Motion, with the model TEPSW520D.DLL. Search SW-520D or tilt in Pick Devices. Its three pins are the module's header. Its properties: PROFILE (DEMO or STEADY), ANGLE - the tilt from upright for STEADY, 0 to 180 degrees - and POT, the trimmer as a fraction of VCC.
Download SW-520D Tilt Switch Library for Proteus Simulation
Click the button below to download SW-520D-Tilt-Switch-Library-for-Proteus-v1.0.zip (about 2.7 MB, without the source code of the models):
SW-520D Tilt Switch Library for Proteus V1.0- README.txt: installation, the demo, the wiring, troubleshooting and the limits of the simulation.
- Proteus Library Files:
TEPSW520D.LIB,TEPI2CLCD16X2.LIB,TEPRESISTOR.LIB,TEPLED5MM.LIB,TEPBUZZER.LIBandArduinoV4TEP.LIB. - Proteus Model Files:
TEPSW520D.DLL,TEPLCD.DLL,TEPRESISTOR.DLL,TEPLED5MM.DLL,TEPBUZZER.DLLandTEPARDUINO.DLL. - Arduino Code: the demo sketch (Wire).
- Proteus Simulation: the demo project, its HEX file and copies of the DLLs.
No Proteus yet? Read How to Download and Install Proteus 7 and 8. New to libraries? Follow How to Install a New Library in Proteus.
How to Install SW-520D Tilt Switch Library for Proteus Simulation
- Close Proteus and extract the whole zip file, for example to your Desktop.
- Copy the files from Proteus Library Files into the LIBRARY folder of Proteus.
- Copy the files from Proteus Model Files into the MODELS folder.
- Delete
TEPSW520D.IDXfrom the LIBRARY folder if it is there. - Start Proteus, press P and search for SW-520D.
Proteus Simulation of a Cash Box Tilt Alarm
The demo uses an A4 sheet with our Arduino UNO Library for Proteus V4.0. The module (U1) stands upright inside the box; its DO goes to D2, the UNO's INT0 pin. A 16x2 LCD from our I2C LCD 16x2 Library for Proteus sits on the I2C bus at 0x27, a green LED on D5 shows "armed", a red LED on D6 the alarm, and the buzzer from our Active Buzzer Library for Proteus on D8 beeps while the alarm is on. The Arduino's monitor is built in, the same window as our TEP Serial Monitor Advance for Proteus. Open SW520D-Cash-Box-Alarm-ArduinoUnoV4.pdsprj.
The DEMO is a recorded run, played in fast time: the box stands on a table, somebody bumps the table at 2 s, then lifts the box and leans it, tips it over, holds it upside down for a second and puts it back.
The Sketch
The sketch never acts on DO directly. An interrupt records every change - when a burst of changes started, when the last one came and how many there were:
void onChange() { // INT0: every change of DO
const unsigned long t = micros();
if (!burst) { burst = true; firstEdge = t; edges = 0; }
edges++; lastEdge = t;
}
The loop waits until DO has been quiet for 100 ms and only then decides:
if (micros() - le >= STABLE_US) { noInterrupts(); burst = false; interrupts(); decide(n, le - fe); }
If DO ended where it started, the balls only bounced: a bump, counted and ignored. If it ended HIGH, the box has tipped over - ALARM. The alarm is latched: it ends only after the box has stood upright again for three seconds, so putting the box down for a moment does not silence it.
if (state == REARMING && upright && millis() - uprightAt >= REARM_MS) { state = ARMED; stamp(); Serial.println(F("ARMED again")); show(); }
SW-520D Simulation Results
Open the project and press Run. The demo runs 10.8 simulated seconds and took about 2 minutes 39 seconds on our PC with the pop-up open and the pictures being taken. Click U1 for its pop-up. Every number below comes from one simulation run, the demo exactly as it ships.
Armed
At power-on the box stands upright: the balls rest on the pins, the switch node is at 0 V, under the trimmer's 2.50 V, and DO is LOW - both LEDs on the module light. The monitor says "DO LOW: the box is upright - ARMED (a change counts after 100 ms)", the green LED is on and the LCD reads "ARMED". The pop-up at 1.0 s draws the module upright with the two balls at the pin end, inside the green part of the arc.
A Bump on the Table
At 2.000 s somebody bumps the table. The balls jump: the closed switch opens four times in 28 ms, the node rises above the trimmer each time and DO flickers - the green ticks at 2 s on the pop-up's trend. The interrupt records all eight edges, the loop waits for 100 ms of quiet, finds DO LOW again and prints "2.06 s bump ignored: 4 pulses in 28 ms". No alarm; the LCD counts "bumps 1". The pop-up at 2.3 s shows 1 bump with 4 chatter openings.
Lifted and Leaned
From 3 s the box is lifted and slowly leaned. The pop-up at 4.5 s shows it at 44.5 degrees from upright - still well inside the green part: the balls stay on the pins, the switch stays closed and DO stays LOW. A tilt switch is not a level gauge; it changes only near level.
Tipped Over
From 5 s the box is tipped over, towards 135 degrees. It passes 100 degrees at 5.533 s and the balls roll to the cap: the Event log has "the balls rolled to the cap at 101 degrees: the switch OPEN - DO HIGH" at 5.549 s. DO stays HIGH, so after 100 ms the sketch acts: "5.58 s TILTED - DO HIGH - ALARM!". The red LED lights, the buzzer beeps four times a second and the LCD reads "ALARM! TILTED". The pop-up at 6.5 s shows the box held upside down at 135 degrees, the balls at the cap, the node and DO at 5.00 V, and the module's DO LED off.
Put Back
At 7 s the box is put back. On the way it passes 80 degrees and the balls roll back to the pins at 7.423 s - "the switch CLOSED - DO LOW, the DO LED on". After the 100 ms check the sketch prints "7.46 s upright again - re-arming after 3 s upright": the buzzer stops, but the red LED stays on and the LCD shows "RE-ARMING". The pop-up at 7.6 s catches the box at 54.5 degrees on its way down, already closed.
Armed Again
The box stands still, upright, and three seconds later the sketch re-arms: "10.46 s ARMED again" - the green LED back on, the LCD reading "ARMED" with "bumps 1 alarm 1". The pop-up at 10.8 s shows the module upright and two switch changes in the whole run.
The Graphs, the Event Log and the Check Page
The Graphs page plots the tilt, the switch node, DO and the switch's state. The Event log lists every scene and every change of the switch, and the bump with its chatter. The Check page sums up the run: VCC at 5.00 V; the tilt 0 degrees now, closing below 80 and opening above 100; the switch closed after 2 changes, with 1 bump and its 4 chatter openings; DO LOW at 0.00 V; and the trimmer at 0.50 VCC, 2.50 V.
The Arduino's built-in serial monitor prints the start-up in two lines, then a line for every event - the bump, the alarm, upright again and armed again - and a status line every 2 s with the state, upright or tilted, and the counts. Its times come from millis(), which starts when the sketch starts, a few hundredths of a second after the simulation does.
Tilt Switch, Vibration Switch or Accelerometer?
They look alike on their modules but answer different questions. The SW-520D tells you which way up something is - upright or tipped over. The SW-420 in our SW-420 Vibration Sensor Library for Proteus is a spring that is closed at rest and flicks open when something vibrates: it tells you that something is shaking. An accelerometer such as the one in our MPU6050 Library for Proteus measures the actual angle on three axes and the acceleration in g - more information, more code, more cost. For "has it fallen over?" the ball switch is hard to beat.
Try It Yourself
- Click U1 while the simulation runs and use the chips: Upright, Leaning 45, Lying flat, Upside down - and Bump.
- Choose Lying flat right after Upright and then after Upside down: the switch keeps whatever state it had.
- Set STABLE_US to 0 in the sketch: the bump at 2 s sets off the alarm.
- Set POT to 0.95 or 0.05: DO sticks.
- Change REARM_MS to 10000: the box must stand still for ten seconds before it re-arms.
Ideas to Extend the Demo
- A tip-over cut-off for a fan heater: switch a relay off while the heater is tipped.
- Two switches at right angles to sense tilting in both directions.
- Count bumps per minute as a cheap knock detector.
Your Own Sketches
The demo sketch is in the zip file, and you can change it in the Arduino IDE. New to Arduino? Start with our Arduino Tutorial for Beginners. Export the compiled binary, and set the UNO's PROGRAM property to the new file. Our guide on How to get the HEX file from Arduino shows you where the file ends up.
Things to Know Before Using a Real SW-520D
- Mount it upright: the tube's cap up, so that "normal" is closed.
- One axis: it does not notice a tilt across the tube.
- Debounce: accept a change only after 50 - 100 ms without another.
- Lying flat: it sits on the switching edge and keeps its last state.
- Signal only: 12 V and 20 mA at most - drive a relay or a transistor for loads.
Troubleshooting
- DO always HIGH: the module is upside down or lying past level.
- The alarm goes off on a knock: no debounce in the sketch.
- Nothing happens when it is tipped sideways: one axis only - turn the module.
- DO never changes: the trimmer is at one end.
- The part is not simulated:
TEPSW520D.DLLis missing from MODELS and from the project folder.
Limitations of the Simulation
- The switch, its trip angles and hold band, the module circuit and the DO polarity follow the datasheets and the module's circuit; the module is our own drawing.
- Our own figures: 15 ms for the balls to roll over, a bump as four openings of 1 - 3 ms within about 30 ms, a 25 ohm sink on DO, 6 mA.
- The DEMO is a recorded run in fast time.
- Not modelled: tilt around the other axes, the spread of the trip angle between switches, wear.
- Arduino is a name of Arduino; this library is not made or endorsed by Arduino, Labcenter or the maker of the SW-520D.
- Tested in Proteus 8.5 in November 2026 (the demo as shipped). Proteus 7 is not supported.
So, that was all about the SW-520D Tilt Switch Library for Proteus Simulation. I hope it helps you build things that know when they have been knocked over. If you have any questions, ask in the comments and I will help you out. Till the next tutorial, take care and have fun!