AI Lamp – How to Use AI in Arduino – UDEMIE

AI Lamp - How to Use AI in Arduino!
AI Lamp - How to Use AI in Arduino!
AI Lamp - How to Use AI in Arduino!

This lamp was created because of an intriguing bet I had with a friend. He argued that it would be impossible to run an AI model on a basic microcontroller such as an Arduino. But here’s the catch: AI isn’t some mystical piece of software; it’s essentially a mathematical formula that generates predictions based on given inputs.

That’s right! All the sophisticated AI models you’ve heard about—large language models like ChatGPT, code generators, story completion tools, music creators, and even cancer prediction models—are fundamentally mathematical formulas. These formulas, while complex, are just that: math. If you were to write them down, it would take hundreds or even thousands of pages. However, not all AI models need to be that complicated. Some can be straightforward, like the one I’m about to demonstrate.

In this Instructable, I’ll guide you through the process of building a smart AI lamp that automatically adjusts to the surrounding brightness after learning the specific levels you are comfortable with. This entire system is powered by a basic AI model running directly on an Arduino! Sounds amazing, right? So, let’s get started!

Supplies

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

Step 1: Understanding AI

Understanding AI
Understanding AI

This lamp was conceptualized due to an intriguing challenge posed by a colleague. He asserted that running an AI model on a basic microcontroller like an Arduino was impossible. However, the essence of AI is not an enigma; it is fundamentally a mathematical formula that generates predictions based on input data.

Indeed, all the sophisticated AI models we frequently hear about—such as large language models like ChatGPT, code generators, story completion tools, music creators, and even cancer prediction models—are essentially complex mathematical equations. These equations, while intricate, are just math. Documenting them could fill hundreds or even thousands of pages. However, not all AI models require such complexity. Some can be relatively straightforward, like the one I am going to demonstrate.

In this Instructable, I will guide you through the process of creating a smart AI lamp that adjusts to the surrounding brightness by learning the specific levels that you find comfortable. This entire system is powered by a basic AI model running directly on an Arduino. Sounds impressive, doesn’t it? Let’s begin!

My brother, who works in the AI domain, provided me with a foundational understanding of AI and machine learning. I realized that creating a physical project allowed me to grasp AI concepts in a very intuitive manner. Therefore, my aim with this Instructable is to also serve as a guide for beginners to understand the workings and fundamental 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!

Step 2: Build the Lamp

Build the Lamp
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.

Attachments

Step 3: Attach the LED and Assemble

Attach the LED and Assemble
Attach the LED and Assemble
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.

Step 4: Prepare to Collect Data

Prepare to Collect Data
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.

Step 5: Begin Data Collection

Begin Data Collection
Begin Data Collection
Begin Data Collection
Begin Data Collection

Upload the attached code to your Arduino board.

Here’s what we will do:

  1. 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.
  2. Move the lamp to different places around your house, exposing it to different surrounding brightness levels.
  3. At each suitable spot, adjust the brightness of the LED to your preferred level using the potentiometer.
  4. Press the button to record the LDR reading and LED brightness onto the serial monitor.
  5. 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.

Attachments

Step 6: Export the Data to CSV

Export the Data to CSV
Export the Data to CSV
Export the Data to CSV
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.

Step 7: Train Your AI Model

Train Your AI Model
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).

Attachments

Step 8: Add the AI Formula to the Arduino

Add the AI Formula to the Arduino
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 🙂

Attachments

Step 9: Final Assembly

Final Assembly
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!

Made with AI - Autodesk Design & Make - Student Contest

Leave a Reply

Your email address will not be published. Required fields are marked *