Light Intensity Plotting Using Arduino and Python's Arduino Master Library

About: A 17-year-old kid. The solo developer of Arduino_Master and Color_Console module in python. Programmer, designer, Scientist , Technology freak.

Arduino being an economical yet highly efficient and functional tool, programming it in Embedded C makes the process of making projects tedious! Arduino_Master module of Python simplifies this and lets us perform calculations, remove garbage values, and plot a graph for a visual representation of data.

If you don't know about this module yet, install it using the command pip install Arduino_Master

Don't worry if you don't know how to use this module, visit this link => Arduino_Master

However, the code for this project will always be available in this instructable.

Supplies:

For this project, you'll need the following:

  1. An Arduino
  2. A Light Dependent Resistor (LDR) and
  3. Python 3 installed on your computer.

Step 1: Building Your Circuit :

We'll be using pin A1 of Arduino to get input data. You can also use the 5V and GND pins of Arduino instead of the Battery. Make the connection as follows :

  1. Connect one end of the LDR to the positive terminal of a 5V battery or to the 5V pin of the Arduino.
  2. Connect the other end of the LDR in parallel to pin A1 and negative terminal of the battery or GND pin of Arduino.
  3. Use a resistor to make sure all of the current doesn't flow to the GND which would result you in not getting a strong enough signal to sense at A1 terminal of the Arduino. (Am using a resistor of 10k ohms).

Step 2: Programming Your Arduino :

Arduino_Master module uses Serial Monitor of the Arduino to send and receive data. The advantage of using this module is, once you program your Arduino, you can change the python program alone for different projects since programming in python is comparatively easier!

Code:

// LDR_1 variable is used to denote pin A1 of Arduino.

int LDR_1 = A1;

// Data received from A1 will be stored in LDR_Value_1.

float LDR_Value_1;

String input;

void setup() { pinMode(LDR_1,INPUT); // LDR_1 is set as an INPUT pin. Serial.begin(9600); // Communication baudrate is set at 9600. }

void loop() { if(Serial.available()>0) // if any input is available in the serial monitor then proceed. { input=Serial.readString(); // Read the input as a string. if(input=="DATA") { LDR_Value_1=analogRead(LDR_1) * (5.0 / 1023.0); // (5 / 1023 ) is the conversion factor to get value in Volts. Serial.println(LDR_Value_1); // If input is equal to "DATA", then read input from LDR_1 and print it on the Serial Monitor. } else int i=0; // if input not equal to "DATA" , do nothing ! }

}

Step 3: Programming Python to Graph Data From Arduino:

Each and every LDR would have its own resistance values and we have to remember that no to electronic components are ever exactly identical in operation. Thus first we have to find the voltage at different intensities of light.

Upload the following program to your python IDE and run it:

Do this for different intensities of light and using the graph draw a conclusion say for example if the intensity is less than 1, the room is too dark. For intensity between 1 and 2, the room is considerably dark. For intensity greater than 2, the light is switched On.

<p># Importing Arduino_Master module</p><p>from Arduino_Master import *</p><p># collecting data
data=filter(ardata(8,squeeze=False,dynamic=True,msg="DATA",lines=30),expected_type='num',limit=[0,5])</p><p># limit is set to 5 since we are using a 5V battery.</p><p># Plotting the values</p><p>Graph(data,stl='dark_background',label='Light Intensity')</p>

Step 4: Final Program to Check the Intensity of Light in a Room.

After coming to a conclusion from the data you got, upload the following program and make sure to change the limits as per your conclusion.

# Importing Arduino_Master module

from Arduino_Master import 



# collecting data
data=filter(ardata(8,squeeze=False,dynamic=True,msg="DATA",lines=50),expected_type='num',limit=[0,5])





#classifying data based on conclusion
info=[]
for i in range(len(data)):
    intensity=data[i]
    if intensity <= 1:
        info.append("Too dark")
    elif intensity > 1 and intensity<2:
        info.append('dark')
    elif intensity>=2:
        info.append('Light ON')




# Plotting the Graph.
compGraph(data,info,stl='dark_background',label1='Light Intensity',label2='State')

Step 5: Result:

The program would take a minute or two to run since you are reading 50 instantaneous values from Arduino.

If you want to speed the process up try changing the lines parameter of the ardata function. But remember that the lesser the observations, the lesser would be the quality of data.

Note: If the complete graph in the above pic is not visible, refer the graph above the Introduction section.

Share

    Recommendations

    • Growing Beyond Earth Maker Contest

      Growing Beyond Earth Maker Contest
    • Pets Challenge

      Pets Challenge
    • Beauty Tips Contest

      Beauty Tips Contest

    2 Discussions