Loading [MathJax]/extensions/MathZoom.js

Thursday, 20 August 2015

Using Arduino to measure friction coefficient

As a sideproject I decided to design a simple experiment and use Arduino to measure the friction coefficient of an object sliding on a given material.

Ideally we would like our first object to slide (not roll) on a sheet of a given material as below:

im

If we know the angle (we can easily set it) and the mass of the wooden block, then the only unknown variable is the friction coefficient and we can easily estimate it by measuring how long it took for the block to go over a certain distance x.


By using two cheap laser pen pointers and two photoresistors we can build a simple timing device. Here is the basic sketch

im2

Now we only need to set up the circuit in the Arduino board, by the way Fritzing is great for Arduino visual schemes

im3

The basic idea is that when the light beam from the laser pointer to the photoresistor gets interrupted, Arduino knows that the wooden block has passed in front of our simple timing device. The first photoresistor starts to record time, the second one stops the counting.

Now we only need to write the Arduino code and Python code to read the measurements through the serial port of our computer.

The Arduino sketch goes as follows:

//Initialize global variables
int ledRed = 8; //Red LED pin
int ledGreen = 9; //Green LED pin
int sensorCurrent = 10; //Sensor pin
int sensorRed = 0; //Red laser light sensor
int sensorGreen = 0; //Green laser light sensor
int measurements = 0; //Number of measurements done
int numberOfMeasurements = 1; //Measurements to be done (must be the same as nSamples in the Python script)
float t1; //Initial time (time at the start of the measurement)
float t2; //End time (time at the end of the measurement)
float total; //t2-t1 Time interval (positive)
boolean green = false; //Check variable. If true the red sensor has been triggered, therefore the green can be triggered
boolean currentOn = false; //True: the current to the light sensors is on
//Setup function
void setup()
{
//Initialize serial port at 9600 baud
Serial.begin(9600);
//Initialize LED pins
pinMode(ledRed,OUTPUT);
pinMode(ledGreen,OUTPUT);
pinMode(sensorCurrent,OUTPUT);
}
//Main loop
void loop()
{
//Turn sensor current on
if(!currentOn)
{
digitalWrite(sensorCurrent,HIGH);
currentOn = true;
}
//Read sensor values
sensorRed = analogRead(A0);
sensorGreen = analogRead(A1);
//Debugging//
//Serial.println(sensorRed);
//If red sensor is triggered start counting time
//and light the red LED (pin 8)
if(sensorRed > 1000)
{
t1 = micros();
green = true;
digitalWrite(ledRed,HIGH);
}
//If green sensor is triggered stop counting and
//print out the time. Light the green LED (pin 9)
if((sensorGreen > 900) && (green))
{
t2 = micros();
total = (t2 - t1);
Serial.println(total);
digitalWrite(ledGreen,HIGH);
//Wait 1 sec
delay(1000);
//Turn LEDs off and increment the number of measurements done
digitalWrite(ledRed,LOW);
digitalWrite(ledGreen,LOW);
green = false;
measurements = measurements + 1;
//If the number of measurements done is equal to the number of the required, the current
//to the light sensors is cut off.
if(measurements >= numberOfMeasurements) //Only one measurement is done in this case,
{ //then the current to the sensors is cut off
digitalWrite(sensorCurrent,LOW);
}
}
}

The Arduino scripts is engineered so that once the number of measurements to be done has been set the current sensor is turned off. Note that the number of measurement in the sketch must the same as the variable nSamples in the following Python script.

The Python script is based on the Arduino class I made previously. You can download it here, while the article where I talk about connecting with Arduino is here. This script uses an Arduino object through a measuring function that collects nSamples measurements. It is very important that the number of measurements to be made is the same both on the Python script and Arduino sketch. The script loops until it collects the measurements and then if save is equal to True, it saves the data into a .txt file. optionally you could improve this using Pandas and the df.to_csv() method. Since I wrote this before I learned about Pandas I will leave it as it is. However, better and more concise code can of course be written.

#------------------------------------------------------------------------------
# Measuring function. Makes nSamples measurements through an Arduino object
# Notes:
# The program loops until it collects the nSamples readings
#
# This function accepts an object of the Arduino class
def measure(nSamples,arduino,save=False):
try:
print('Waiting for measurements...\n')
data = np.array(arduino.readData(nSamples,array=True,Floaters=True))
# Convert microseconds readings into seconds (1 s = 10^6 micro s)
data = data/1000000
print(str(nSamples) + ' measurements done:',data,'\n')
except Exception as e:
print(e)
if save:
try:
path = 'C:\\measurements.txt'
file = open(path,'w')
for element in data:
file.write(str(element))
file.write('\n')
file.close()
print('Data saved successfully')
except Exception as e:
print(e)
#------------------------------------------------------------------------------
# Run the program
# Make 1 measurement (the same number of measurement must be set in the Arduino
# sketch for the program to work properly).
measurements = 1
uno = Arduino()
measure(measurements,arduino,save=True)
uno.closeConn()

Hope this was inspiring.

2 comments: