I am trying to control a 5V pump with arduino and ultrasonic sensor however i can't make it work with the pump connected to a pin.
I saw on the forum that you need transistor or relay but i don't understand why ?
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It only takes a minute to sign up.Sign up to join this community
Probably because the pump will use more current (mA) than a GPIO pin of the Arduino can deliver.
A GPIO pin on the Arduino can deliver 20 mA, or 40 mA max (but not advised for longer time).
Therefore, a separate power supplyis used, where the base pin of a transistor is connected to a GPIO pin (with a resistor) to 'switch' the transistor on and off. The transistor base can be very low current (way within a GPIO's current range), which result in a much bigger current from the collector (connected to the power supply) to the emitter (connected to GND). The pump is somewhere between the external power supply and GND).
If you search for 'transistor as a switch' you will find many examples.
You also can use a relay if you don't need to switch it on often. Relay's are typically used for net-powered devices (110+ volts) and/or high current. See also Duncan's comments for additional notes regarding relays.
@michel-keijzers has pointed you in the right direction. Depending upon the power requirements for your pump, you might consider looking specifically at logic-level power MOSFETs as the type of transistor in your circuit.
You might also investigate the use of a pull-up resistor to fully turn on a MOSFET.