A Smart Lamp That Uses Sleep Cycles to Improve Sleep Quality

by Arpan Mondal in Circuits > Microcontrollers

1568 Views, 37 Favorites, 0 Comments

A Smart Lamp That Uses Sleep Cycles to Improve Sleep Quality

This lamp will improve your sleep quality using science
Instructables_thumbnails.png
VID_20241230_1503567922-ezgif.com-crop.gif
Screenshot 2025-01-07 182812.png

This is a smart sleep-assisting lamp that could (probably) improve your sleep and help you feel more refreshed in the morning. I developed this concept with inspiration from online sleep calculators and some research I did online.

Now, let me be clear, it won't magically make you feel better if you're sleep-deprived due to overworking or excessive TikToking. Think of this lamp as an enhanced alarm clock replacement that primarily does two things:

First, it gently wakes you, simulating the rising sun. When it's time to get up, the lamp gradually increases in brightness, transitioning from a warm orange to a bright white, mimicking a beautiful sunrise over a period of five to ten minutes.

The second part is even more intelligent. It actually calculates the optimal time for you to wake up, based on your desired wake-up time and your actual sleep time, and ensuring you're roused at the end of a sleep cycle.

Read on to learn more about the concept.

Supplies

Xiao esp32C3: Link to buy

Ws2813 addressable LEDs: Link to buy

IR proximity sensor: Link to buy

A damaged laptop screen to recycle the diffusion sheet (or a standard diffusion sheet)

Jumper wires

Soldering iron

Wood / Foam board

Paint

Theory

Screenshot 2025-01-07 185755.png
Screenshot 2025-01-07 190011.png
Screenshot 2025-01-07 190003.png

To understand how the lamp works, we need to understand the concept of sleep cycles.

When you fall asleep, you go through 4 main stages. Stage 1 is light sleep. This is the transition between wakefulness and sleep, lasting for about 5-10 minutes.

Then you go into a slightly deeper sleep. In this stage, your body temperature drops, and your heart rate and breathing become more regular.

Stage 3 is deep sleep. It’s harder to wake up during this stage, and if you do, you might feel disoriented.

Finally, you go into REM sleep, most vivid dreams occur during REM sleep. Your muscles are temporarily paralyzed to prevent you from actually getting up and running when you are running a marathon in your dream.

All these four stages make up one sleep cycle which lasts for about 90 minutes. And you experience several of these cycles during the night. While the duration may vary a bit from individual to individual, the average seems to be about 90 minutes based on several studies.

A fairly old paper by Vlasta Brezinova, for example, investigated the sleep cycle duration of groups of different aged individuals. While there were some differences in the durations, the average duration was about 90 minutes. Other research papers seem to agree on this too. I have written down my research on this google doc. Feel free to go through it for more details.

Online Sleep Calculators

Screenshot 2025-01-07 190558.png
Screenshot 2025-01-07 190548.png

Online sleep calculators like this one are simple tools designed to help you figure out the best time to go to bed or wake up based on the concept of sleep cycles.

They work backward from your desired wake-up time: You input the time you need to wake up.

They calculate optimal bedtimes based on sleep cycles: Using the average 90-minute sleep cycle length, they calculate back in 90-minute increments to suggest several possible bedtimes. These bedtimes are designed to allow you to complete a certain number of full sleep cycles before your alarm goes off.

Example:

If you need to wake up at 7:00 AM, a sleep calculator might suggest the following bedtimes:

  1. 10:00 PM (for 6 full sleep cycles + time to fall asleep)
  2. 11:30 PM (for 5 full sleep cycles + time to fall asleep)
  3. 1:00 AM (for 4 full sleep cycles + time to fall asleep)


We are going to do almost the same thing but in reverse.

The Concept

collage.jpg

Like I mentioned, the lamp figures out the best time for you to wake up, based on when you want to wake up and when you fall asleep, and makes sure you're roused at the end of a sleep cycle.

Let me explain how that works. Let's say you sleep at 10:30 PM and you want to be up by 6:00 AM. The lamp does some quick math and calculates the end times for three, four, and five sleep cycles. That would be 3:00 AM, 4:30 AM, and then right at 6:00 AM. In this case, the lamp would wake you up at 6:00 AM.

Now, if you go to bed a bit later, say 11:30 PM, the same process happens. The end of the third cycle would now be 4:00 AM, the fourth at 5:30 AM, and the fifth at 7:00 AM. Since your target wake up time is 6:00 AM, it checks which of those cycle end times is closest. Here, it's the end of the fourth cycle, at 5:30 AM. So, that's when the lamp would wake you up.

Figuring Out Sleep Time

VID_20241230_143604640-ezgif.com-optimize(1).gif
Screenshot 2025-01-07 182917.png
Screenshot 2025-01-07 191506.png

Now, you might be wondering, 'How on earth will this lamp know when I actually fall asleep?' Well, the truth is, it doesn't know exactly. But, it makes a very clever educated guess. And to do this, we make two key assumptions.

First, we figure that most people spend a little time winding down before sleep, maybe scrolling through their phone or reading a book in bed. Second, we rely on some past research that suggests the average adult takes about 15 minutes to fall asleep after they've laid down for the night. This 15-minute window is also commonly used by those online sleep calculators you might have seen.

So, here's what we do: we attach a little proximity sensor underneath the lamp. Once you're done with your phone or your book and you're ready to drift off, you simply place it in front of the lamp where the proximity sensor detects it. The lamp then waits a short period, just to make sure you're not picking it back up, and then it turns itself off. And that's when it starts a 15-minute countdown, assuming that's roughly when you'll fall asleep. After those 15 minutes are up, the wake-up time is calculated exactly the way I described earlier.

Lamp Design

Screenshot 2025-01-07 192321.png
Screenshot 2025-01-07 192333.png
Screenshot 2025-01-07 192420.png
Screenshot 2025-01-07 192400.png
Screenshot 2025-01-07 192427.png

The lamp shade itself can actually be 3D printed. If you're interested in that, you can find the STL files I made below. Now, you might notice my lamp has this beautiful wooden frame and base. But it's not wood at all! It's foam board!

We need two U-shaped sections. Each has a little protrusion underneath to create space for the proximity sensor, and a notch in the middle so they can connect together.

Then I made a base out of an empty roll of cellophane tape.

Diffusion Sheet

Screenshot 2025-01-07 192624.png
Screenshot 2025-01-07 192602.png
Screenshot 2025-01-07 182917.png

To diffuse the light, we're going to use an interesting material. This is actually a diffusion sheet salvaged from a broken laptop screen. You can usually find these pretty easily at a local electronics scrap shop. This sheet is perfect for our project because it diffuses light beautifully, giving it a nice frosted glass look.

The Realistic Wood Look

Screenshot 2025-01-07 192753.png
Screenshot 2025-01-07 192806.png
Screenshot 2025-01-07 192942.png
Screenshot 2025-01-07 192954.png
Screenshot 2025-01-07 193018.png
Screenshot 2025-01-07 193006.png

Now, let me show you how I gave the foam board that realistic wood grain appearance. First, I mixed up a light brown acrylic paint and coated all the surfaces with it. It's important to cover the edges evenly as well. Once that's done, just let it dry completely.

Next, I grabbed a darker shade of brown watercolor and diluted it with a bit of water. Using a sponge, I gently painted the foam pieces with long, linear strokes. Make sure to run the sponge in the same direction across all the surfaces, mimicking the natural grain of wood. Don't forget to cover those edges with the darker paint, too! Once all the pieces are coated, set them aside to dry completely.

Finally, we need an even darker brown to really bring out the detail. I mixed some brown watercolor with just a touch of black. This time, I diluted it a little less than before. Using the same sponge technique, I applied strokes just like we did earlier, ensuring they all run in the same direction. Let it dry one last time, and you have a beautiful wooden finish! Nobody will even guess there's foam board underneath!

Electronics

Screenshot 2025-01-07 193335.png
Screenshot 2025-01-07 193347.png
Screenshot 2025-01-07 193447.png

While those are drying, let's shift our focus to the electronics. At the heart of our lamp, we'll be using a tiny ESP32 board called the Xiao ESP32S3. This is essential for retrieving the time from the internet and ensuring the LEDs receive the correct signals.

For the light source, we're using a high-quality WS2813 addressable LED strip. A good quality strip is vital for accurately reproducing colors, particularly when you're aiming for those relaxing, ambient effects in the evening.

For both of these, I've used electronics from Seeed studio as their components are highly reliable and the LEDs are of very good quality. You can find them here: https://www.seeedstudio.com/

We'll also be using a small IR proximity sensor. You don't need a high-end one; a basic, affordable one will work just fine.

Let's get started by cutting our LED strip down to 10 LEDs. Now for the wiring – follow the connection diagram in the image above. To make sure everything's working correctly, I wrote a simple test code that produces a soft, warm white light. You can find the code below.

Connections

Screenshot 2025-01-07 193739.png
Screenshot 2025-01-07 193702.png
Screenshot 2025-01-07 193714.png
Screenshot 2025-01-07 193749.png

Now we can move on to connecting all the electronics. For the Xiao ESP32, I recommend creating a small socket with wires soldered to female headers. I've shown how to make this in a previous project. Unfortunately, right around this point, my soldering iron decided to give up on me! So, for the remaining connections, I had to use jumper wires. You can follow the wiring diagram that's on screen to make your connections.

Make sure to leave three female jumper connectors free underneath for the proximity sensor that we'll connect later. Okay, it might not be the prettiest wiring job, but since it'll be hidden inside the lamp, it's perfectly functional for now. Once my new soldering iron arrives, I'll definitely be tidying things up with proper soldered connections.

Assemble

Screenshot 2025-01-07 193923.png
Screenshot 2025-01-07 193911.png
Screenshot 2025-01-07 193931.png
Screenshot 2025-01-07 194004.png
Screenshot 2025-01-07 193949.png
Screenshot 2025-01-07 193938.png
Screenshot 2025-01-07 194025.png

Grab the rubber adhesive and apply a generous amount to the notches of both the U sections. Let the adhesive air dry for about 5 minutes before sticking the two pieces together. This creates a much stronger bond when you press the pieces firmly together.

Next, take the diffusion sheet and carefully wrap it around the base. Apply some rubber adhesive to the overlapping ends and secure them together. Do the exact same thing for the base cover. If you noticed, I didn't glue the diffusion sheet or the base cover directly to the base itself. This is to allow me to easily remove them later if I ever need to troubleshoot or access the electronics.

You'll also notice I've created a small slot in the base. This is specifically for the USB-C cable, allowing us to power the ESP32 and reprogram it if needed without taking everything apart.

Finally, carefully slide the entire base assembly between the two glued U-sections.

Attach the proximity sensor underneath with double sided tape. And just like that, the main structure of our lamp is complete!

Code

Screenshot 2025-01-07 194254.png
Screenshot 2025-01-07 194302.png
Screenshot 2025-01-07 194313.png
Screenshot 2025-01-07 194308.png

Now it's time to upload the main code to our ESP32. It largely follows the same principles we discussed at the beginning.

The code uses the internet to fetch the current time. This can be eliminated if you're using an RTC module. It has a function named runSunriseAnimation() that simulates that beautiful sunrise animation I mentioned earlier. And the function calculateWakeTime() calculates the wake time based on the current time (sleep time). It assumes a 15 minute time to fall asleep and 90 minute sleep cycle. It then calculates the closest time to wake you up based on a set ideal wake up time (7:00 AM in the code)

Now, to be honest, my code might not be perfect, so I really encourage you to take a look at it. If you spot any areas for improvement, please do share your suggestions in the comments – I'd love to hear your ideas and learn from you. For now though, the code is functioning as intended and will get our lamp working.

You can find the code below.

Conclusion

Screenshot 2025-01-07 182903.png
Screenshot 2025-01-07 182842.png
Screenshot 2025-01-07 182917.png

And there you have it! Our sleep cycle lamp is finally complete! Doesn't it look fantastic? Honestly, who would ever guess it's not real wood? Plus, we gave a new life to a discarded laptop screen – that's a win-win!

Now, using it is simple. Before you start winding down for the night – whether you're catching up on your phone or enjoying a book – just turn the lamp on. Then, when you're ready for sleep, place your phone on the table in front of it and drift off. The lamp will handle the rest, gently turning off and waking you up at the optimal time, all without disrupting your sleep cycle. I’d like to have a speaker to produce sounds along with the sunrise effect though.

Thanks for reading till here!