About the book
In this book I explain in each chapter first some basics with simple examples and then with a project the practical application. If the code is a bit longer, you can find the link to Github where you can download it. This book is written for absolute beginners and no prior knowledge is needed. I use different hints in this book to visually highlight what is most important. I would like to present you all the hints used in this book with a short description.
Version note
Because the Arduino IDE and the libraries are all about tinkering projects, it sometimes happens that the sketch no longer works after an update. Therefore please always check the versions I give, because they are tested and work.
Note
Show you succinctly how something works with a simple explanation.
ATTENTION
The most important sign in the whole book, if you do not follow what is written here, problems can occur or it can even be dangerous.
Task
Here you can find more examples and exercises on the topic.
Source Code
Here you can find the sketch for the current theme, if the code is longer, then you find here the link to Github for download.
/*
Project: DHT11 Temperature and humidity sensor
https://github.com/Edistechlab/
Author: Thomas Edlinger for www.edistechlab.com
Date: Created 29.08.2020
Version: V1.0
*/
void setup() { }
void loop() { }
Parts needed
Here you can find the links to the needed parts. The links are marked with an asterisk which means that they are so called affiliate links. If you place an order I get a small share without any additional costs for you. With this you can also support Edi's Techlab.
© Copyright 2024-2025 - Copyright notice
All contents of this book, in particular texts, photographs and graphics, are protected by copyright. Copyright is held by Thomas Edlinger, unless explicitly stated otherwise. Please ask me if you want to use the contents of this book. info@edistechlab.com
- I use theAdfruit libraries a lot in this book, if you want to support them, you can buy the parts from them. https://www.adafruit.com
- I use the Fritzing software for my circuit diagrams. https://fritzing.org
- I created the book and all graphics with software from Affinity
These links are NOT sponsored, but I think these programs are worth recommending
Electric current I
The electric current, with the unit ampere (A), is the flow or directed movement of electrons. Current can only flow when there are sufficient free and mobile charge carriers between two different electric charges. For a better idea, we can think of the flow of electricity as flowing water in a pipe.The more water flows through the pipe, the greater the quantity of water. Electricity behaves in the same way: the more electrons flow through a conductor, the higher the electric current strength.
Current flow
Current in a circuit
Electric currents flow in most cases in a closed circuit, for this the following three components are needed.
Voltage source: The voltage source is needed to exert a “pressure” on the electrons. This pressure sets the electrons in motion.
Consumer: We use the term “consumer” to refer to components or devices, such as light bulbs or a hair dryer, which start to glow as a result of the electric current, or generate warm air to dry hair. Here it also becomes clear that electricity in the actual sense is not consumed, but merely converted. The assumption of the “current consumption” has, like that of the current direction also, an equally stored, historical origin - in addition however equal more.
Electric lines: To allow electrons to move freely, the voltage source is connected to electrical wires and the consumer to form a circuit - the electric circuit. These lines are usually made of copper or aluminum.
Picture 1.1.1: Model for current in a conductor
Picture 1.1.2: Simple circuit with a lamp
Current direction (technical/physical)
We distinguish between the technical and physical direction of current. In the technical direction of current, the flow of electrons runs from plus to minus; in the physical direction of current, it is exactly the opposite.
The background of these two different current directions has - like the assumption of the “current consumption” - its origin in history. When the current was discovered, it was determined that the current flows from the positive pole to the negative pole. With the progress of knowledge about the electricity, in particular at the beginning of the atomic physics, one noticed the “wrong” assumption made before. One agreed on the designation physical current direction. In addition, it was decided to continue to use the original direction of current and to call it the technical direction of current. In circuit diagrams, unless otherwise specified, the technical current direction is always used.
Calculation of the current strength - Ohm's law
To be able to calculate the current strength, I would like to introduce three different formulas to you here. The first formula comes from the famous Ohm's law. Variations of Ohm's law lead in the further to calculations of the power and electricity quantity.
Please note that in mathematical formulas and circuit diagrams, however, not the SI units but uniformly defined formula letters are used. For example, we know that electric current carries the SI unit „A“ for ampere. In formulas, however, the letter „I“ (capital i) is used. Only, the result of the current calculation leads then again the physical unit „A“. The same applies to the voltage, which we measure in volts (SI unit „V“), the formula letter „U“. For the resistance, after whose name the Ohm's law is also named, the formula letter is „R“ and stands for the ohm - for this, the Greek letter omega „Ω“ is also used as the SI unit.
Picture 1.1.3: Technical current direction (+ to -)
Picture 1.1.4: Physical current direction (- to +) Electron flow direction
The results of the above calculations all carry the SI unit A for ampere or powers derived from it, such as “mA” (milli-ampere =1/1000A) or “kA” (kilo-ampere =1,000A).
To complete Ohm's law - this describes the always same relationships between current (I), voltage (U) and resistance (R) and can be connected via a “magic triangle”.
RUx
IDepending on what you want to calculate, simply cover the relevant formula letter and read the rest as “formula”. Thus, if you want to calculate the current and cover the I accordingly, you will only see U/R, which tells you to divide the measured voltage (U) by the resistance (R). Proceed in the same way to calculate the voltage: Cover the U and read and calculate RxI, i.e. current (I) times the resistance (R). Ohm's law can be as simple as that, but in electrical engineering it is an elementary component and basic knowledge.
I will go into the details of voltage and resistance in the following chapters.
Currentflow
Voltage
Circuit symbols for the voltage source
Depending on the type of voltage source, there are two different circuit symbols. The general circuit symbol for a voltage source or, if it is a battery, you can use the circuit symbol provided for it to define the voltage source more precisely.
U
Picture 1.1.5: Model for the voltage
Picture 1.1.7: Voltage source (battery) circuit symbols
Picture 1.1.6: Voltage source circuit symbols
Direct and alternating current
With direct current, the magnitude and direction of the voltage is the same at all times. The current flows in a constant direction. Direct current is often abbreviated DC and an example of a voltage source of this type is the battery.
With alternating current, on the other hand, the magnitude and direction of the voltage changes constantly and cyclically. Accordingly, the direction in which the current flows also changes. The alternating current is abbreviated as AC. The AC is provided to us as useful electrical energy at the sockets, with an effective value of the voltage of 230 volts at 50 hertz. This means that the voltage oscillates sinusoidally from +325 volts to -325 volts and back 50 times in one second.
In our electronics projects, we only use DC voltage for the power supply, which is why I will not go intoAC voltage any further here.
Calculation of the electrical voltage
I would like to present you three of the numerous formulas for the calculation of the voltage. The first formula comes again from the Ohm's law mentioned earlier. The second formula describes the relationship between voltage to work and charge. The last formula serves the voltage calculation over the power and the current.
Picture 1.1.8: Sine wave of an alternating current
Resistance
Function of a resistor
If the electric charge moves, then you get electric current, which is driven by the charge. A resistor hinders the flow of the electric charge. It can be said that almost any conductor (except superconductor) or device is a resistor.
Resistors have a wide variety of uses, whether for current limiting, e.g., for LEDs, as voltage dividers, or as pull-up or pull-down resistors.
Resistance has the formula symbol R and is expressed in Ω (ohms). We can determine the resistance using the Ohm's law with the following formula.
Resistance = Voltage Current R = U I
„Normal“ resistors
Resistors usually consist of an insulating porcelain body covered with a thin layer of carbon or metal and a protective lacquer. Carbon layer resistors are usually ocher colored, metal layer resistors are usually painted blue. The resistance value is then printed on the protective lacquer in the form of colored rings to make it equally legible without a magnifying glass and from all sides.
The circuit symbol for a resistor in Europe shown on the left side is a rectangle, on the right side you see how the circuit symbol looks like in the USA. Since many circuit diagrams come from there, it is important to know this sign.
Resistor color code table
Before you start determining the value of resistors, you need to count how many color rings are printed on the resistor. Carbon film resistors usually have 4 rings. Metal film resistors have 5 rings. Resistors with 5 rings have a more accurate resistance value.
As a first step, we need to figure out where, front is and where back is. As a rule of thumb, for the blue metal film resistors we look for the brown (sometimes red) stripe and for the ochre carbon film resistors we look for the gold (sometimes silver) stripe. This indicates the tolerance in each case. This ring is then at the back and we can now start reading from the front. There are of course other colors for the indication of the tolerance, these are then rather more special resistors and are not usually used in home electronics projects.There are also resistors where the tolerance ring is slightly separated from the other color rings and therefore easier to recognize. Then you start from the front to assemble the resistor value. The individual colors are assigned specific and unique values. We take these from the following tables. The third or the fourth ring (in case of 5 rings) is the multiplier. Let's try this in practice with two examples.
Picture 1.2.1: Resistance according to EN
Picture 1.2.2: Resistance according to ANSI
Example 1:
Ring 1: brown =1
Ring 2: black = 0
Ring 3: orange = x1000 (multiplier)
Ring 4: gold 5% (tolerance)
10 x 1000 = 10k Ohm ± 5%
Example 2:
Ring 1: red = 2
Ring 2: red = 2
Ring 3: black = 0
Ring 4: brown = x10 (multiplier)
Ring 5: brown 1% (tolerance)
220 x 10 = 2.2k Ohm ± 1%
Ring color Ring 1 Ring 2 Ring 3 Ring 4 (multiplier) Ring 5 (tolerance)
Please note that for carbon film resistors - i.e. those that have only fourth rings printed on them and are ocher colored - the third ring is already the multiplication factor. You skip in the table above the column labeled „3rd ring“ - this column is explicitly and without exception only for the metal film resistors.
Temperature-dependent resistors
Almost all electrical components usually also exhibit a slightly higher resistance at higher temperatures. However, there are also semiconductor materials in which this temperature dependence is very pronounced and is used specifically. As special resistors, these particular materials are often used as temperature sensors.
The following is an overview of the different resistors and their possible applications.
Table 1.2.1 Resistor color code table
Picture 1.2.3: Resistor 4 rings
Picture 1.2.4: Resistor 5 rings
PTC resistors
PTC thermistors or PTC resistors „Positive Temperature Coefficient“ is the name given to substances whose resistance increases with increasing temperatures. Mostly used the PT100 or PT1000. However, the „PT“ does not come from the type of resistor (PTC), but is derived here from the resistor material - the chemical element designation for platinum.
Picture 1.2.5: PTC resistor circuit symbol
NTC resistors
Application
Temperature control
Liquid level sensor
NTC thermistors or NTC resistors „Negative Temperature Coefficient“ are substances whose resistance decreases with increasing temperatures.
Picture 1.2.6: NTC resistor circuit symbol
Photoresistors
Application
Temperature sensor
Temp. stabilization of
semiconductor circuits
In addition to temperature-dependent resistors, there are also resistors that are exposure-dependent. The photoresistor or LDR resistor called “Light Dependent Resistor” has a resistance of a few hundred ohms at full illumination. As the light intensity decreases, the resistance increases and can rise to a few mega ohms. The change in resistance of an LDR is very slow compared to the temperature dependent resistors described above.
Picture 1.2.7: Photoresistor circuit symbol
Varistors
Application
Illuminance measurement
Twilight switch
Flame detector
A varistor or VDR resistor „Voltage Dependent Resistor“ is an electrical resistor whose value depends on the applied voltage. The resistance value of a varistor behaves in the opposite direction to the applied voltage. This means that the resistance decreases with increasing voltage and increases with decreasing voltage.
Picture 1.2.8: Varistor circuit symbol U
Application
Voltage limitation
Voltage stabilization
Overvoltage protection
Adjustable resistors
An adjustable resistor is a resistor whose resistance value can be changed mechanically via a sliding contact, by turning or moving it.
An adjustable resistor value has a low and a high value. The minimum value can be 0 Ω, for example. The maximum value results from the resistance designation. Each potentiometer, also known as a “poti” for short, has three terminals, with the full resistance value specified on the potentiometer appearing between the two outer terminals.
If one of the end contacts and the middle tap from the potentiometer is connected to the circuit, the resistance can be adjusted between zero and the maximum value with the help of a rotary knob or slider.
1.2.9: Poti circuit symbol
Application
Variable voltage divider
Variable resistors
Finally, it should not go unmentioned that every electrical line in itself has a certain resistance. This resistance can also be determined as a function of the cable cross-section, the cable length and the respective cable material. An overload of the electrical lines leads to a heating of these, which can lead up to the cable fire. The dangers that can emanate from electricity should therefore not be underestimated.
If you already have experience with the Arduino IDE, then you can build this small example that writes the value of the potentiometer every 0.5 seconds in the serial monitor.
Picture 1.2.10: Node-MCU structure with Poti
Source Code https://github.com/Edistechlab/
int potpin = 0; //Analog pin A0 for the potentiometer
void setup() { Serial.begin(115200); }
void loop() { Serial.println(analogRead(potpin)); // (Value between 0 and 1023) delay(500); }
Picture
Pull-up / Pull-down resistors
„Pull“ means to pull, „up“ means to go up and „down“ means to go down. So a pull-up resistor „pulls“ something up, the pull-down resistor pulls something down. The principle is quite simple: one resistor pulls the electrical voltage up, the other pulls it down. In doing so, the voltage is usually either pulled up to the operating voltage or pulled down to GND (0V).
What do we need these pull-up / pull-down resistors for?
ARaspberry Pi orArduino has so-called pins that are used as inputs and outputs.To ensure that these work correctly, they must be in a defined state – either HIGH or LOW. However, without this definition, interference can occur: voltage fluctuations or high-frequency interference from surrounding components can cause the pin to receive an unclear signal. In such cases, an input could unintentionally switch between HIGH and LOW, triggering unwanted circuits.
To avoid this problem, we use pull-up and pull-down resistors. These resistors ensure that the state of a pin is always clear – even when no signal is applied.
Let's now take a closer look at the different applications and operating principles of these resistors. Without resistor:
+5 Volt direct
In this example we don't use a resistor and now this bears the risk that there could be a voltage at the input due to external influences that switches the Arduino. The contact is hanging in the air.
When the push button is pressed, the +5 V are present and the Arduino switches properly.
direct
Here is the same example, but this time we switch against GND. Again, the contact is hanging in the air.
When the push button is pressed, GND is present and the Arduino switches properly.
GND
Pull-up resistor:
Pull-up
Pull-down resistor:
When the push button is open, the resistor pulls up the input to +5 V. This is definitely “HIGH”. Therefore this resistor is called pull-up resistor - the resistor pulls the input up to the operating voltage.
When the push button is closed, GND is connected to the input. The voltage drops completely at the pull-up resistor and thus GND is present at the input - a definite “LOW”.
Pull-down
When the push button is open, the resistor pulls the input to GND. Here is definitely 0 V, so „LOW“. Therefore this resistor is called pull-down resistor. The input is pulled down to GND.
When the push button is closed, +5 V is connected to the input. The voltage drops completely at the pull-down resistor, whereby +5 V and thus a definite „HIGH“ is present at the input.
What values should pull-up and pull-down resistors have?
The choice of resistor value depends greatly on the respective application. Normally, values from 10 to 100 kΩ are suitable. So you don't necessarily have to use the often recommended 10 kΩ resistors.
A higher resistance (e.g. 100 kΩ) is particularly useful when the Arduino or the system is powered by a battery. Here, low power consumption plays a central role, and higher resistance values help to conserve battery power. Thanks to Ohm's law, we know that the smaller the resistance, the higher the current flow – which increases power consumption and shortens battery life.
However, a lower resistance (e.g. 10 kΩ or less) has the advantage of being more sensitive to interference pulses. In “harsher environments” where there is a lot of electromagnetic interference, lower resistance values can be useful because they compensate for more interference.
To sum up:
Higher resistances (e.g. 100 kΩ): low power consumption, ideal for battery-powered projects, but more susceptible to interference.
Lower resistances (e.g. 10 kΩ): better interference resistance, higher power consumption.
The following graphic is intended as a guideline and shows which resistance values are suitable for different applications. The general rule is:
1–10 kΩ for general applications.
10–100 kΩ for battery-powered systems.
gen. battery
short circuit contact open
Pull-up or pull-down resistor, which one do I use now?
It doesn't really matter if we use a pull-up or pull-down resistor. If you have the choice to switch to GND or VCC, then it is a question of noise immunity. The better choice here is the pull-up resistor, because it is also more battery friendly.
Note
For DIY projects, the resistance value is usually not critical. At 10 kΩ, you are in a solid middle range that is suitable for most applications. Alternatively, you can also use the next larger resistor that you currently have to hand. The exact value is usually not crucial, so you don't have to worry about it too much.
Simple circuits