Go Down

Topic: Using pin12 on the WiFi shield makes it not connect to network (Read 905 times) previous topic - next topic

chrisconradi

Hi,

I just bought an Arduino and a new world is opening! As my first project I thought I'd build a weather station.

I've hooked up multiple sensors to my arduino uno, all working fine. But I have an issue with a barometric pressure sensor I bought from sparkfun, the MPL115A1, https://www.sparkfun.com/products/9721.

I copied a piece of code I found here https://forum.sparkfun.com/viewtopic.php?f=8&t=23965#p137388.

The problem is, I am running a WiFi shield between the arduino uno and this temperature sensor. Whenever I connect SDO from the barometric sensor to pin12, as the code suggests, the WiFi shield is unable to connect to my network.

After some reverse engineering of the code and reading up on the SPI interface, it seems that pin 12 (and 10+11+13) are reserved for communication between the arduino uno and the WiFi shield. Does this mean I cannot use them for anything else? I can also see that pin 7 is used for handshake and pin 4 is used for the SD card. Can it be that the WiFi shield occupies half of my digital pins?

So now I am looking for a workaround. With the other sensors connected (light, temperature 2x, humidity, wind speed, wind direction, and rain) I am running out of pins. I did some exercises in the Arduino starter kit that talked about using a shift register to allow you to connect multiple inputs to one digital pin. Can I use a shift register for pin 12 as well, to keep the barometric sensor from interfering with the WiFi shield?

PaulS

Quote
Can I use a shift register for pin 12 as well, to keep the barometric sensor from interfering with the WiFi shield?

No.

That sensor is an SPI device, like the WiFi shield. You can use multiple SPI devices on the Arduino, simultaneously, as long as each one has a separate chip/slave select pin. You can connect the sensors chip select pin to any unused pin, and set that pin LOW to activate the sensor and HIGH to deactivate it.

Quote
After some reverse engineering of the code and reading up on the SPI interface, it seems that pin 12 (and 10+11+13) are reserved for communication between the arduino uno and the WiFi shield. Does this mean I cannot use them for anything else? I can also see that pin 7 is used for handshake and pin 4 is used for the SD card. Can it be that the WiFi shield occupies half of my digital pins?

6 of 20 is not quite half. The analog pins can be used as digital pins, too.

chrisconradi

Thanks for the reply! I was not aware you could use analog pins as digital. Great!

This particular barometric sensor uses, as far as I can understand, three SPI pins: SDI, SDO and SCK. By messing around with it, it seems that only SDO is affecting the WiFi shield. Is this the only one I need to remap?

In my code, pasted below, I never set pins 11, 12 or 13. I'm having a hard time understanding what is going on, but I think it somehow selects these three pins as they are the default SPI pins. Where and how would I make it use other pins?

Code: [Select]
/*
MPL115A1 sparkfun breakout baropressure meter
SDN       : pin 6
CSN       : pin 9
SDI/MOSI  : pin 11
SDO/MISO  : pin 12
SCK       : pin 13
*/

#include <WiFi.h>
#include <SPI.h>

//==============================//
//           Set Pins           //
//==============================//
const int    PinBaroShutDown      = 6;    // MPL115A1   Connected to SDN on barometer
const int    PinBaroChipSelectPin = 9;    // MPL115A1   Connected to CSN on barometer
const long   UpdateInterval       = 2000; // Update interval for post to server in milliseconds


//==============================//
//          Variables           //
//==============================//
float pressure; // Initialize sensor value: Pressure in hecto-pascal
char wifiSSID[] = "ssid";
char wifiPass[] = "password";

//==============================//
//    For barometric sensor     //
//==============================//
#define PRESH   0x80
#define PRESL   0x82
#define TEMPH   0x84
#define TEMPL   0x86
#define A0MSB   0x88
#define A0LSB   0x8A
#define B1MSB   0x8C
#define B1LSB   0x8E
#define B2MSB   0x90
#define B2LSB   0x92
#define C12MSB  0x94
#define C12LSB  0x96
#define CONVERT 0x24
float A0_;
float B1_;
float B2_;
float C12_;


//==============================//
//           Setup()            //
//==============================//
void setup()
{
  // Initialize serial
  Serial.begin(9600);
 
  // start the SPI library:
  SPI.begin();
 
  // Set pin modes
  pinMode(PinBaroShutDown, OUTPUT);
  digitalWrite(PinBaroShutDown, HIGH);
  pinMode(PinBaroChipSelectPin, OUTPUT);
  digitalWrite(PinBaroChipSelectPin, HIGH);
  delay (10);
   
  // read barometer registers that contain the chip-unique parameters to do the math
  unsigned int A0H = readRegister(A0MSB);
  unsigned int A0L = readRegister(A0LSB);
          A0_ = (A0H << 5) + (A0L >> 3) + (A0L & 0x07) / 8.0;
 
  unsigned int B1H = readRegister(B1MSB);
  unsigned int B1L = readRegister(B1LSB);
          B1_ = ( ( ( (B1H & 0x1F) * 0x100)+B1L) / 8192.0) - 3 ;
 
  unsigned int B2H = readRegister(B2MSB);
  unsigned int B2L = readRegister(B2LSB);
          B2_ = ( ( ( (B2H - 0x80) << 8) + B2L) / 16384.0 ) - 2 ;
 
  unsigned int C12H = readRegister(C12MSB);
  unsigned int C12L = readRegister(C12LSB);
          C12_ = ( ( ( C12H * 0x100 ) + C12L) / 16777216.0 )  ;
}

//==============================//
//           Loop()             //
//==============================//
void loop()
{
  if (status != WL_CONNECTED) //If not connected to WiFi
    connectWifi();
   
  else {
    pressure = getPressure(); // Pressure in hPa
 
    Serial.print("Pressure: ");
    Serial.println(pressure);

    delay(UpdateInterval);
  }
}


float getPressure()
{
  // Returns a pressure reading in hPa
 
  digitalWrite(PinBaroChipSelectPin, LOW);
  delay(3);
    SPI.transfer(0x24);
    SPI.transfer(0x00);
    digitalWrite(PinBaroChipSelectPin, HIGH);
    delay(3);
  digitalWrite(PinBaroChipSelectPin, LOW);
    SPI.transfer(PRESH);
    unsigned int presH = SPI.transfer(0x00);
        delay(3);
    SPI.transfer(PRESL);
    unsigned int presL = SPI.transfer(0x00);
        delay(3);
    SPI.transfer(TEMPH);
    unsigned int tempH = SPI.transfer(0x00);
        delay(3);
    SPI.transfer(TEMPL);
    unsigned int tempL = SPI.transfer(0x00);
        delay(3);
    SPI.transfer(0x00);
      delay(3);
  digitalWrite(PinBaroChipSelectPin, HIGH);

  unsigned long press = ((presH *256) + presL)/64;
  unsigned long temp  = ((tempH *256) + tempL)/64;

  float pressure = A0_+(B1_+C12_*temp)*press+B2_*temp;
  float preshPa = (pressure*  (65.0/1023.0)+50.0) * 10;

  return(preshPa);
}

unsigned int readRegister (byte thisRegister)
{
  //Read registers in barometer
 
  unsigned int result = 0;   // result to return
  digitalWrite(PinBaroChipSelectPin, LOW);
    delay(10);
    SPI.transfer(thisRegister);
    result = SPI.transfer(0x00);
  digitalWrite(PinBaroChipSelectPin, HIGH);
  return(result);
}

void connectWifi()
{
  // Connects the arduino to a WPA2 WiFi network
}

PaulS

Quote
This particular barometric sensor uses, as far as I can understand, three SPI pins: SDI, SDO and SCK. By messing around with it, it seems that only SDO is affecting the WiFi shield. Is this the only one I need to remap?

You can't. The SPI pins are fixed in hardware. As I said, you can have any number of devices connected to the SPI pins at one time, as long as each has a separate slave/chip select pin.

The WiFi shield uses pin 10 as the slave select pin. The pressure sensor can use any unused pin as it's slave select pin (as long as the corresponding pin on the pressure sensor is connected to that pin on the Arduino).

Quote
In my code, pasted below, I never set pins 11, 12 or 13.

Yes, you did. Right here:
Code: [Select]
#include <SPI.h>

Quote
Where and how would I make it use other pins?

You don't.


chrisconradi


Quote
This particular barometric sensor uses, as far as I can understand, three SPI pins: SDI, SDO and SCK. By messing around with it, it seems that only SDO is affecting the WiFi shield. Is this the only one I need to remap?

You can't. The SPI pins are fixed in hardware. As I said, you can have any number of devices connected to the SPI pins at one time, as long as each has a separate slave/chip select pin.

The WiFi shield uses pin 10 as the slave select pin. The pressure sensor can use any unused pin as it's slave select pin (as long as the corresponding pin on the pressure sensor is connected to that pin on the Arduino).


Again, thanks for the reply!

Hmmm... I am currently using the following pins for my barometric sensor:
pin8 as sleep/shutdown
pin9 as chip select
pin11 as serial data in
pin12 as serial data out
pin13 as serial clock

Shouldn't this in theory work fine with the wifi shield? Any tips on troubleshooting this?

Go Up