Go Down

Topic: Plotting Graph's in MatLab with Arduino Data (Read 9128 times) previous topic - next topic



I've just discovered how to import data from the Arduino Uno with dht11 temp sensor into a vector and plot a graph using the code below:

Code: [Select]
arduino = serial('COM4');


x = 1:1:60;

for i=1:length(x)
    y(i) = fscanf(arduino, '%d');


%  Plot data from arduino

I have labeled the X axis Time (Seconds), but is this true?  Does anyone know how exactly the data from the Arduino is being extracted?  And if its not in seconds, how is this possible?

Also could anyone point me in the right direction on how to actually see the temp sensor data live on the screen.  I have seen it done on You Tube, but nobody actually shows you what to do.

Thanks in advance!


Does anyone know how exactly the data from the Arduino is being extracted?

Not until you post the Arduino code you use.

I worked on these DHT libs - http://playground.arduino.cc/Main/DHT11Lib - and  - http://playground.arduino.cc/Main/DHTLib -
so I know a bit about the timing of the sensor. The readout itself takes far less than a second
The playground pages have some same code that can easily be converted to your needs (strip off what you do not need).

However the sensor need a 2 second interval before it can read again (reliably).
so a one second interval might affect accuracy & precision.

Furthermore the communication is over serial which is asynchronous which means always there is a drift in timing possible.

Hope this helps,
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)


Sorry, here is my Arduino code:

Code: [Select]
#include <LiquidCrystal.h>

#define commPin 7  // Connect the Communications pin on the module to pin 7 on the Arduino

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup()

  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Temp & Humidity");   

void loop()

uint8_t data[3];                 // We will use this to store and array of the data
uint8_t bitCount = 7;            // As data is sent with the most significant bit first we start at the end
uint8_t byteCount = 0;           // Keep count of the Bytes

        unsigned long t = 0;             // Used to time the data pulse

        char str[512];                   // We will use this to construct a string to send to the Serial port with the readings

for (int i=0; i< 4; i++) data[i] = 0;      // Empty the data array

// First we need to tell the module we want it to take a reading, This is done by pulling the
        // communications pin low for 18mS, then high for 40uS.
pinMode(commPin, OUTPUT);
digitalWrite(commPin, LOW);
digitalWrite(commPin, HIGH);
pinMode(commPin, INPUT);

        // When the module is ready to send its data it will pull the communications pin low then high
while(digitalRead(commPin) == LOW)
while(digitalRead(commPin) == HIGH)

        // The module will now start sending 40 bits of data, the first Byte is the Humidity, the second is just null,
        // the third is the Temperature, the fourth is null and the 5th is a check sum
        // For simplicity we will just look at the first 3 Bytes.

for (int i=0; i<24; i++)
while(digitalRead(commPin) == LOW)    // Wait for the High pulse to start

                t = micros();                   // Use variable t to store the time when the high pulse starts.

while(digitalRead(commPin) == HIGH)   // Wait for the High pulse to end

                // if we now do micros() -t we know how long the High pulse lasted, around 28uS represents a '0' and
                // around 70uS represents a '1'. To allow some error in timing we can simply say that if the High Pulse
                // is longer than 49uS then it is a '1' and if it is shorter then it is a '0'
if ((micros() - t) > 49) data[byteCount] |= (1 << bitCount);
if (bitCount == 0)               // If the bitCount has reached 0 then we are on to the net Byte
bitCount = 7;            // Reset the bitCount to the MSB
byteCount++;             // On to the next Byte
else bitCount--;                 // bitCount not yet 0 so subtract 1

int humidity    = data[0]/3.75;               
int temperature = data[2];

        sprintf(str, "%d \t %d", humidity, temperature);   // Construct a string out of all three readings
        Serial.println(str);                               // Print it to the serial link
        delay(2000);        // This is not a quick sensor so give it time to rest :)
        // set the cursor to column 0, line 1
        // (note: line 1 is the second row, since counting begins with 0):
        lcd.setCursor(0, 1);
        // print the number of seconds since reset:
        lcd.print("oC ");
        lcd.print("% ");

So when I execute the fscanf function, each array variable will be logged every 2 seconds then?

I have attached my graph too if it helps:


Go Up