How I can connect more than 10 sensors to Arduino Uno R3? I calculated it and I can connect just 4 fire flame sensors (sensors which can find a fire (a candle, for example)) and maybe 1 ultrasonic sensor. I need more for my project. I need an IR Sensor, a fire flame sensor on every side, and 1 ultrasonic sensor in front. Additionally, I need to connect 2 DC motors, 1 fan, and 1 servo-motor for ultrasonic sensor. How can I solve this? I must buy Arduino Mega?
-
Make a bus with protocol and some bus-devices :)– PaulMar 15, 2016 at 10:16
-
And if you don't want to make a bus: Count the pins/protocols you need to use. Make a nice diagram out of it. And judge you requirements– PaulMar 15, 2016 at 17:35
-
Please want to know how to read the recording on a computer.Am a beginner– EmmanuelMar 25, 2018 at 13:59
-
do you know how to solve the aforementioned problem with multiplexer TCA9548A?– user58617Aug 6, 2019 at 8:50
4 Answers
You can easily increase number of I/O pins using an expander, e.g. PCF8574.
The expander can be connect using your SCL/SDA pins located next to GND/AREF pins. (Here you can find detailed photo: http://forum.arduino.cc/index.php?topic=84190.0) You can connect up to 8 such expanders to extend the number of your digital pins to 64.
Once you connect it, here is a code snippet of how to use it:
#include <PCF8574.h> //library available at: https://github.com/skywodd/pcf8574_arduino_library
void setup() {
PCF8574 sensors;
sensors.begin(0x20); //0x20 means that you connected A0, A1, A2 pins of PCF8574 to the ground
sensors.pinMode(0, INPUT_PULLUP);
sensors.pinMode(1, INPUT_PULLUP);
/*...*/
}
void loop() {
byte sensorValue = sensors.digitalRead(0); //reads digital value of 1st sensor connected to pin 0 of PCF8574 expander
/*...*/
}
You can read more with this tutorial (note: they are using wire interface, so the code will differ. Choose the one you prefer).
I wouldn't recommend multiplexing, but I do agree that one way is to add a second MCU.
Another better option is to use an N-bit shift register. These are little IC's that are exactly what the name entails: shift registers, and are much easier to setup and interface to than wasting a whole other microcontroller to add I/O's or multiplexing (which limits your application). You effectively shift in the I/O settings that you want and then can latch those registers when you have the proper I/O sequence loaded into the register. In other words if I wanted to have 8 new I/O pins, I could connect an 8-bit shift register to my MCU and if I wanted 4 of the first pins HIGH and the last 4 low, I would shift in 00001111b, then latch the data so that it appears on the output just as if it were any regular I/O pin on the MCU.
They only require a few pins to operate them (, so this is an easy way to add more sensors to your system. As always, check the datasheet to make sure the IC meets your specs (in terms of current draw, voltage, etc).
A disadvantage to this is that for an N-bit shift register, you will burn N clock cycles to load in the data. For most applications this should be fine, but it is worth noting. It also makes for slightly more complex code (although I am sure there are libraries for them out there).
Note: I saw that you mentioned controlling things such as DC motors, etc. Make sure you are not trying to directly drive them with the I/O pins, and are using a switch or transistor to control these types of peripherals.
The Mega is the easiest way to get many pins is, as you said, the Mega. It has many other advantages, such as more amperage, more memory, and more UART ports. I highly recommend doing this route for a beginner, even if you get a clone instead of an official board. If none of this would be an advantage to you, there are a few different things.
- Second MCU. You can get another chip identical to the Uno's to connect. This is called an ATmega standalone. They're fairly cheap. I2C is the easiest way to connect them, using only two pins from each chip. The setup will depend on the project.
- Multiplexing. There is a way that you can connect more stuff than there is pins on the Arduino. This is called multiplexing. It does affect performance a little, but for some applications it is worth it. Note: this won't work well with libraries or SPI/I2C. The 4051 chip seems like a decent chip. The Arduino playground has good introduction article on this chip..
There are a number of ways to get more inputs and outputs, with different implications. And it depends on the types of sensors, and on the libraries or code snippets you want to use.
The first thing you need to do is list the sensors you want, along with the type of interface each uses; I use a spreadsheet.
For example, a flame sensor could just use a single digital output (going to a digital input on the Arduino), with the threshold set with a pot on the module. Or it could output an analog signal which goes to an analog input on the Arduino, allowing the code to detect intensity or set varying thresholds without needing to move the pot. Each approach has different implications.
There are still other sensor and actuator interfaces besides these two (simple digital and analog). Some give a variable value (like analog) but based on timing a pulse on a digital signal; this is how most inexpensive ultrasonic rangefinders work. And of course there are sensors which use standard (I2C or SPI) or proprietary (one-wire protocol, or the DHT-xx temp & humidity sensors).
Likewise some outputs will need PWM or other signally, beyond simple on/off.
Extra simple digital inputs, or outputs, can be had fairly easy with I2C extenders, or with shift registers. For example, checking a digital output flame sensor, or turning on a light.
Extra analog inputs can be done with analog multiplexing (like an electronic switch), or via external ADC modules using I2C or SPI.
If timing is required, there are larger potential problems. Often you would use a library to handle such things.
Libraries. Most sensor and actuator libraries will be written to use direct input and output pins on the Arduino. You may be able to pass a pin number when initializing, but if you are using a shift register or extender there's no way to tell the library how to use your custom extension. In this case you will have to roll our own code without using the library, or make a modified version of the library which does I/O through your hardware extension hardware.
Special Pin Functions. A second problem is special pin functions; if the library or code snippet you want to use expects to use a special feature of a pin (like a pin interrupt, or a timer input capture), that may not be available from your extension hardware, or may be extra work to handle. For example, some extenders may be able to signal an interrupt pin on the Arduino when one of their inputs changes - but you'll have to write your own interrupt handler to catch that, and replace the functionality of the direct pin interrupt.
Timing. And a third problem is that sometimes tight timing is hard to manage through extension hardware.
Most of these can be resolved with appropriate effort, sometimes easy and sometimes not. Look through your list of sensors and actuators; sort out which ones can most easily be attached to extension hardware (like a shift register), and which ones should be directly attached to the Arduino for simplicity or performance. You could be lucky.
Recommendations
1) If you can handle any fancy sensors or actuators with direct pins, and cover any simple ones with shift registers or I2C extender chips, that's a viable option. For example, if your code wants to check occasionally whether a flame has been sensed (digital version), or wants to turn on a motor, it's not that hard to go through a shift register instead of doing a direct pin access.
2) If you go beyond that, and run out of pins for things that are much easier to handle when directly connected, then I suggest going to a Mega or Mega2560. It has more digital pins and more analog inputs and more PWM outputs. Most (tho not all) libraries will adapt just by changing pin numbers. It's much easier than rewriting libraries to use external digital and analog pins.