I have been trying to find answers on Google to this question but cannot seem to find any answers that actually pertain to what I am wanting to know. I would consider myself a beginner in the area of electronics having some experience but not understanding how everything comes together outside of the some of the basics. What I am wondering is this; How can I tell if I need to use a resistor in a circuit? More specifically, When I am hooking sensors up to an Arduino, how can I determine if I need resistors in the circuit? Anywhere I look I typically see the default LED/Resistor example but that is not helpful. It doesn't explain how Arduino/Raspberry PI modules and sensors fit into the equation. I know the data sheets for the various modules/sensors explain how much current is needed/used by the device but how do I take that and apply it to an Arduino circuit? Thanks in advance.
From the specifications of the sensor, you know the maximum current the sensor should be allowed to draw. Suppose you need to put 5v across the sensor.
Then write down Ohm's law: E = IR (the voltage across a component is given by the product of its resistance and the current through it. You need to know the value of a resistor to put in series with the sensor to limit the current to that maximum value or less. So solve Ohms law for resistance (R) of voltage (E):
R = E/I or 5v / maximum current.
That gives you the values of a series resistor to limit the current to the maximum, assuming the sensor has no (or very little) internal resistance of its own.
Or let's look at an LED, where the spec sheet gives you both the maximum current and the internal voltage drop (called "forward voltage"). If you don't need the LED to run at full power (and you probably don't), it will last longer if you limit it to less than that, so let's take 75% as a working figure and calculate the serial resistor to give 75% of maximum current. Red LEDs typically have 2v forward voltage and 15 milliamps max current (but don't build for "typical" parts - build for the part you have). We'll assume a 5v power supply:
R = E/I = (5v - 2v) / (.015 X 0.75) = 3 / .01125 = 266 Ohms. So we'd pick an available resistor value close to 266 Ohms (no need to be exact here - we've already left a safety factor by reducing the current 25%).
If you calculate the voltage (again, using Ohm's law) across the 266 (or whatever you used) Ohm resistor, you'll find a voltage drop of about 3v, and adding the LED's forward voltage of 2V, you'll get the power-supply voltage of 5v.
Why do we need a resistor on an LED which draws 15mA of current which the Arduino can supply nicely, but we don't on a sensor like the HC-SR04 which draws 15mA of current?
It's about the behavior of the component or module itself, and that needs a datasheet for the component and spec-sheet for the module. An LED has a forward voltage drop but otherwise looks like a nearly short circuit, so it requires a resistor to protect it from over-current. A module often provides some circuitry around the component to help the user integrate into their project. Of course, how much help it provide is up to the designer. One module may offer nothing more then breadboard-compatible pins where another one - for the same device - may have power-conditioning or power supply components, 3.3v <-> 5v logic-level shifters, and protective circuitry to help protect an expensive component from misuse.
Why does this component need a resistor and another one doesn't? It's up to the electrical behavior of those components. Here's a trivial, but I hope illustrative example:
The device on the left might be considered an LED module, compared to the one on the right which is just an LED. Obviously, all I've done is to solder the current limiting resistor on place one leg to make a ready-to-go breadboard LED. It is designed for 5v circuits; putting it in a 12v circuit would fry the LED. Here is where I (as the "module designer") used the LED's datasheet to know what circuitry it needed to protect it, and as the "consumer", would need to consult the spec sheet for the module to know how to apply it. If I were selling these things, they'd come with a Spec Sheet: "Only use this LED module in 5v or less applications."] Hmph - pretty trivial example, but you get the idea.
How can I tell if I need to use a resistor in a circuit?
You can tell because the circuit includes a resistor.
If a circuit needs a resistor the circuit diagram will include a resistor. If it doesn't, then it won't.
The circuit for connecting an LED to an Arduino consists of an Arduino, an LED and a resistor. Because of that you know it needs a resistor. Because it says it needs a resistor.
A resistor drops a certain voltage across it when a specific current flows through it (Read up on Ohm's Law...). If you need to drop a specific voltage with a specific known current, or if you need to cause a specific current to flow with a fixed voltage, then you use a resistor for that job.
You don't just throw resistors willy-nilly into every circuit you make simply because you see one being used with an LED.
If a device says it runs off 5V and uses 300mA current then you know that you need to hook it up to a power source that is 5V. You also know that the module will use up to 300mA of current from that source, so your source has to be capable of providing at least that much current.