A complete sleep‑monitoring pipeline built around a Raspberry Pi Pico W, using DIY pressure sensors. It uses a Wi‑Fi web server for data transfer, a Python script to download the data in CSV format, and MATLAB for visualization and analysis. Future goals include interpreting the data and developing a smart alarm that wakes the user during light‑sleep phases
The system collects pressure deltas in four different positions on a mattress during sleep and stores it for later analysis.
The figure below summarizes the full workflow:
- Real‑time movement detection and event logging
- Low‑cost DIY pressure‑sensor array using velostat
- Embedded Wi‑Fi web server running on the Pico W for data transfer
- One‑click data download via Python script
- MATLAB tools for visualization and analysis
- Calibration system to normalize sensor sensitivity
- End‑to‑end workflow: hardware → firmware → data → analysis
- Arduino IDE 2.x + Raspberry Pi Pico Board 4.5.2 or later
- Python 3 with the
requestspackage (pip install requests) - MATLAB 2019a or later
- Sensor calibration
- Reads the pressure‑sensor matrix
- Detects and stores movement events
- Serves a Wi‑Fi web server for data transfer in CSV format
- Based on velostat, a pressure‑dependent resistive sheet material
- Each sensor is a 3cm × 3cm velostat square between two 2.5cm × 2.5cm aluminum‑foil electrodes
- Resistance varies with applied pressure
- Four sensors are placed under the mattress at torso height
- Although physically in a line, they are electrically connected in matrix form to reduce wiring
- Each sensor is modeled as a pressure‑dependent resistor
- To build a sensor, cut a 3cm × 3cm square from a velostat sheet, and place it between two 2.5cm × 2.5cm aluminum‑foil electrodes
- Two external 1.5 kΩ resistors connected between GP26/27 and GND are needed to provide a correct reference
- Other values in the range 0.5kΩ to 5kΩ can be used: edit
#define R0 1.5inSensors.h - Working principle: SENSOR_LINE_1/2 are alternately driven to Vcc and GND, while measuring SENSOR_COL_A/B voltages
- The system of four equations and four unknowns (the sensor resistances) is solved iteratively in
Sensors.cpp - Optional: A switch allows to turn ON/OFF the Wi-Fi. You can also use the serial-monitor command W to toggle Wi-Fi state, indicated by the built-in LED
a) Sensor detail b) Breadboard assembly
Sensors connected in matrix form
- To set up Wi-Fi, edit your credentials char ssid[] and char password[] in WiFiControl.cpp. Alternatively, create a different .cpp file and place them there (no header file needed)
- When you power the Pico W, it will try to connect to your Wi-Fi. Using a serial monitor, you can view its progress
- If Wi-Fi successfully connects, the IP running the web server will be printed; For example:
IP: 192.168.1.21 - The built‑in LED in the Pico W shows the Wi-Fi status
- W - Toggle Wi-Fi state ON/OFF. The built-in LED switches according to Wi-Fi State
- P - Prints the sensor readings in real-time. Useful to view in graph format with the Arduino IDE Serial Plotter
- CS - Start the calibration process (instructions below)
- CE - End the calibration process (instructions below)
- E - Prints recorded events in raw MATLAB format: [time (seconds) , sensor ID (0 to 3) , pressure]
- The code comes with a default calibration based on prescribed dimensions, so it's not mandatory to calibrate it, although it's useful to compensate individual differences between sensors
- The sensor array should be in its final position under the mattress
- Use CS to start the calibration process. If Wi-Fi is OFF, the built-in LED will turn ON
- Press and release the mattress in each of the 4 sensor positions in sequence
- Apply approximately the same pressure to all four sensors
- Use CE to end the calibration process
- The calibration algorithm adjusts the sensitivity of each sensor to map the pressure applied during calibration to the value of 1000
- The calibration is stored in permanent memory, so you don't need to recalibrate every time you power the Pico W
- Using P in the serial monitor activates real‑time printing of sensor data
- Open the Arduino IDE serial plotter to view in graphical format
- Below is an example of the serial plotter
Serial plotter during calibration
- Make sure the Pico W web server is ON by checking the LED status
- Make sure your Python has
requestsinstalled - Run
analysis/get_events.pyto download the data and createevents.csv
get_events.pysearches for local web servers that respond to 192.168.1.x/events, with x from 1 to 64, as it is very likely to be in that range- Whenever you turn ON the Wi-Fi, its IP is printed on the serial monitor
- If you know the IP, you can view the data in the browser by accessing http://IP/events
- Use MATLAB script
analysis/create_dataset.mto convert raw CSV logs inevents.csvto the.matformat - Select a name by editing
output_name = 'example_name.mat' - Optionally, you can add timestamps by editing
labelsandlabels_time
- Running
analysis/sleep_analysis.mwill load the.matfile and generate:- Plot of pressure over time
- Annotated events (if present in the file)
- Histogram of pressure distribution
- If you don't have a dataset, you can test the visualization with
dataset/synthetic_sleep.mat
Events over time and pressure distribution
- Basic sleep‑stage estimation from pressure patterns
- Smart alarm triggered during light‑sleep phases
- Higher‑resolution sensor array (if needed for the goals above)
- Temperature tracking during sleep
- Battery‑powered portable version

