Go Down

Topic: Need Helps for communication SPI (Read 139 times) previous topic - next topic

Baptiste_39

Hello everyone,



I am a beginner and wonder how can I solve this problem.
It is in the context of a TP at the university:

"Write the program that reads the values ​​of the I2C module pmodHYGRO This program will send the temperature and humidity values ​​to the serial link"


Ok, so this is What I do.


Code: [Select]
#include <Arduino.h>
#include <SPI.h>
 
void setup() {
  // put your setup code here, to run once:
  //déclarez quel(s) connecteur(s) seront utilisés pour les lignes SS et configurez-les en sorties (OUTPUT).
//  pinMode(10, OUTPUT);
  // pour activer le bus SPI
  SPI.begin();
  SPI.setDataMode(SPI_MODE0);
  SPI.setClockDivider(SPI_CLOCK_DIV2);
  //indiquer dans le code dans quel sens seront envoyés les bits de données de chaque octet, le bit de poids fort (MSB) ou celui de poids faible (LSB) en premier
  SPI.setBitOrder(MSBFIRST);
}

void setValue(int value)
{
  digitalWrite(10, LOW);
  //SPI.transfer(0); // send command byte
  SPI.transfer(value); // send value (0~255)
  digitalWrite(10, HIGH);
}
 
void loop() {
// put your main code here, to run repeatedly:
for (int a = 0; a < 256; a++)
  {
    setValue(a);
    delay(200);
  }
  for (int a = 255; a >= 0; --a)
  {
    setValue(a);
    delay(200);
  }

}



Unfortunately, that's doesn't work..
Obviously I forgot to make a screenshot for the error and saw that I can work only in the workshop...

And I wonder if someone know what's "the memory footprint of the program" please ?



sherzaad

Hello everyone,



I am a beginner and wonder how can I solve this problem.
It is in the context of a TP at the university:

"Write the program that reads the values ​​of the I2C module pmodHYGRO This program will send the temperature and humidity values ​​to the serial link"


Ok, so this is What I do.


Unfortunately, that's doesn't work..

I2C and SPI are not the same protocol! look it up...

Nikosant03

As sherzaad mentioned you tried to use τηε SPI protocol instead of the I2C protocol. You can use the I2C protocol by including the wire.h library. Also take into account that for the I2C bus you need just two wires (SDA, SCL) while for the SPI bus you need 4 wires (MISO, MOSI, SCLK, CS).

I hope that helps.

Baptiste_39

Excuse me, but I made a mistake in my description and it's really communication with SPI.
Thanks you for you helps.

GolamMostafa

#4
Oct 17, 2019, 05:00 pm Last Edit: Oct 17, 2019, 05:07 pm by GolamMostafa
The datasheet [Fig-1) clearly says that the sensor has I2C interface. The image of the sensor (Fig-2) also shows the connector is labelled with I2C Bus signals (SDA, SCL). Why are you saying that the sensor has SPI interface?

Are you using BME280 (Fig-3) which is also a Humidity-temperature sensor, and it has both SPI/I2C interface.

Datasheet:

Figure-1:

Pmod Sesnor with Humidity-Temperature Elements (I2C Interface)

Figure-2:



BME280 Sensor with Humidity-Temperature-Pressure Elements (SPI/I2C Interface)

Figure-3:

Please, post the picture of the sensor you are using.

Baptiste_39

So this is what I've changed :

Code: [Select]
[code]#include <Arduino.h>
#include <SPI.h>

void setup() {
  // put your setup code here, to run once:
  //déclarez quel(s) connecteur(s) seront utilisés pour les lignes SS et configurez-les en sorties (OUTPUT).
//  pinMode(10, OUTPUT);
  // pour activer le bus SPI
  SPI.begin();
  SPI.setDataMode(SPI_MODE0);
  SPI.setClockDivider(SPI_CLOCK_DIV2);
  //indiquer dans le code dans quel sens seront envoyés les bits de données de chaque octet, le bit de poids fort (MSB) ou celui de poids faible (LSB) en premier
  SPI.setBitOrder(MSBFIRST);
}

void setValue(int value)
{
  digitalWrite(2, LOW);
  SPI.transfer(0); // send command byte
  SPI.transfer(value); // send value (0~255)
  digitalWrite(2, HIGH);
}

void loop() {
// put your main code here, to run repeatedly:
for (int a = 0; a < 256; a++)
  {
    setValue(a);

    Serial.print("Luminosité:");
    Serial.println(a);
    delay(200);
  }
  for (int a = 255; a >= 0; --a)
  {
    setValue(a);

    Serial.print("Luminosité:");
    Serial.println(a);
    delay(200);
  }
}
/*void setup() {

  // Initialise la communication avec le PC
  Serial.begin(9600);
}

// Fonction loop(), appelée continuellement en boucle tant que la carte Arduino est alimentée
void loop() {

  // Mesure la tension sur la broche A0
  int valeur = analogRead(A0);

  // Envoi la mesure au PC pour affichage et attends 250ms
  Serial.println(valeur);
  delay(250);
}*/
[/code]


But I still can not display the brightness value in lux in the console..

Nikosant03

You are still using the SPI protocol... You won't be able to take measurements from a sensor with I2C interface by using SPI protocol... It's like you are trying to get communicate with a Chinese guy in his language without speaking Chinese.

Baptiste_39

Ok thanks you for your helps !
That's does work now :)

GolamMostafa

Ok thanks you for your helps !
That's does work now :)
It is working in SPI or I2C Mode?

Go Up