I am working on a project that requires more pins than what an Arduino Uno has. Hence, I have learnt how to connect two arduinos and establish a communication between them. Being new to programming, I would like to know how to program such a two arduino system. For example, a sensor at arduino 1 should switch on an led at arduino 2.
Or should I get a bigger arduino such as the Mega.
Usually you need two Arduinos when there's a long distance between the sensors. A weather station has sensors and displays inside the house, with a long wire or wireless connection to an outdoor unit.
If everything is nearby then use the bigger processor.
bob1609:
Being new to programming, I would like to know how to program such a two arduino system. For example, a sensor at arduino 1 should switch on an led at arduino 2.
First, I agree with what @MorganS has said in Reply #1
But there are circumstances in which multiple Arduinos make sense even at closer range. Then you need to decide how to communicate between them. Serial is an obvious option but SPI and I2C could be suitable for very short distances. For longer distances Serial using RS485 can be used.
When you have decided how you want to communicate then you need to decide what you want to communicate - the content of the messages. In general, keep the messages as short as possible. For example using Serial a command to turn on an LED on pin 7 could be <7,1> or, if you want to send data for several LEDs and if the pins are always the same and known in advance you could send <0,0,1,1,0> which would turn the first two off, the next two on and the last one off.
If they're next to one another you could indeed communicate using I2C, but unless you have a pressing need for a second Arduino I think getting a port extender or an Arduino with more ports is the easier and more sensible way to go. Those port extenders in turn connect over I2C so no extra pins needed assuming you're using I2C already.
Getting sensors that connect over I2C also can help greatly to reduce pin requirements.
Do check what you would want to move to the port extender. They can drive an LED just fine, but you can't read certain sensors such as the DHT22 over a port extender.
Expanders go for a fraction of that price - and add less complexity to the programming and intercommunication.
If you need more speed or internal memory, you can get an 80 MHz ESP8266 for about the same price. 1 GB, 80 kB variable memory, and even WiFi built in. Still less programming headaches than adding a second device and try to keep them in sync.
MCP23018 are just over £1 from RS and Farnel
I have systems with 3 or 4 MCP23018 devices - I woild not wish to have to program 3 or 4 processors and deal with all the communication overheads
I ran into a similar issue for a design project in school recently. Take a look at the Wire.h library. I used it to sync up the timing between multiple servos but its basically set up to create a "master" arduino to send commands and a "slave" arduino to receive and execute.
horace:
MCP23018 are just over £1 from RS and Farnel
I have systems with 3 or 4 MCP23018 devices - I woild not wish to have to program 3 or 4 processors and deal with all the communication overheads
Why not? It's very simple. I had 3 slave nanos controlled by a master mega, each controlling 2 steppers each and each with 2 sensors in around 30 minutes. It was effortless apart from a bug in the stepper library. This way each nano can do it's own separate loop command checking sensors and running the steppers, and will act on communication from the mega of when to start / stop etc.
MorganS:
Usually you need two Arduinos when there's a long distance between the sensors. A weather station has sensors and displays inside the house, with a long wire or wireless connection to an outdoor unit.
If everything is nearby then use the bigger processor.
Okay thank you very much, looks like I've gotta do some arduino shopping