general information about arduino_1

Upload: lavinia-angelescu

Post on 08-Aug-2018

212 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/22/2019 General Information About Arduino_1

    1/12

    Written by Bicycle Adapted Generator Team with assistance from Arduino Forum

    ContentsGeneral 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

  • 8/22/2019 General Information About Arduino_1

    2/12

    Written by Bicycle Adapted Generator Team with assistance from Arduino Forum

    General Information about Arduino

    Operating voltage 5V

    Digital I/O pins 54 (15 provide PWM output)

    Analog input pins 16DC current per I/O pin 40mA

    DC current for 3.3V pin 50mA

    Flash Memory 256Kb

    ADC sample rate 9600Hz but can be changed (see below for details)

    Baud rate 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 batteryrecommended) 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 pinArduino 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 pin3.3V pin. Same functionality as 5V pin

    GNDGround pin of the Arduino. This can be used by the sensors for gnd connection

    IOREFreference 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

  • 8/22/2019 General Information About Arduino_1

    3/12

    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 software7. 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)

  • 8/22/2019 General Information About Arduino_1

    4/12

    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 16sbi(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 (1000500) / 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)

  • 8/22/2019 General Information About Arduino_1

    5/12

    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 922Voltage sensor

    readings

    -30 0 30

    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)

    922512 = 410 units30 V / 410 = 0.073 volts per unit

    Formula for voltage

    (Arduino reading512) * 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.

  • 8/22/2019 General Information About Arduino_1

    6/12

    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

  • 8/22/2019 General Information About Arduino_1

    7/12

    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 digitalpin 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;

  • 8/22/2019 General Information About Arduino_1

    8/12

    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

  • 8/22/2019 General Information About Arduino_1

    9/12

    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)

    rpm amps_1 volts_1 amps_2 amps_3

    5:13:12 65535.00 2.52 8.15 0.07 9.4

    5:13:13 90.00 3.11 8.56 0.07 4.44

    5:13:14 150.00 2.81 8.36 0.07 8.44

    5:13:15 120.00 2.81 8.43 0.07 9.79

    5:13:16 150.00 3.18 8.56 0.07 -0.75

    5:13:17 120.00 2.74 9.25 0.07 3.915:13:18 120.00 2.89 8.63 0.07 9.8

    5:13:19 120.00 3.03 8.43 0.07 8.55

    5:13:20 150.00 2.89 8.77 0 1.09

    5:13:21 150.00 3.03 9.11 0 8.46

    5:13:22 150.00 2.96 9.32 0.07 2.63

    5:13:23 150.00 3.26 8.84 0 6.76

    5:13:24 150.00 3.18 8.77 0 8.14

    5:13:25 180.00 3.03 8.84 0.07 9.58

    5:13:26 150.00 3.03 8.84 0 7.31

    5:13:27 150.00 2.96 9.18 0 8.99

    5:13:28 180.00 2.89 9.11 0.07 9.96

    5:13:29 150.00 2.81 9.32 0 6.96

    5:13:30 180.00 3.11 9.11 0 6.19

    5:13:31 150.00 2.89 9.45 0 7.06

    5:13:32 150.00 3.11 9.32 0.07 6.56

    5:13:33 150.00 2.81 8.77 0 11.7

    5:13:34 120.00 3.11 9.18 0 5.08

    5:13:35 150.00 3.26 9.38 0 4.73

    5:13:36 150.00 2.96 9.45 0.07 5.92

    5:13:37 150.00 2.89 9.73 0.07 11.24

    5:13:38 180.00 3.18 8.97 0 7.22

    5:13:39 150.00 3.18 9.38 0.07 7.3

    5:13:40 180.00 3.18 9.86 0 2.96

    5:13:41 150.00 3.4 9.73 0 4.98

    5:13:42 180.00 3.33 10 0 5.38

    5:13:43 150.00 3.11 9.8 0.07 4.66

    5:13:44 150.00 3.18 10 0.07 4.52

    5:13:45 150.00 3.03 9.86 0 3

    5:13:47 150.00 3.33 9.66 0 -1.62

    5:13:48 180.00 3.18 9.66 0 9.81

    5:13:49 120.00 3.18 9.93 0 2.55

  • 8/22/2019 General Information About Arduino_1

    10/12

    Written by Bicycle Adapted Generator Team with assistance from Arduino Forum

    5:13:50 150.00 3.48 10.55 0.07 3.57

    5:13:51 180.00 3.33 11.23 0 1.16

    5:13:52 150.00 3.63 10.75 0.07 5.37

    5:13:53 150.00 3.7 10.82 0 1.1

    5:13:54 180.00 3.4 10.96 0 2.49

    5:13:55 180.00 3.4 11.85 0 4.72

    5:13:56 150.00 3.26 11.3 0.07 2.66

    5:13:57 150.00 3.33 12.4 0.07 -1.18

    5:13:58 150.00 3.4 11.03 0 8.98

    5:13:59 120.00 4.07 11.37 0 4.99

    5:14:00 150.00 3.85 12.95 0 0.46

    5:14:01 120.00 3.18 11.44 0 8.99

    5:14:02 120.00 3.7 11.85 0.07 7.93

    5:14:03 150.00 3.85 10.82 0.07 4.78

    5:14:04 150.00 3.7 13.43 0.07 0.36

    5:14:05 120.00 3.85 13.01 0.07 -1.21

    5:14:06 150.00 3.7 12.47 0 2.03

    5:14:07 150.00 3.7 11.03 0.07 5.56

    5:14:08 120.00 4 12.74 0 0.42

    5:14:09 120.00 3.33 13.01 0.07 2.94

    5:14:10 150.00 3.85 12.6 0.07 0.48

    5:14:11 150.00 3.77 12.47 0.07 1.03

    5:14:12 150.00 3.92 11.99 0 3.13

    5:14:13 150.00 3.7 12.26 0 2.6

    5:14:14 150.00 4 12.26 0 2.7

    5:14:15 120.00 4.14 11.51 0.07 1.33

    5:14:16 150.00 4.22 12.67 0 -1.07

    5:14:17 150.00 4.14 13.08 0 3.72

    5:14:18 120.00 3.48 13.49 0 1.155:14:19 180.00 3.92 13.29 0 0.18

    5:14:20 120.00 3.55 11.44 0.07 6.72

    5:14:21 120.00 4.29 12.74 0 -0.73

    5:14:22 150.00 3.85 13.01 0 1.57

    5:14:23 120.00 3.77 9.8 0 -0.21

    5:14:24 120.00 2.96 13.36 0.15 2.53

    5:14:25 90.00 4 11.85 0 -0.8

    5:14:26 120.00 3.03 10.96 0.07 5.06

    5:14:27 90.00 3.26 13.36 0 -1.58

    5:14:28 120.00 2.74 7.47 0 -2.33

    5:14:29 60.00 3.4 8.7 0.07 0.38

    5:14:30 120.00 2.59 12.33 0 3.7

    5:14:31 90.00 4.14 12.74 0 -2.37

    5:14:32 120.00 2.52 8.97 0 4.2

    5:14:33 120.00 2.22 7.74 0.07 0.05

    5:14:34 60.00 2.52 9.86 0 0.24

    5:14:35 90.00 3.77 11.03 0 -1.2

    5:14:36 120.00 2.81 7.33 0.07 0.16

    5:14:37 90.00 2.44 9.93 0 0.61

  • 8/22/2019 General Information About Arduino_1

    11/12

    Written by Bicycle Adapted Generator Team with assistance from Arduino Forum

    5:14:38 90.00 3.03 12.6 0 -0.04

    5:14:39 120.00 3.4 9.8 0 0.47

    5:14:40 90.00 3.26 13.15 0.07 -0.49

    5:14:41 120.00 3.92 10.21 0.07 -0.13

    5:14:42 120.00 2.07 6.78 0 -1.76

    5:14:43 60.00 2.96 11.3 0 0.73

    5:14:44 120.00 4 12.12 0 -0.7

    5:14:45 90.00 2.29 6.37 0.07 -2.51

    5:14:46 90.00 3.48 8.29 0 0.05

    5:14:47 90.00 2.66 11.71 0.07 2.12

    5:14:48 120.00 4 12.4 0 -2.11

    5:14:49 90.00 2.96 7.67 0 -0.15

    5:14:50 90.00 2.29 8.36 0 1.29

    5:14:51 90.00 3.7 13.15 0.07 -0.35

    5:14:52 120.00 3.18 8.22 0 0.68

    5:14:53 120.00 2 8.84 0 3.1

    5:14:54 90.00 3.92 12.54 0 -1.94

    5:14:55 90.00 3.92 10.55 0.07 -2.13

    5:14:56 90.00 3.26 7.88 -0.07 0.15

    5:14:57 120.00 2.59 11.44 0 0.67

    5:14:58 90.00 4 12.26 0 -0.61

    5:14:59 90.00 2.96 7.67 0.07 0.65

    5:15:00 120.00 3.4 13.01 0 -0.89

    5:15:01 90.00 3.77 10.14 0 -1.12

    5:15:02 120.00 2.96 11.92 0 3.69

    5:15:03 120.00 4 12.47 0 -0.4

    5:15:04 90.00 2.81 8.36 0.07 0.58

    5:15:06 120.00 3.77 13.56 0 -1.1

    5:15:07 90.00 3.26 8.08 0.07 -0.255:15:08 120.00 2.81 10.75 0 5.45

    5:15:09 120.00 3.85 11.23 0 -0.68

    5:15:10 90.00 3.03 7.81 0 2.76

    5:15:11 120.00 3.77 13.22 0 -1.35

    5:15:12 90.00 3.55 9.8 0.07 0.51

    5:15:13 120.00 3.7 12.95 0 -2.3

    5:15:14 120.00 3.85 9.52 0.07 0.67

    5:15:15 120.00 2.89 12.54 0.07 2.48

    5:15:16 120.00 3.48 8.01 0 -0.06

    5:15:17 30.00 0 -11.17 0 -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

  • 8/22/2019 General Information About Arduino_1

    12/12

    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.