2

I have a project where I want to use several motors (servo and stepper) which are activated with an RFID and show info on a display.

What’s common practice or a smart way to get past such limitations?

3
  • 1
    Either use IO expanders / shift registers, or split your project down into smaller parts each with its own controller.
    – Majenko
    Jan 15 at 14:25
  • 2
    switch from Uno to Mega
    – Juraj
    Jan 15 at 15:03
  • if you need to run things simultaneously, especially with a pwm controlled servo, go for more controllers or a controller with more hardware pwm ports.
    – Abel
    Feb 12 at 2:13

2 Answers 2

7

Basically there are 4 ways to go if you run out of pins:

  • Change to a microcontroller board with more pins
  • Arrange the parts in a smart way to save pins (like if you have many buttons or LEDs you could arrange them as a matrix)
  • Use extra peripherals like shift registers, port expanders, PWM chips, ... which need less pins to be controlled
  • Use extra microcontrollers to offload some function to them and just communicate over a data bus of your choice (like I2C or Serial) with less pins.

Which of the above is best for you depends on your project and your requirements. Here are some notes about the options:

  • Choosing a bigger microcontroller is often the easiest solution, though you need to invest in buying it and rewrite and/or rewire your project. If you change to a different kind of microcontroller it might also involve searching for alternative libraries (since often libraries are not compatible across microcontroller types)
  • Arranging buttons or LEDs in a matrix will save you pins, but you also create extra complexity for the software driving them since they need to be actively scanned through all the time. That will take new considerations regarding your program flow (keeping the program responsive using millis() and a non-blocking coding style like in the BlinkWithoutDelay example)
  • With shift registers you need to buy the actual shift registers and with that commit to a data direction (since shift registers come as shift-in and shift-out registers) (as @Mat stated in the comments there are shift registers which support both ways, but they are more complex to use than the standard ones). If you drive something with them you also need to make sure to stay without the specific limitations of that shift register. You need 3 pins for them, but you can daisy chain them. So to drive 4 shift registers you just chain them and still use the 3 pins on your microcontroller. The Arduino Framework provides the shiftIn() and shiftOut() functions to control them.
  • Port expanders usually come with either I2C or SPI interface and are more flexible than shift registers (like different data directions for the pins), but also cost a bit more and are a bit more difficult to control. The Arduino Framework doesn't have direct functions to control them. You can either use the Wire or SPI library directly (after reading and understanding the communication protocol in the datasheet of the port expander) or you might find a fitting third party library for your specific port expander.
  • For driving extra motors or servos a PWM chip can be helpful. These are usually connected via I2C or SPI and can create PWM/servo signals on their outputs. For example the 16 Channel PWM Driver from Adafruit. There are lots of similar products out there, this is just the one, that comes to my mind.
  • Using an extra microcontroller might sound a bit wasteful. But it can make things easier, especially if you have multiple things to control which overload the computation capabilities of your microcontroller (with an Uno you can easily get in that terrain). If you pursue this hobby for a while you will often have some microcontrollers lying around. You can get Arduino Nano clones from China for about 1€ per piece. Other microcontrollers are also not expensive. The advantage over the other methods is that you can offload a part of the code to the other microcontroller. While for peripherals like port expanders the logic still happens on your main controller, here you could write more complex logic into the second microcontroller. I used this one time for creating a user interface on a TFT display so that the main microcontroller was free to do its actual job. An additional advantage with extra microcontrollers is (like @Majenko wrote in the comments), that your code gets more modular. For example lets imagine you write code for the extra microcontroller to control the speed of a fan. Then you decide to add more fans to the project. With the modular design you can just add more fans and always take the same code. Or you might wanna add a fan to another project. Just take the existing code and integrate the interface to the second microcontroller in that project.

I hope this helps as an orientation. More definitive answers can only be given here, when you describe your exact situation.

5
  • 1
    Another benefit of multiple microcontrollers is that of modularity: instead of writing one piece of code to control 5 motors you can write one piece of code to control one motor and replicate it 5 times along with the exact same hardware.
    – Majenko
    Jan 15 at 15:30
  • (There are shift registers that go both ways like the 74x299. A bit more complex than the 595 though.)
    – Mat
    Jan 15 at 19:28
  • @Mat Ok, didn't know that. Great to learn something new. Thanks
    – chrisl
    Jan 16 at 0:30
  • 2
    Excellent, detailed answer. Well done!
    – Nick Gammon
    Jan 16 at 7:05
  • you kind of skirt around this, but you really should mention switching periphreals to a common bus, like I2C to drive a pwm board, a 4 chan ADC, and an LCD from 2 pins. Or WS2812B to drive 5 LEDs instead of 5 pins.
    – dandavis
    Jan 22 at 3:42
1

There are several ways, but I recommend these:

  1. MUXs/DEMUXs for I/O pins
  2. Extra peripherals for special function like SPI, USART, ADC, and so on.

1: MUX and DEMUX can make you use more pins with little pins. For example, you can control extra 2^n pins with n pins. The advantage of MUX and DEMUX is that it respond immediately. However you have to use more pins if you want to control lots of pins and it can only control one pin at a time.

2: You can use additional peripherals to use specific functions like PWM, SPI, ADC and so on. The advantage of this method is that you can use more functions and, in some cases, you control lots of peripherals with less pins. Because of these good features, the peripheral devices are relatively expensive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.