No. of GPIO pins required for all the sensors

I am working on a 4WD car project and I'm using the following electronic parts:

• H-Bridge, L298N: 2 PWM pins and 4 direction contol pins
• 8 Ultrasonic sensors, HC-SR04: 2 pins, for TRIG and ECHO
• IMU, MPU6050: 2 pins, I'm not sure
• 2 Speed Sensors, H206: 1 output pin
• Servo Motor: 1 PWM pin

There are 40 pins on the Raspberry Pi; a few are reserved for ground and 5v power supply and the PWM output can be controlled with certain pins. So I'm assuming that I need to increase the number of GPIO pins with an I/O expander like the MCP23017, or can I make it work on the same Pi?

• For starters, add how many pins all those parts need (per part). I'm not looking it up, and I doubt someone else will too – Swedgin Mar 3 '20 at 13:23
• Also, your side note doesn't belong here, but in a seperate question. Though you need to add how much current everything draws – Swedgin Mar 3 '20 at 13:24
• @Swedgin, I have edited the question, please take a look – Incompleteness Mar 3 '20 at 14:00
• You're in luck @joan did it for you. – Swedgin Mar 3 '20 at 14:01

From memory.

H-Bridge l298n         4 x GPIO
8 Ultrasonic HC-SR04  16 x GPIO
IMU-MPU6050            2 x GPIO (I2C GPIO#2 and GPIO#3)
2 Speed Sensors(H206)  2 x GPIO (not sure probably)
Servo Motor            1 x GPIO


So a total of 25 GPIO. There are 26 GPIO available on the Pis with the 40-pin expansion header.

Depending on how the sonar rangers are triggered you might only need one trigger GPIO (rather than one per ranger).

If ENA/ENB are used for speed control rather than IN1/2/3/4 you would need two more GPIO for the L298N.

• HC-SR04 uses 2 right one each for TRIG and ECHO and it would be 6 for the h-bridge including the pwm pins and the servo would use another pwm pin right? I have edited the question so please take a look – Incompleteness Mar 3 '20 at 14:01
• @Incompleteness You could use a demultiplexer to drive the trigger pins of the HC-SR04 if they will be triggered seperately. – Swedgin Mar 3 '20 at 14:53
• @joan how do control speed with only IN1/2/3/4, I thought they were only for turning the wheel forward and backward – Incompleteness Mar 3 '20 at 16:37
• You apply PWM to IN1/2/3/4. The motor driver L9110S only has IN1/2/3/4. It does not have ENA/ENB. – joan Mar 3 '20 at 17:13