AI Lamp - How to Use AI in Arduino!
by mondal3011 in Circuits > Arduino
63879 Views, 325 Favorites, 0 Comments
AI Lamp - How to Use AI in Arduino!
This lamp was conceptualized because of an interesting bet I had with a friend. He claimed that it is impossible to run an AI model on a basic microcontroller like an Arduino. But here's the thing - AI isn't a magical piece of software. It's essentially just a math formula that spits out predictions based on inputs.
You read that right! All the fancy AI models you've been hearing about - LLMs like ChatGPT, code generators, story completion software, music generators, and even models that predict cancer. Under the hood, they all are just mathematical formulae. However, they are very complex. If you write them down, it would take hundreds or even thousands of pages to complete writing. But not all AI models need to be complex. Some can be simple. Like the one I am going to show how I built.
In this Instructable, I'll walk you through how I built this smart AI lamp that automatically adjusts to surrounding brightness after learning the specific brightness levels that you are comfortable with. All powered by a basic AI model running directly on an Arduino! Sounds great? Then let's start!
Supplies
Arduino UNO https://a.co/d/gfj3Saa
Male-Female jumper wires https://a.co/d/1HzvSw1
Potentiometer https://a.co/d/gRAjS5N
LED
10K resistor https://a.co/d/13zsMSa
LDR (Photoresistor) https://a.co/d/709Vs7p
5v power supply https://a.co/d/iFgz31o
Adhesive
Understanding AI
My brother, who is working in the AI domain, helped me understand a lot about the basics of AI and machine learning. I realized making a physical project helped me understand about AI in a very intuitive way. So, I aim to make this Instructable also a guide to beginners in understanding the working and basic concepts of AI.
Artificial Intelligence is quite a fancy term in my opinion. The goal here is to generate an action or output based on some observations or inputs.
For example, while using ChatGPT, your prompt is the input and the GPT’s answer is the output. Similarly, in, say, cancer prediction systems, the camera image of the tumour (for example) is the input and the stage of cancer predicted is the output.
So, there is this “brain” or AI that takes in the input and spits out the output. And this brain, ladies and gentlemen, is the mathematical formula I was talking about. It works like any formula you’ve read at school. Say you have a very simple AI model that inputs two numbers and outputs a different number. When you input 1 and 2, you get 3 as the output. When you input 3 and 4, you get 7 as the output. So for a set of inputs of a, and b, what is the “brain” doing? The brain is just the formula a+b. See? That’s what AI is. A formula.
When we say we are “Training” an AI model, we are essentially trying to find the secret formula that works for a set of inputs and outputs.
Now that you understand AI, let's build and train one!
Build the Lamp
The lamp was built using some pieces of card paper. It was inspired from an IKEA lamp design.
Collect the required materials - black and white card paper, an old lid for the top, scissors, glue. Then, use the template provided in this step to make the lamp shape on the papers.
Use the template to cut out the necessary pieces from the card paper. I recommend using white paper for the stand and black for the shade, it looks pretty cool.
Begin by assembling the lampshade using adhesive.
Downloads
Attach the LED and Assemble
Use a small white LED light and solder wires to it. Don't use a high power LED as the Arduino might not be able to power it.
Use a small amount of white adhesive or double-sided tape to temporarily secure the LED in place.
Thread the wires through the lampshade. You may need to create a small opening at the back of the lampshade, which is basically the old lid.
Once the LED is in position, it's time to attach the lampshade to the base. Apply a generous amount of strong adhesive to the rim of the base. Carefully align the lampshade with the base, making sure it is centered and straight. Once aligned, gently press the lampshade onto the base, applying even pressure to ensure a secure bond. Let the adhesive dry completely. This may take some time, so be patient and avoid disturbing the lamp.
Prepare to Collect Data
Every AI model needs to be trained on a dataset. This is what makes an AI model different from standard coding. The model learns from the dataset and tries to find a relation between the inputs and the output. This way, the behavior of the model is tailored to the characteristics of the dataset. Why is this important? By doing this, you are tuning the model to adjust the brightness of the lamp to your specific case. The brightness adjustments will be perfectly suited to your preferences. Confused? Follow along and everything will be clear.
First, we will create the dataset by collecting data of surrounding brightness and LED brightness. The Arduino itself will be used to collect data.
First, build the circuit as shown in the diagram. This circuit is a data collection system. The push button works like a snapshot. Every time you press it, it records the readings from the LDR and the brightness output of the LED into the serial monitor.
Begin Data Collection
Upload the attached code to your Arduino board.
Here's what we will do:
- Set up the lamp with the Arduino. Make sure the LDR is not facing the light from the LED. We don't want it to read the LED's light when the lamp turns on. A good place for the LDR is the back side of the lamp base.
- Move the lamp to different places around your house, exposing it to different surrounding brightness levels.
- At each suitable spot, adjust the brightness of the LED to your preferred level using the potentiometer.
- Press the button to record the LDR reading and LED brightness onto the serial monitor.
- Collect 20 to 50 data points (or 100 if you have patience) by repeating steps 2 to 4 throughout the day.
By doing this at different places and times of the day, you are recording your preferred lamp brightness values for different conditions.
We will use this data to train an AI model. The larger the dataset, the better will be the accuracy.
Downloads
Export the Data to CSV
Now we need to save the recorded data so that we can train our model on it. Copy the collected data from the serial monitor and paste it in a notepad (TXT document)
Now, rename the document to replace the .txt extension with .csv. Windows might throw a warning, but you can press "Yes"
Now, when you click on the file, it should open in Excel. You will find two columns of numbers - the first column being LDR readings and second LED brightness values. I recommend adding the column names at the first row to make the python programming easier in the next step.
Train Your AI Model
This is the fun (and a bit technical) part. Now we will create an AI model. I recommend using python. While you could use any other language and write a program from scratch to train a regression model, python has some neat libraries that will make our jobs much easier. Don't worry if you are new to this, you can use my code attached in this step and eventually learn to code in python in the process :)
As I already mentioned (so many times that you might be frustrated by now), AI is basically a mathematical formula. We are about to train an AI that will generate the best formula to produce the right LED brightness values based on the LDR readings. The model will learn this from the dataset we collected.
Linear Regression
We are going to use a python library called scikit-learn (or sklearn). This library already contains the code to train a linear regression model, so all we need to do is import it and send it our data.
But what is a linear regression model? It is a simpler version of neural networks (another fancy kind of AI model). This method of machine learning involves fitting a line through our data to create an interpolation that can be used to predict future data.
Too many technical terms? Well, all you need to know for this project is that, linear regression will generate an equation of the form y = wX+b. Here, y is the prediction (LED brightness), X is the input (LDR reading), w, and b are the parameters that are to be obtained by training the model.
Create and train the AI model
Make sure to install sklearn and pandas by running pip install scikit-learn and pip install pandas respectively on windows command prompt (CMD). Pandas will help us read the CSV file and scikit-learn will create our model.
Once done, replace the CSV file name with yours. Then, when you run the code, sklearn creates a linear regression model (our AI brain) that will try to relate the inputs (LDR) to outputs (LED brightness)
Linear regression basically fits a line with equation y = mX + c through the dataset. In our case, y is the prediction, X is the input, m is the slope of the line (weight) and c is the y intercept (bias). So, our equation becomes prediction = w*input + b.
Once the model is trained, we can obtain the w and b values to use in our Arduino code. As you see from my code above, I have obtained w = 0.2487 and b = 149.9255. Don't copy my values and use it in yours! These numbers are tailored to the brightness of my surroundings and my preferable LED brightness levels. Things may be different for you.
Notice how I generated a prediction for an LDR input of 83, obtaining a predicted LED brightness of 170.57. It is pretty close to the actual LED brightness of 171 from the dataset (above).
Downloads
Add the AI Formula to the Arduino
And finally, we use the learned model parameters in the Arduino so that it can automatically start adjusting the brightness of the lamp.
We create a new Arduino code where the predicted brightness is obtained from the same equation: prediction = w*LDR input + b.
We have obtained w and b after training the model in the previous step. By inserting these values in the Arduino code, we can have it start doing its thing.
You can now remove the push button and the potentiometer, since they were only there to collect data. Now your lamp will adjust the brightness without you having to fiddle with a potentiometer. Because it has learned your preferences! That is AI, my friend :)
Downloads
Final Assembly
Finally, hide all the electronics inside the base of the lamp. Now your lamp will adjust the brightness according to your needs! You don't even have to touch it. Just turn it on and forget it!
Now you may ask was AI necessary for this? There are hundreds of tutorials on how to use an LDR to automatically adjust the brightness of a lamp. Well, the answer is yes and no. You don't need AI for auto brightness. But generic auto brightness will only adjust the brightness in a predefined way. You may have to fiddle with a potentiometer until you get your desired brightness for different situations. But with this method, you are training the lamp to understand your needs. How much brightness you need at different situations.
But mostly, this project is a guide on getting started with AI and explaining what AI really is. You can make all sorts of things with this. You can have a weather prediction system with temperature, humidity, pressure, and air quality sensors (and maybe data from the internet as well) and use the data from the sensors to predict the weather and warn you if you are about to put your clothes in the washing machine. Or maybe you can have an accelerometer and gyroscope attached to a glove and train it to learn sign language and translate it. All of this can be done with a microcontroller like Arduino or esp8266. Just train the model to obtain the weights and biases from a computer and insert them into the microcontroller.
I hope this project got you interested in AI and encouraged you to learn more about it to create more interesting projects!