Вы находитесь на странице: 1из 12

Contents

General Information about Arduino ............................................................................................................. 2


Quick notes about important pins ............................................................................................................ 2
Connecting the Arduino to Computer: ..................................................................................................... 2
How to Change ADC sample rate .............................................................................................................. 4
Sensors used in the Bicycle Adapted Generator Project .......................................................................... 4
Temperature sensor.............................................................................................................................. 4
Voltage Sensor ...................................................................................................................................... 4
Current Sensor ...................................................................................................................................... 5
RPM sensor ........................................................................................................................................... 5
Sample code from Bicycle Adapted Generator Project with detailed comments .................................... 6
PLX-DAQ .................................................................................................................................................... 9
Steps to using PLX-DAQ....................................................................................................................... 11

Written by Bicycle Adapted Generator Team with assistance from Arduino Forum

General Information about Arduino


Operating voltage
Digital I/O pins
Analog input pins
DC current per I/O pin
DC current for 3.3V pin
Flash Memory
ADC sample rate
Baud rate

5V
54 (15 provide PWM output)
16
40mA
50mA
256Kb
9600Hz but can be changed (see below for details)
Up to 1Mbps

Note: 9600Hz ADC sample rate suggest each analog read will take about 100us
Changing the ADC sample rate up to 77KHz does not significantly affect precision of the value.

Arduino Mega can be powered via the USB connection or with an external power supply. (7-12V battery
recommended) If external power supply has to be used, connect to the power jack on the Arduino board
or through VIN pin.

Quick notes about important pins


5V pin Arduino outputs a regulated 5V on this pin. It can be used as a voltage source for sensors. Do
not make a mistake of supplying 5V voltage to this pin. It will damage the board permanently
3V3 pin 3.3V pin. Same functionality as 5V pin
GND Ground pin of the Arduino. This can be used by the sensors for gnd connection
IOREF reference voltage from Arduino. Supplies certain amount of voltage set by the microcontroller
(which is controllable by C code)
For more information, please visit the Arduino website.

Connecting the Arduino to Computer:


First step is to configure your computer to recognize the Arduino. If you connect the Arduino to the
computer and skip this step, the computer wont recognize the Arduino and you wont be able to upload
the code to the microcontroller
1. Install the Arduino environment from the Arduino website. (download the file and extract on
the C drive.)
2. Connect the board to the computer. PWR LED should light up
3. Wait for the windows to try and install driver for the Arduino. It will fail
Written by Bicycle Adapted Generator Team with assistance from Arduino Forum

4. Go to device manager on your Windows (click on start icon and right click computer. Then click
properties. You will see general information about your computer and see few link tabs on the
left. Click on the device manager)
5. Look under ports (COM & LPT). You will see a port named Arduino Mega (COMxx)
6. Right clock the port and choose update driver software
7. Choose the browse my computer for Driver software option and navigate to Drivers folder
located inside your Arduino environment (from step 1)
Above steps will allow the Arduino to be properly recognized by your computer. Next, you need to let
the Arduino environment know that you are using Arduino Mega (see below image)

Written by Bicycle Adapted Generator Team with assistance from Arduino Forum

How to Change ADC sample rate


Normally, the sample rate of ADC is preset to 9600Hz. The calculation to get the pre-set value is as
follows:
ADC clock speed = 16MHz
Pre-scale factor = 128
16MHz / 128 = 125KHz (clock speed)
Conversion time (analog to digital) = 13 ADC clocks
Pre-set sample rate = 125KHz / 13 = 9600Hz
By changing the pre-scale factor, you can increase the ADC clock speed and sample rate up to 1MHz and
77KHz separately. It is advised not to go over the 1MHz limit because of precision problems. Below is a
code to change pre-scale factor (tested by one of the people on forum on ATMEGA chip)
// defines for setting and clearing register bits
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
// set prescale to 16
sbi(ADCSRA,ADPS2);
cbi(ADCSRA,ADPS1);
cbi(ADCSRA,ADPS0);

Sensors used in the Bicycle Adapted Generator Project


-

Temperature sensor (TMP36)


Voltage sensor (Phidgets 1135)
Current sensor (Phidgets 1122)
RPM sensor (IR LED and Phototransistor from Solarbotics)

Temperature sensor
The sensor output varies from 0-5V
The sensor output follows the formula shown below:
Temperature in Celsius = [(Vout in mV) 500] / 10;
For example if the sensor output is 1V, that means (1000 500) / 10 = 50 Celsius.

Voltage Sensor
Unlike other sensors used for this project, the specification sheet did not reveal the relationship
between sensor output voltage and its actual readings. The formula had to be calculated through
measurements and resources in the lab. (Steps shown below)

Written by Bicycle Adapted Generator Team with assistance from Arduino Forum

1. Arduino readings range from 0 to 1024 (which corresponds to 0 to 5V). Voltage sensor reading
varies from -30 to 30V. When zero voltage was detected, Arduino read 512. The output of the
voltage sensor range from 0.5 to 4.5V. Small table shown below shows the relations ship
between these values
Arduino readings
102
512
922
Voltage sensor
-30
0
30
readings
Voltage sensor output 0.5
2.5
4.5
2. By looking at above table, the relationship between voltage sensor reading and Arduino
readings can be seen. Calculation with small explanation is given below
Arduino reading of 922 -> refers to 30V on voltage sensor
Arduino reading of 512 -> refers to 0 V on voltage sensor
Assuming linear relationship (why shouldnt it be)
922 512 = 410 units
30 V / 410 = 0.073 volts per unit
Formula for voltage
(Arduino reading 512) * 0.073 = Actual voltage measured by sensor
The formula above may change slightly for each sensor

Current Sensor
The current sensor formula wasnt provided and relationship between its output voltages to actual
sensor readings had to be figured out again using method in the voltage sensor section
Formula
Actual current measured by sensor = (Arduino reading - 512) * 0.074;

RPM sensor
A circuit consisting of IR emitting diode and phototransistor is used to calculate the rpm (shown below).
The idea was to detect the falling edge of the output. Normally, the output is kept at 0V until
interruption occurs between IR diode and phototransistor. The moment interruption is detected, the
output is pulled up to 5V until interruption disappears.

Written by Bicycle Adapted Generator Team with assistance from Arduino Forum

Sample code from Bicycle Adapted Generator Project with detailed comments
Below is a sample code from Bicycle Adapted Generator (comments are shown in green or red)
Pay Special attention to the Red comment text about early initialization of the analog pin and how to
communicate properly with the PLX-DAQ program. (Further information about the PLX-DAQ described in
the next section)
int tspin = 0; //The variables declared here refer to pin number that Arduino will read from
int tspin_1 = 1; // for example tspin = 0 will read in information from analog pin 0 (shown later)
int tspin_2 = 2;
int current = 3;
int volt_read = 4;
int current_5 = 5;
int current_6 = 6;
int voltvalue = 0; // The variables decloased below are used for calculations. You do not have to declare
them here but I prefer to keep them in one place
float volt = 0;
int ampvalue = 0;
float amps = 0;
int ampvalue_5 = 0;
float amps_5 = 0;
int ampvalue_6 = 0;
float amps_6 = 0;
int reading_1 = 0;
int reading_2 = 0;
float aref_volt = 5; //reference voltage that Arduino will read the values in
float d_ratio = 1023.00; // resolution of the Arduino (12 bit resolution => 2^12)
float tot_data;
int ledPin = 13; // digital pin 13 will supply voltage to IR LED for rpm readings
volatile byte rpmcount;
unsigned int rpm;
unsigned long timeold;
void rpm_fun()
{
//function for generating rpm values. rpmcount refers to number of interruptions possible
rpmcount++;
}
void setup() //initialization of the arduino
Written by Bicycle Adapted Generator Team with assistance from Arduino Forum

{
Serial.begin(9600);
//analogReference(); //This is a code that can be used to increase the accuracy reading of arduino by
changing analog voltage reference (instead of 5/2^12 you can change this to 3.3 for 3.3/2^12)
attachInterrupt(0, rpm_fun, FALLING); // Function for detecting interrupt. Number 0 refers to digital
pin 2 of the arduino. Everytime falling edge is detected from digital pin 2, the function rpm_fun will run
//Turn on IR LED
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
rpmcount = 0;
rpm = 0;
timeold = 0;
Serial.println("CLEARDATA"); //Serial.println are modified to allow PLX-DAQ program to understand the
data coming off the Arduino
Serial.println("LABEL, ,rpm, temp, amps_1, volts_1, amps_2, amps_3"); //Column labels for each data
}
void loop()
{
detachInterrupt(0); // stop reading the interruption from digital pin 2
rpm = 31*1000/(millis() - timeold)*rpmcount; //formula to calculate rpm
timeold = millis();
rpmcount = 0;
attachInterrupt(0,rpm_fun,FALLING); //start reading the interruption
int reading = analogRead(tspin);
float voltage = reading * aref_volt;
voltage /= d_ratio;
float temperature = (voltage - 0.5)*100; //formula to calculate temperature
Serial.print("DATA,TIME,"); //initialize the recording of data in PLX-DAQ. Will record the data in each
column of excel separated by comma. Also will record time for each data measurement
Serial.print(rpm); Serial.print(","); //record rpm and move on to next column (comma)
Serial.print(temperature); Serial.print(",");
reading_1 = analogRead(tspin_1); // Initialize the analog pin to charge the sample and hold capacitor
early
delay(100);//Delay to allow charging of the capacitor
reading_1 = analogRead(tspin_1);
float voltage_1 = reading_1 * aref_volt;
Written by Bicycle Adapted Generator Team with assistance from Arduino Forum

voltage_1 /= d_ratio;
float temperature_1 = (voltage_1 - 0.5) * 100;
reading_2 = analogRead(tspin_2);
delay(100);
reading_2 = analogRead(tspin_2);
float voltage_2 = reading_2 * aref_volt;
voltage_2 /= d_ratio;
float temperature_2 = (voltage_2 - 0.5) * 100;
ampvalue = analogRead(current);
delay(100);
ampvalue = analogRead(current);
amps = (ampvalue - 509) * 0.074; //formula for calculating current
Serial.print(amps); Serial.print(",");
voltvalue = analogRead(volt_read);
delay(100);
voltvalue = analogRead(volt_read);
volt= (voltvalue - 570) * 0.0685; //formula for calculating voltage
Serial.print(volt); Serial.print(",");
ampvalue_5 = analogRead(current_5);
delay(100);
ampvalue_5 = analogRead(current_5);
amps_5 = (ampvalue_5 - 511) * 0.074;
Serial.print(amps_5); Serial.print(",");
ampvalue_6 = analogRead(current_6);
delay(100);
ampvalue_6 = analogRead(current_6);
amps_6 = (ampvalue_6 - 511) * 0.074;
for(int k=0;k<60;k++) {//measure average current instead of instantaneous current
ampvalue_6 = analogRead(current_6);
amps_6 = (ampvalue_6 - 511) * 0.074;
tot_data += amps_6;
}
Serial.println(tot_data/60);
tot_data = 0;
delay(400); //delay of 400ms to allow total of 1sec for each measurements
}
Written by Bicycle Adapted Generator Team with assistance from Arduino Forum

PLX-DAQ
The program PLX-DAQ (it is actually excel macro) is designed to receive data from the propeller
(microcontroller from company Parallax) and export the data into an excel spreadsheet.
Since it uses Serial transmission and USB cable to transfer data, I used this program and modified the
Arduino code slightly to let PLX-DAQ understand the data it receives from Arduino.
An example of the excel spread sheet using PLX-DAQ is shown below.(using some the sample code
above)
5:13:12
5:13:13
5:13:14
5:13:15
5:13:16
5:13:17
5:13:18
5:13:19
5:13:20
5:13:21
5:13:22
5:13:23
5:13:24
5:13:25
5:13:26
5:13:27
5:13:28
5:13:29
5:13:30
5:13:31
5:13:32
5:13:33
5:13:34
5:13:35
5:13:36
5:13:37
5:13:38
5:13:39
5:13:40
5:13:41
5:13:42
5:13:43
5:13:44
5:13:45
5:13:47
5:13:48
5:13:49

rpm
65535.00
90.00
150.00
120.00
150.00
120.00
120.00
120.00
150.00
150.00
150.00
150.00
150.00
180.00
150.00
150.00
180.00
150.00
180.00
150.00
150.00
150.00
120.00
150.00
150.00
150.00
180.00
150.00
180.00
150.00
180.00
150.00
150.00
150.00
150.00
180.00
120.00

amps_1
2.52
3.11
2.81
2.81
3.18
2.74
2.89
3.03
2.89
3.03
2.96
3.26
3.18
3.03
3.03
2.96
2.89
2.81
3.11
2.89
3.11
2.81
3.11
3.26
2.96
2.89
3.18
3.18
3.18
3.4
3.33
3.11
3.18
3.03
3.33
3.18
3.18

volts_1
8.15
8.56
8.36
8.43
8.56
9.25
8.63
8.43
8.77
9.11
9.32
8.84
8.77
8.84
8.84
9.18
9.11
9.32
9.11
9.45
9.32
8.77
9.18
9.38
9.45
9.73
8.97
9.38
9.86
9.73
10
9.8
10
9.86
9.66
9.66
9.93

amps_2
0.07
0.07
0.07
0.07
0.07
0.07
0.07
0.07
0
0
0.07
0
0
0.07
0
0
0.07
0
0
0
0.07
0
0
0
0.07
0.07
0
0.07
0
0
0
0.07
0.07
0
0
0
0

amps_3
9.4
4.44
8.44
9.79
-0.75
3.91
9.8
8.55
1.09
8.46
2.63
6.76
8.14
9.58
7.31
8.99
9.96
6.96
6.19
7.06
6.56
11.7
5.08
4.73
5.92
11.24
7.22
7.3
2.96
4.98
5.38
4.66
4.52
3
-1.62
9.81
2.55

Written by Bicycle Adapted Generator Team with assistance from Arduino Forum

5:13:50
5:13:51
5:13:52
5:13:53
5:13:54
5:13:55
5:13:56
5:13:57
5:13:58
5:13:59
5:14:00
5:14:01
5:14:02
5:14:03
5:14:04
5:14:05
5:14:06
5:14:07
5:14:08
5:14:09
5:14:10
5:14:11
5:14:12
5:14:13
5:14:14
5:14:15
5:14:16
5:14:17
5:14:18
5:14:19
5:14:20
5:14:21
5:14:22
5:14:23
5:14:24
5:14:25
5:14:26
5:14:27
5:14:28
5:14:29
5:14:30
5:14:31
5:14:32
5:14:33
5:14:34
5:14:35
5:14:36
5:14:37

150.00
180.00
150.00
150.00
180.00
180.00
150.00
150.00
150.00
120.00
150.00
120.00
120.00
150.00
150.00
120.00
150.00
150.00
120.00
120.00
150.00
150.00
150.00
150.00
150.00
120.00
150.00
150.00
120.00
180.00
120.00
120.00
150.00
120.00
120.00
90.00
120.00
90.00
120.00
60.00
120.00
90.00
120.00
120.00
60.00
90.00
120.00
90.00

3.48
3.33
3.63
3.7
3.4
3.4
3.26
3.33
3.4
4.07
3.85
3.18
3.7
3.85
3.7
3.85
3.7
3.7
4
3.33
3.85
3.77
3.92
3.7
4
4.14
4.22
4.14
3.48
3.92
3.55
4.29
3.85
3.77
2.96
4
3.03
3.26
2.74
3.4
2.59
4.14
2.52
2.22
2.52
3.77
2.81
2.44

10.55
11.23
10.75
10.82
10.96
11.85
11.3
12.4
11.03
11.37
12.95
11.44
11.85
10.82
13.43
13.01
12.47
11.03
12.74
13.01
12.6
12.47
11.99
12.26
12.26
11.51
12.67
13.08
13.49
13.29
11.44
12.74
13.01
9.8
13.36
11.85
10.96
13.36
7.47
8.7
12.33
12.74
8.97
7.74
9.86
11.03
7.33
9.93

0.07
0
0.07
0
0
0
0.07
0.07
0
0
0
0
0.07
0.07
0.07
0.07
0
0.07
0
0.07
0.07
0.07
0
0
0
0.07
0
0
0
0
0.07
0
0
0
0.15
0
0.07
0
0
0.07
0
0
0
0.07
0
0
0.07
0

3.57
1.16
5.37
1.1
2.49
4.72
2.66
-1.18
8.98
4.99
0.46
8.99
7.93
4.78
0.36
-1.21
2.03
5.56
0.42
2.94
0.48
1.03
3.13
2.6
2.7
1.33
-1.07
3.72
1.15
0.18
6.72
-0.73
1.57
-0.21
2.53
-0.8
5.06
-1.58
-2.33
0.38
3.7
-2.37
4.2
0.05
0.24
-1.2
0.16
0.61

Written by Bicycle Adapted Generator Team with assistance from Arduino Forum

5:14:38
5:14:39
5:14:40
5:14:41
5:14:42
5:14:43
5:14:44
5:14:45
5:14:46
5:14:47
5:14:48
5:14:49
5:14:50
5:14:51
5:14:52
5:14:53
5:14:54
5:14:55
5:14:56
5:14:57
5:14:58
5:14:59
5:15:00
5:15:01
5:15:02
5:15:03
5:15:04
5:15:06
5:15:07
5:15:08
5:15:09
5:15:10
5:15:11
5:15:12
5:15:13
5:15:14
5:15:15
5:15:16
5:15:17

90.00
120.00
90.00
120.00
120.00
60.00
120.00
90.00
90.00
90.00
120.00
90.00
90.00
90.00
120.00
120.00
90.00
90.00
90.00
120.00
90.00
90.00
120.00
90.00
120.00
120.00
90.00
120.00
90.00
120.00
120.00
90.00
120.00
90.00
120.00
120.00
120.00
120.00
30.00

3.03
3.4
3.26
3.92
2.07
2.96
4
2.29
3.48
2.66
4
2.96
2.29
3.7
3.18
2
3.92
3.92
3.26
2.59
4
2.96
3.4
3.77
2.96
4
2.81
3.77
3.26
2.81
3.85
3.03
3.77
3.55
3.7
3.85
2.89
3.48
0

12.6
9.8
13.15
10.21
6.78
11.3
12.12
6.37
8.29
11.71
12.4
7.67
8.36
13.15
8.22
8.84
12.54
10.55
7.88
11.44
12.26
7.67
13.01
10.14
11.92
12.47
8.36
13.56
8.08
10.75
11.23
7.81
13.22
9.8
12.95
9.52
12.54
8.01
-11.17

0
0
0.07
0.07
0
0
0
0.07
0
0.07
0
0
0
0.07
0
0
0
0.07
-0.07
0
0
0.07
0
0
0
0
0.07
0
0.07
0
0
0
0
0.07
0
0.07
0.07
0
0

-0.04
0.47
-0.49
-0.13
-1.76
0.73
-0.7
-2.51
0.05
2.12
-2.11
-0.15
1.29
-0.35
0.68
3.1
-1.94
-2.13
0.15
0.67
-0.61
0.65
-0.89
-1.12
3.69
-0.4
0.58
-1.1
-0.25
5.45
-0.68
2.76
-1.35
0.51
-2.3
0.67
2.48
-0.06
-0.03

Steps to using PLX-DAQ


1. Install the PLX-DAQ program (from the internet or from the attached files)
2. Run the PLX-DAQ from the start menu and allow the macro to run in the excel spread sheet
3. If all is well, you will see a screen similar to image below

Written by Bicycle Adapted Generator Team with assistance from Arduino Forum

4. Simply click connect (pay attention to baud rate shown on the settings above and from the
Arduino which is declared using Serial.begin(9600) code in the setup section. Make sure these
two are equal)
5. If the Arduino code is set up correctly, you should see the data being recorded every second.
Please refer to red comment text in the sample code for more information about
communication between PLX-DAQ and Arduino.

Written by Bicycle Adapted Generator Team with assistance from Arduino Forum

Вам также может понравиться