Arduino Uno Multiple SPI channels?

hello does the Arduino Uno Multiple SPI channels? why I'm asking is because i know the Ethernet shield is using D13, 12, 11 and 10 which are the SPI pins but i have a 12 bit adc convertor chip that requires to be on digital pin 13,12,11 and 10 can this be done?

Joseph

11-12-13 all get connected in parallel to all devices.
Master must then control a seperate slave select (chip select) to all devices.
In addition, 10 must be an output (whether used as chip select or not) for the Uno to be the SPI master.
Both devices cannot be 10 - you must seperate them, or perhaps demultiplex them with another logic device (a simple 2-input OR gate, you decide in between calls to either device where pin 10 will be allowed to go low).

ok so for the pin 13,12,11 together is ok but the selector on pin 10 is the problem so what happens if i say in the sketch pin 10 is for the Ethernet and pin 9 for the ADC convertor or am i wrong sorry still kind of new to this whole programing arduinos. what is seperate demultiplex mean?

Joseph

or would it be better to do a I2C ADC convertor?

Changing one of them to pin 9 would be ok as long as your wiring matches.

Hello this is the Code for the MCP3208 12 Bit ADC convertor what i would like to do is add this to the Ethernet Shield so i can read the output of it for the Voltages.

#define SELPIN 9 //Selection Pin 
#define DATAOUT 11//MOSI 
#define DATAIN  12//MISO 
#define SPICLOCK  13//Clock 
int readvalue; 

void setup(){ 
 //set pin modes 
 pinMode(SELPIN, OUTPUT); 
 pinMode(DATAOUT, OUTPUT); 
 pinMode(DATAIN, INPUT); 
 pinMode(SPICLOCK, OUTPUT); 
 //disable device to start with 
 digitalWrite(SELPIN,HIGH); 
 digitalWrite(DATAOUT,LOW); 
 digitalWrite(SPICLOCK,LOW); 

 Serial.begin(9600); 
} 

int read_adc(int channel){
  int adcvalue = 0;
  byte commandbits = B11000000; //command bits - start, mode, chn (3), dont care (3)

  //allow channel selection
  commandbits|=((channel-1)<<3);

  digitalWrite(SELPIN,LOW); //Select adc
  // setup bits to be written
  for (int i=7; i>=3; i--){
    digitalWrite(DATAOUT,commandbits&1<<i);
    //cycle clock
    digitalWrite(SPICLOCK,HIGH);
    digitalWrite(SPICLOCK,LOW);    
  }

  digitalWrite(SPICLOCK,HIGH);    //ignores 2 null bits
  digitalWrite(SPICLOCK,LOW);
  digitalWrite(SPICLOCK,HIGH);  
  digitalWrite(SPICLOCK,LOW);

  //read bits from adc
  for (int i=11; i>=0; i--){
    adcvalue+=digitalRead(DATAIN)<<i;
    //cycle clock
    digitalWrite(SPICLOCK,HIGH);
    digitalWrite(SPICLOCK,LOW);
  }
  digitalWrite(SELPIN, HIGH); //turn off device
  return adcvalue;
}


void loop() { 
 readvalue = read_adc(1); 
 Serial.println(readvalue,DEC); 
 readvalue = read_adc(2); 
 Serial.println(readvalue,DEC); 
 Serial.println(" "); 
 delay(250); 
}

This is the code for the Ethernet

/*
  Web Server
 
 A simple web server that shows the value of the analog input pins.
 using an Arduino Wiznet Ethernet shield. 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5 (optional)
 
 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe
 
 */

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,177);

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
	  client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(sensorReading);
            client.println("
");       
          }
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}

i would like to be able to read the voltages Correctly Monitoring 12v and 5V both are DC but need the mapping i never did before i think that is what it is called to read the voltages. Can someone please help me thank you.

Joseph

This one is tricky - the ethernet appears to use SPI, yet all the SPI transfers are buried away somewhere. <SPI.h> is called yet SPI.begin() is nowhere to be seen.
The code is reading the analog channels:
int sensorReading = analogRead(analogChannel);
So why not use them?
0-5V will yield a reading from 0 to 1023.
0-12V is too wide, you need to run that thru 2 resistors to make a voltage divider - 12V to 5.6K to 4K to Gnd - to output a 0-5V range. 1023 will then represent 12V, 512 = 6V, 256 = 3V, etc.

The MCP3208 wants to use SPI, yet this code is written to bit-bang out the transfers instead.

hello thank you i have all the Resistors in place that wasn't a problem it's just the coding I'm not a coder or programer i do not know how to put it together to work. this is what i need help with is the coding.

Joseph

10-bit resolution is not enough? You need 12-bit? 5V and 12V supplies typically don't vary much and 10-bit resolution is plenty to alert you to failing levels.

yeah 10 bit for me is not enough me I'm a little crazy when it comes to over the top maxing stuff out. 10Bit resolution for me is not accurate enough hehe.

Joseph

I try to mod it my self LOL Epic Fail got nothing from it well got no errors in the compiling from it either so that was ok i try to upload a sketch for the Ethernet client to see if my Ethernet shield wasn’t working and that worked ok this is the code i modded. This is my code below i moded.

/*
  Web Server


 */

// 12 Bit ADC
 #define SELPIN 8 //Selection Pin 
#define DATAOUT 11//MOSI 
#define DATAIN  12//MISO 
#define SPICLOCK  13//Clock 
int readvalue; 










#include <Wire.h>

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,10);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(8077);

void setup() {
  analogReference(DEFAULT);
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
    
    // 12 Bit ADC
     pinMode(SELPIN, OUTPUT); 
 pinMode(DATAOUT, OUTPUT); 
 pinMode(DATAIN, INPUT); 
 pinMode(SPICLOCK, OUTPUT); 
 //disable device to start with 
 digitalWrite(SELPIN,HIGH); 
 digitalWrite(DATAOUT,LOW); 
 digitalWrite(SPICLOCK,LOW); 
 
 
  }


  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}



int read_adc(int channel){
  int adcvalue = 0;
  byte commandbits = B11000000; //command bits - start, mode, chn (3), dont care (3)

  //allow channel selection
  commandbits|=((channel-1)<<3);

  digitalWrite(SELPIN,LOW); //Select adc
  // setup bits to be written
  for (int i=7; i>=3; i--){
    digitalWrite(DATAOUT,commandbits&1<<i);
    //cycle clock
    digitalWrite(SPICLOCK,HIGH);
    digitalWrite(SPICLOCK,LOW);    
  }

  digitalWrite(SPICLOCK,HIGH);    //ignores 2 null bits
  digitalWrite(SPICLOCK,LOW);
  digitalWrite(SPICLOCK,HIGH);  
  digitalWrite(SPICLOCK,LOW);

  //read bits from adc
  for (int i=11; i>=0; i--){
    adcvalue+=digitalRead(DATAIN)<<i;
    //cycle clock
    digitalWrite(SPICLOCK,HIGH);
    digitalWrite(SPICLOCK,LOW);
  }
  digitalWrite(SELPIN, HIGH); //turn off device
  return adcvalue;
}






void loop() {

  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be
//closed after completion of the response
          client.println("Refresh: 3");  // refresh the page automatically
//every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // output the value of each analog input pin
          client.println("<h1>");
          client.println("<body style=background:#000000>");

            //12 volt line connected to 1:4 divider  << 10 Bit ADC on board
          int analogChannel = 2;
          int sensorReading = analogRead(analogChannel);
          
          client.println("<font color='white'>");
          client.print("12v power supply Input pin ");
          client.print(analogChannel);
          client.print(" is ");
          client.print("( ");
          client.print(13.8*(sensorReading/1023.0));
          client.print(" v");
          client.print(" )");
          client.println("
");
          
          client.println("
");

          //5 volt line << 10 Bit ADC on board
          analogChannel = 3;
          sensorReading = analogRead(analogChannel);
          client.print("5v power supply Input pin ");
          client.print(analogChannel);
          client.print(" is ");
          client.print("( ");
          client.print(9.6*sensorReading/1023.0);
          client.print(" v");
          client.print(" )");
          client.println("
");

         
         
           client.println("
");
          
          
         //5 volt line << 10 Bit ADC on board
          analogChannel = 0;
          sensorReading = analogRead(analogChannel);
          client.print("5v Arduino Power Input pin ");
          client.print(analogChannel);
          client.print(" is ");
          client.print("( ");
          client.print(9.1*sensorReading/1023.0);
          client.print(" v");
          client.print(" )");
          client.println("
");
          
          client.println("
");
          
          // 12 Bit ADC
         //5 volt line  << 12Bit ADC  Code
           readvalue = read_adc(8);
          client.print("3.3v Arduino Power Input pin ");
          client.println(readvalue,DEC);
          client.print(" is ");
          client.print("( ");
          client.print(7.1*sensorReading/1023.0);
          client.print(" v");
          client.print(" )");
          client.println("
");
             client.println("
");
          
        
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}