# Sensor Calibration with Python’s Jupyter Notebook and the linear regression model

November 5, 2017 Oliver Prislan
Wikis > Prismatec Wiki > Sensor Calibration with Python's Jupyter Notebook and the linear regression model

This example shows how Jupyter Notebook can be used to calibrate the metering of current. To install Jupyter follow the instruction on https://www.anaconda.com/download/

Let’s assume we have a current sensor connected to an Ardunio performing the analog reading of the sensor and converting it into a digital signal that is sent to a Raspberry Pi via serial connection. An analog sensor provides a contigeous set of values. The Arduino analog-digital converter can only represent values of 0 to 1023 (10bit) in the range [0..5]V. Zero Volt repersents „0“ and 5V represents 1023.

Applying a factor can turn this voltage into a corresponding current. Determining the factor requires a comparisson of readings with a 2nd source. Here, this is a clamp ampere meter.

We created a text file with a list pairs of metered values (arduino / clamp ampere meter):``` Phase Arduino AmpereMeter 0 L1    0.40     0.41 1 L1    6.40     8.80 2 L2    0.22     0.30 3 L2    0.36     0.47 4 L2    0.65     0.94 5 L2    4.60     6.90 6 L2    6.10     8.30 7 L3    0.70     1.10 8 L3    1.30     1.90 9 L3    5.00     6.90 10 L3   7.60    10.20 ```

This data is used in the script below:
``` %matplotlib inline import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn import datasets, linear_model import re df = pd.read_csv("d:/data/ems/messung_enmon/Ard_vs_Amp.txt") x = df.Arduino y = df.AmpereMeter n = df.Arduino.count() x = x.values.reshape(n, 1) y = y.values.reshape(n, 1) plt.scatter(x,y, label='metered values', color='red') regr = linear_model.LinearRegression() regr.fit(x, y) m = re.sub('[\[\]]', '', str(regr.coef_)) b = re.sub('[\[\]]', '', str(regr.intercept_)) func = "f(x) = "+ m +" * x + " + b plt.plot(x, regr.predict(x), color='blue', linewidth=2, label=func) plt.legend() plt.show() ``` the X-Axis represents current sensorvalues, the Y-axis the metered values of the clamp-ampere-meter.
The slope of the regression line m = 1.36 is used as calibration factor. An offset of b = 0.05 can be subtracted.

The result will be better more values are metered with a finer resolution. Non-linear curves can be fitted with Python’s more advanced „Fitting“-algorithms.