Hi was wondering is there any code any for Slave to Master data transfer using 2 Arduinos using SPI. Also what about daisy-chain SPI using arduinos?
Nick has a good tute at
Start there then get back to us if you don't understand anything.
As for daisy chaining, that's something I've tried to talk many people into with no luck so far
I haven't done it yet but I can see places where it's a good idea.
What's the application?
Rob
I don't know if you've seen this Multi-touch Input Sensor based of Arduino MegaMulti-touch Input Sensor based of Arduino MegaMulti-touch Input Sensor based of Arduino Mega | Arduino Blog i made this over a year ago and i plan to expand on to it now i was thinking about creating a cascading system utilizing arduinos and i'm just working out the best implementation for this expansion i had 2 ideas either to create a traditional SPI Master slave configuration where the master polls individual slaves for information and which it then processes and sends to the computer or to user a daisy chain where the data is collected from all arduinos then sent to a master controller in one swoop which is then processed and sent to the pc.
There's a theoretical increase in performance with daisy-chaining because you don't have a select for each slave. Also if there are a lot of slaves there's a saving in pins.
Apart from that it just seems like a neat idea.
Rob
There is also this method i used this in the board to interface with multiple ADCs this allowed for polling of multiple ADCs in parallel of sorts. I wonder if this can work with Arduinos.

This is more of a variation to the traditional bus design; SCLK, MOSI and SS are all common and MISO is not.
That's exactly the method I suggested in a thread a couple of days ago. The only hassle is that you have to "reconstruct" the data because you get 8 bytes in with all the "0" bits in one byte, all the "1" bits in the next byte etc. But depending on the application that may not be an issue.
You could to up to 8 devices this way (or more if you use a second/third/etc port).
I haven't done the maths though, because you would have to bit bang the SPI it may still be faster to daisy chain and use the SPI hardware, or use the standard technique with N select lines.
Did you measure any speed advantage?
Rob
I did notice a significant improvement over the standard technique; although both implementations i used bit bang to read the ADCs. I didn't do any measurements in terms of using a oscilloscope but i did notice a difference in my overall system. BTW i presume that the SPI library doesn't use bit bang and uses the spi hardware it self.
Yes, which is why I personally would have dedicated the extra wires to SS rather than MISO/1/2/3 - it's the same number of wires, but the hardware SPI has to use the hardware MISO line. I don't particularly like polling if it can be avoided. One way is to use I2C (which can have slaves initiate a transaction) to say "I have something" and then the master kicks in with SPI to do a high-speed download of the data.
Also I wonder if interrupts can be used in this situation instead of polling.
Thanks for all the information. After reading more i don't think theres a major advantage apart from saving pins if i were to go with Daisy Chaining. And since im using the Arduino Mega i'll more than enough pins. One thing i did come across and wanted more information on was the SS pin; the SPI hardware has only a single SS pin i presume to select other slaves on your BUS you can substitute the any DI pin as an SS pin to select a slave?
i presume to select other slaves on your BUS you can substitute the any DI pin as an SS pin to select a slave?
Correct.
Rob
Yes, but as I mention on my page above, the "hardware" SS pin must be selected as an output, even if you don't use it for one of your SS pins. So, you may as well use it for that. Then you can use any other pins of your choice to select other devices. Of course, configure them as outputs, and only bring one low at a time.
One last thing, at the moment my prototype board has the following architecture where the SPI interface between the ADC and the Arduino is Bit Bang SPI using the bus architecture model as what i have posted above.
Now what i want to do is expand on this project by having multiple modules such as this sending data to the pc for that i though i'd use another arduino (just one as a master) that acts as a data concentrator to collect all the data from multiple modules and send it to the computer. After reading what you guys said, I'm just wondering would it be possible to change the Bit Bang SPI to hardware SPI while at the same time have the master arduino acting as a data concentrator on the same bus? it would kinda be like this:
Master Arduino -> Slave Arduino -> ADC -> Sensors
-> Slave Arduino -> ADC -> Sensors
.
.
.
Now another thing, would this be the best architecture for system, i want the design optimsed for speed i.e. the data which is received at the PC (the faster it is the better), i would think daisy chaining would be faster since you won't have the master communicating to each slave saying send me data.
If using SPI hardware I don't think there's any argument that daisy-chained has to be the most efficient, there is NO overhead for multiple slaves apart from an initial "load data" signal (and not even that if you are reading directly from ADCs). After that you can read a 1000 slaves with no extra handshaking.
Note that I haven't actually done this, there may be a gotcha I haven't thought of.
The drawing shows a parallel approach that has to use bit banging. This puts it behind the 8 ball for speed for starters, but the more inputs you have the better it gets and there will be a point at which it will be faster that daisy-chaining using the SPI hardware. What that point is I don't know.
For example if you have a Mega you could feed say 48 ADC MISO signals into the processor, The code would look something like this
data[i++] = PORTA;
pulse clock pin;
data[i++] = PORTB;
pulse clock pin;
data[i++] = PORTC;
pulse clock pin;
data[i++] = PORTD;
pulse clock pin;
data[i++] = PORTF;
pulse clock pin;
data[i++] = PORTG;
tell ADCs to start a new reading
Assuming that the various port pins are all available and only 8 bits per reading. This data will of course have to be reconstructed later.
If you need faster then the next thing to do is add hardware.
The drawing shows 8 sensors going to a single ADC, are they 8-input chips? What sample rate do you need?
At 8 bits each that's 1024 bits of data, have you done the maths? At the end of the day all this data has to get to a PC and AFAIK the fastest you can do this from an Arduino is 115200bps (via USB anyway, maybe using Ethernet would be faster).
Rob
Nobody spotted the deliberate error then?
for (x = 0; x < 8; x++) {
pulse clock pin;
data[i++] = PORTA; // all the 0 bits
data[i++] = PORTB;
data[i++] = PORTC;
data[i++] = PORTD;
data[i++] = PORTF;
data[i++] = PORTG; // all the 7 bits
}
tell ADCs to start a new reading.
I thought about it while moving camp today but was out of range and anyway had to drive all day.
Rob
Graynomad:
Nobody spotted the deliberate error then?for (x = 0; x < 8; x++) {
pulse clock pin;
data[i++] = PORTA; // all the 0 bits
data[i++] = PORTB;
data[i++] = PORTC;
data[i++] = PORTD;
data[i++] = PORTF;
data[i++] = PORTG; // all the 7 bits
}
tell ADCs to start a new reading.
I thought about it while moving camp today but was out of range and anyway had to drive all day. ______ Rob
Best not to argue with a gray nomad, eh? Anyway, what happened to PORTE?
As was just pointed out in another thread my spelling ain't reel good
I knows me numbers but have trouble wiff me lettuce.
Rob
The drawing shows 8 sensors going to a single ADC, are they 8-input chips? What sample rate do you need?
At 8 bits each that's 1024 bits of data, have you done the maths? At the end of the day all this data has to get to a PC and AFAIK the fastest you can do this from an Arduino is 115200bps (via USB anyway, maybe using Ethernet would be faster).
Yes, I'm using a MCP3208 its a 8 Channel, 12-bit ADC. For my final prototype i intend to use 5390 sensors; however i plan to threshold sensor values to restrict the amount of data flow; Further more i plan limit the number of inputs to 30 which equates to about 175 sensors; this should decrease the bandwidth i need even further- 1751250 (number of sensors * bits per sensor * samples per second) = 105000bps this is under the 115200bps that the Arduino can output. So theoretically it should work.
105000bps this is under the 115200bps that the Arduino can output
Only just, not much room for sloppy code.
Rob
I guess you are right i've been thinking it over all day yesterday, Ethernet with a simple chat server maybe the way to go it should be much faster for communication plus it'll give me additional bandwidth just in case for future expansion. So reading over everything you guys are recommending that i keep the parallel approach for acquiring data from the sensors to the slave arduino but i should go with a traditional master->slave hardware spi configuration for data concentration on the master arduino and ouput it all via ethernet?
I think the parallel approach is good for a LOT of inputs, but as I said you'd have to do the maths to see at what point it makes sense.
OK, I just had a look at the 3208 specs, it seems that every transaction needs 3 bytes (the first one sets the channel) and there's no way around that. So all of a sudden the time for each reading is 3x what we've been talking about.
And because you can't daisy chain the ADCs you can't use SPI hardware in any mode but the normal "multiple SS" pins. So yes if you want to be able to expand the system I think you should have slave Arduinos that handle N ADCs. These slaves should probably be daisy-chained but that depends on the maximum number of slaves you think the system will go to.
The slaves handle the ADCs and reduce each sensor to an 8-bit value stored in an array. On receiving a "SPI about to start" pulse from the master they load the next value from the array into SPDR.
The master then does SPI.transfer() N times where N is the number of slaves.
Thus expansion is simply a matter of changing N, up to the point where the available bandwidth or capability of the master is exceeded.
Rob
