SPI Daisy Chaining nanos to a mega

Hi all,

I've been looking up ways to send over instruction data over SPI from a mega to a series of nanos. Due to the limitation of the pins available on mega, I was thinking of setting up a daisy-chain model, but I am not sure where to start in terms of programming.

I get the logic/fundamental behind how daisy chain SPI works: essentially acting as a big shift register, with one slave send over instruction to the next one.

Is there any example code I could be taking a look at? I found this: GitHub - hideakitai/SPIDaisyChain: Daisy-chained SPI library for Arduino, but I was under the impression that the slave and the master would have different codes.

Thanks in advance!

Out if curiosity, why did you choose SPI as mode of communication for your project?

Would UART not do the job then?

wooje12344:
I've been looking up ways to send over instruction data over SPI from a mega to a series of nanos. Due to the limitation of the pins available on mega,

You must have an awful lot of nanos if you have run out of pins on a Mega - which has about 60 I/O pins altogether

...R

sherzaad:
Out if curiosity, why did you choose SPI as mode of communication for your project?

Would UART not do the job then?

Honestly I'm quite new to this whole communication side of project, and it seemed to fit the job at the time of researching communication methods.

Robin2:
You must have an awful lot of nanos if you have run out of pins on a Mega - which has about 60 I/O pins altogether

...R

Quite a bit actually; thinking at least 144 right now. Making custom pcb with appropriate processor is out of my skillset at the moment unfortunately, which is why I opted for nano.

Not wishing to be over-pedantic, I think you should look a bit more closely into this, as the very concept seems absurd. SPI is a bus. This means the devices on it are wired in parallel. A daisychain is exactly what it looks like; the wiring is in series. So, if you "get the logic" of a daisy chain SPI, you are a better man than I, but I feel that you might find that anybody writing about an SPI daisychain is not going to be on GitHub for very long.

Further, I would suspect that running 144 SPI devices on a Mega is either impossible or more trouble than it is worth, as it implies you need 144 i/o pins for chip select and, as Robin points out, a Mega offers about sixty.

I hardly dare ask what you are really trying to do, but I'm game to bet that you will be better off and will save a bucketful of money if you ditched Arduino altogether and used some ESP8266 thingies, so you can run all the comms over WiFi.

wooje12344:
Honestly I’m quite new to this whole communication side of project, and it seemed to fit the job at the time of researching communication methods.

Quite a bit actually; thinking at least 144 right now. Making custom pcb with appropriate processor is out of my skillset at the moment unfortunately, which is why I opted for nano.

If its not so confidential, would you mind describing this project of yours? maybe we can then suggest to you what could be a much more suitable means of communication.
IMHO SPI does not sound as the most suitable means of communication between all those boards.

sherzaad:
IMHO SPI does not sound as the most suitable means of communication between all those boards.

Indeed, and it is worth Googling.
"How many devices can you have on the SPI bus"
It seems that the answer is four...

You may try this topology to operate 144xNANOs; but try one and then two and then 3......144, ....256. This is not a daisy chain operation; but, a simultaneous operation in sequential manner.

256Nano.png

256Nano.png

wooje12344:
thinking at least 144 right now.

I strongly suspect that SPI is not the appropriate solution.

Tell us what you are trying to make - without knowing that it is impossible to give useful advice.

...R

Nick_Pyner:
but I feel that you might find that anybody writing about an SPI daisychain is not going to be on GitHub for very long.

Further, I would suspect that running 144 SPI devices on a Mega is either impossible or more trouble than it is worth, as it implies you need 144 i/o pins for chip select and, as Robin points out, a Mega offers about sixty.

Forgive my lack of understanding of English, but I'm not sure what you're trying to imply here. I was under the impression that daisy chain SPI would require only 4 pins.

Nick_Pyner:
Indeed, and it is worth Googling.
"How many devices can you have on the SPI bus"
It seems that the answer is four...

Not sure if this is true, as I've seen projects with more than that.

sherzaad:
If its not so confidential, would you mind describing this project of yours? maybe we can then suggest to you what could be a much more suitable means of communication.
IMHO SPI does not sound as the most suitable means of communication between all those boards.

Robin2:
I strongly suspect that SPI is not the appropriate solution.

Tell us what you are trying to make - without knowing that it is impossible to give useful advice.

...R

I agree, but I'm quite clueless to this sort of thing, and SPI honestly happened to be one of the first ones I (quite hastily) thought would work.

I'm trying to control 144 modules set in 12 by 12 array to shift a surface about two feet by two feet. Each module will have trigger switches to respond and control the servo, and I want each module to receive a simple position data from the Mega/computer. I was planning on using the mega simply as a distributor to send out the positional data to the modules.
Is there a better way to (near) simultaneously send instructions to 144 modules?

Again, much thanks for your help.

wooje12344:
Forgive my lack of understanding of English, but I'm not sure what you're trying to imply here. I was under the impression that daisy chain SPI would require only 4 pins.

You have obviously forgotten the CS pin - one for each device. By virtue of what it is, the CS pin is not actually part of the bus, but it is also not the sort of thing you want to forget about. I understand that, if you only have ONE device on the SPI bus you may dispense with the CS pin but, in that situation, the fact that the SPI bus is a bus is barely relevant.

What I was also saying is that a bus and a daisychain are contradiction in terms, and anybody writing about a "daisychain bus" as one, probably has no understanding of either. While English may be a second language, you obviously have a pretty good grasp of it, and I am being as lucid as I can.

Not sure if this is true, as I've seen projects with more than that.

Well, that is what Google said. I know there is a lot of junk on the internet but, since I have never seen, or been able to imagine, more than two myself, I would rather believe Google than you. It could be that you are right, sort of, but it may be that

  1. You didn't see what you thought you saw, or
  2. It didn't work, or
  3. It was a stupid idea, or
  4. it is more trouble than it is worth.
    It is likely that an example of the last is clearly visible in reply#7, and Golam doesn't seem overly enthusiastic about it either.

Is there a better way to (near) simultaneously send instructions to 144 modules?

As offered in reply #4, WiFi.
What you intend is beyond me, but I still fail to see why you would use any other means. One thing you can be pretty sure of with WiFi is the total absence of argument about how many slave modules you can entertain.

wooje12344:
Forgive my lack of understanding of English, but I'm not sure what you're trying to imply here. I was under the impression that daisy chain SPI would require only 4 pins.
Not sure if this is true, as I've seen projects with more than that.
I agree, but I'm quite clueless to this sort of thing, and SPI honestly happened to be one of the first ones I (quite hastily) thought would work.

I'm trying to control 144 modules set in 12 by 12 array to shift a surface about two feet by two feet. Each module will have trigger switches to respond and control the servo, and I want each module to receive a simple position data from the Mega/computer. I was planning on using the mega simply as a distributor to send out the positional data to the modules.
Is there a better way to (near) simultaneously send instructions to 144 modules?

Again, much thanks for your help.

since your setup does not seem to be over a big distance, if it was me and did not want to add more hardware, I would try networking all the boards as follows:


(you would need to add those diodes!)

I would then structure the serial data to be transmitted in 2 parts:

  1. the header: basically acts as a unique identifier for each board. 1 byte would be enough IMHO (1 - 144)
  2. the actual data - ie your servo position data.
    You have have a look as Serial Input Basics tutorial to understand how to setup and parse serial data.

this setup had the advantage that all nodes will 'immediately' have the position information that any node would transmit and does not need a 'master' unit so to speak.

Of course there are other means of networking these boards; the final choice is yours! :wink:

hope that helps....

Nick_Pyner:
You have obviously forgotten the CS pin - one for each device. By virtue of what it is, the CS pin is not actually part of the bus, but it is also not the sort of thing you want to forget about. I understand that, if you only have ONE device on the SPI bus you may dispense with the CS pin but, in that situation, the fact that the SPI bus is a bus is barely relevant.

I was under the impression the line is shared among all the slaves, as all the information on the conventional model and daisy chain model seem to come with following diagrams. But then again, I'm basically clueless in mcu communications so I might be misunderstanding.

Nick_Pyner:
What I was also saying is that a bus and a daisychain are contradiction in terms, and anybody writing about a "daisychain bus" as one, probably has no understanding of either. While English may be a second language, you obviously have a pretty good grasp of it, and I am being as lucid as I can.

Not sure when I used the term daisy chain bus, but it is true I have no understanding of either. That's why I'm the one asking questions and you're the one answering.

Nick_Pyner:
Well, that is what Google said. I know there is a lot of junk on the internet but, since I have never seen, or been able to imagine, more than two myself, I would rather believe Google than you. It could be that you are right, sort of, but it may be that

  1. You didn't see what you thought you saw.
  2. It didn't work
  3. It was a stupid idea
  4. it is more trouble than it is worth.
    It is possible that an example of the last is clearly visible in reply#7, and Golam doesn't seem very enthusiastic about it either.As offered in reply #4, WiFi.

I would obviously like to have less nanos involved and have one driving several servos, but the people involved in the project seem to really like the idea of having a modular system that can be expanded without tweaking the design too much every time an adjustment is made. I'll try looking into WiFi via ESP8266.

wooje12344:
I'm trying to control 144 modules set in 12 by 12 array to shift a surface about two feet by two feet. Each module will have trigger switches to respond and control the servo, and I want each module to receive a simple position data from the Mega/computer.

Let's put the question of how to implement this to one side for a moment and use the time to consider all the details of what this project is required to do.

  • What is the overall purpose of the project?
  • What sort of surface are you talking about?
  • What level of precision is required for the motion?
  • What is the longest acceptable time between the motion of the first and last module?
  • What exactly is in each of the modules?
  • How far apart are the modules?

...R

Robin2:
Let's put the question of how to implement this to one side for a moment and use the time to consider all the details of what this project is required to do.

  • What is the overall purpose of the project?
  • What sort of surface are you talking about?
  • What level of precision is required for the motion?
  • What is the longest acceptable time between the motion of the first and last module?
  • What exactly is in each of the modules?
  • How far apart are the modules?

...R

  1. It's for an academic paper, which is why I am hesitant to disclose too much detail.
  2. A reflective surface is attached to each servo to change the amount of light through.
  3. The servo will be switching between three states to alter the angle of the surface attached to each module, making constant minute adjustments from sensor feedback within each module.
  4. I would imagine within a second from the first to the last
  5. A module will consist of a servo and a couple of trigger switches
  6. The modules are right next to each other in a fixed array of 12 by 12, all fitting within a two feet by two feet space.

As the servos are all close together and as a relatively long time is permissible for all the updates if this was my project I would build it with 3 Megas - each controlling about 48 servos. There seems no need for all the servos to be attached at one time so I can't see any obstacle to a Mega working its way through a series of 48 and updating them one (or a few) at a time.

Megas have 3 spare HardwareSerial ports so if one of them is designated as master it could easily communicate with the other two.

It will be essential to have an adequate power supply for the servos based on the maximum number that will be moving at any one time.

...R

Robin2:
As the servos are all close together and as a relatively long time is permissible for all the updates if this was my project I would build it with 3 Megas - each controlling about 48 servos. ...

or use 9 of these PCA9685 16-Channel Servo Driver + 1 mega or any arduino of your choice

sherzaad:
or use 9 of these PCA9685 16-Channel Servo Driver + 1 mega or any arduino of your choice

I had forgotten about those. But it seems that each one is about the same price as a Mega clone.

...R

wooje12344:
Not sure when I used the term daisy chain bus,

You raised it in your first post but, OK, I took a deep breath and had a trawl. I see that you are not completely alone after all, and

does give it a mention. It would appear that it might fit my third, and possibly fourth, categories. I assume your diagram with MISO > MOSI pins indeed daisychained is the same sort of thing as the linked article, which merits only the most casual glance.

I would obviously like to have less nanos involved and have one driving several servos, but the people involved in the project seem to really like the idea of having a modular system that can be expanded without tweaking the design too much every time an adjustment is made.

I suggest you look at Pieter P's ESP8266 magnum opus, and I don't think you should be using any Nanos at all - and no Mega either. You may have priorities that are not clear and I can't comment on the "people involved" other than that I don't think they could have looked at the problem any more seriously than you have.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.