Basic Course about ARDUINO - Lesson 8
TOPICS INDEX
- Warnings
- Copyright Notice
- Magnetism and the Magnetic Field
- Curiosity: The Hall Principle
- Project 27 – Detection of the presence of a Magnetic Field – KY003
- Analysis of the Sketch: Project 27 – Use of the LED on board the Arduino
- Project 28 – Analog Detection of a Magnetic Field – KY035
- Curiosity: The Metal Detector
- Project 29 – Magnetic Detector with Linear Magnetic Hall Sensor KY024
- Sketch analysis: Project 29 – Magnetic Detector

Warnings
Regarding the safety aspects, since the projects are based on a very low voltage power supply supplied by the USB port of the PC or by backup batteries or power supplies with a maximum of 9V output, there are no particular risks of an electrical nature. It is however necessary to specify that any short circuits caused during the exercise could cause damage to the PC, to the furnishings and in extreme cases even to burns, for this reason every time a circuit is assembled, or modifications are made on it, it will be necessary to do it in power failure and at the end of the exercise it will be necessary to disconnect the circuit by removing both the USB cable for connection to the PC and any batteries from the appropriate compartments or external power connectors. Furthermore, again for safety reasons, it is strongly recommended to carry out the projects on insulating and heat resistant mats that can be purchased in any electronics store or even on specialized websites.
At the end of the exercises it is advisable to wash your hands, as the electronic components could have processing residues that could cause damage if swallowed or if in contact with eyes, mouth, skin, etc. Although the individual projects have been tested and safe, those who decide to follow what is reported in this document assume full responsibility for what could happen in the execution of the exercises provided for in it. For younger children and / or for their first experiences in the field of electronics, it is advisable to perform the exercises with the help and in the presence of an adult.
Copyright Notice
All trademarks shown belong to their legitimate owners; third party trademarks, product names, trade names, corporate names and companies mentioned may be trademarks of their respective owners or registered trademarks of other companies and have been used for explanatory purposes only and for the benefit of the owner, without any purpose of infringement. of the Copyright rights in force. What is reported in this document is the property of Roberto Francavilla, Italian and European copyright laws are applicable to it – any texts taken from other sources are also protected by copyright and property of their respective owners . All information and contents (texts, graphics and images, etc.) shown are, to the best of my knowledge, in the public domain. If, unintentionally, material subject to copyright or in violation of the law has been published, please notify us by email to info@bemaker.org and I will promptly remove it.
Roberto Francavilla
Magnetism and the Magnetic Field

This Lesson we dedicate it to the study of the Magnetic Field. This is an important and challenging topic, so I realize that it could be a difficult topic for the younger ones. I’ll try to explain it as simply as possible, but if I don’t succeed … don’t worry! Go ahead anyway.
First of all, let’s define what magnetism is: that is, the property that some bodies possess in attracting objects of a ferrous nature to themselves.

Bodies with this property are called magnets. Magnetite, an iron ore, is an example of a natural magnet. A magnet attracts ferrous bodies at its ends, which are called magnetic poles. These two poles are not the same and are divided into north pole (N) and south pole (S). Poles of the same type (N-N and S-S) repel each other and poles of opposite types (N-S and S-N) attract each other.

The poles of a magnet can never be separated, if you split a magnet it will restore the poles as they were in the original dimension.
The magnetic field, in physics, is defined as follows: the magnetic field is a vector field, capable of passing through bodies, generated in space by the motion of an electric charge or by a time-varying electric field. Let us try to decipher what physics says in simpler terms. So, we have materials that are normally magnetized and materials that can become magnets if passed through by a current.
First of all, let’s see why physics speaks of a “vector field”. The magnetic field is defined “vector” because its lines of force, ie the areas of influence, have a very precise direction. In the case of a magnet made thanks to the circulation of current, the magnetic field produced depends on the direction in which the electric current circulates. In the case of permanent magnets, however, the lines of force go from the North Pole to the South Pole of the magnet. In the electronic and electrotechnical field, the magnetic field generated by an electric current is the phenomenon most used in various purposes.

The area of influence of the magnetic field is the space in which we can observe the phenomenon of magnetism. Obviously the closer we are to what the magnetic field produces, whether it be a material passed through by the current or a permanent magnet, the greater the effect of this field.
It is also possible to do a very simple experiment to visualize the lines of force of the magnetic field produced by a permanent magnet. Just take a smooth white cardboard and cover it (dusting it) with iron filings (iron filings are the waste from iron working, they are very small iron needles, they look like many specks). When a magnet is placed under the cardboard; giving light shocks to the cardboard, the magnetized iron filings are arranged along the lines of force of the magnetic field, composing the design of the projection on the plane of the same lines of force.

The Earth also has its own Magnetic Field and several physicists and scholars think it is due to the large amount of iron and nickel present in the core (other theories instead say that the magnetic field generated by the Earth is of an electrical nature). However, surely you have seen a compass whose needle aligns itself with the lines of force of the Earth’s magnetic field indicating its magnetic North Pole which is slightly different from the geographic North Pole.

As I said earlier, in electrical engineering and electronics the phenomenon of magnetism is widely used, in fact in this Lesson we will see some projects that use this phenomenon to regulate certain events. The principle used is the so-called “Hall effect”.
Curiosity: The Hall Principle
To measure the Magnetic Field is used the so-called Principle of Edwin Hall, an American physicist, who noted that if we make a metal (or semiconductor) sheet cross a direct current between the terminals “+” and “-” and this sheet we immerse it in a magnetic field, the flow of the current (or rather of the electrons) is deflected and therefore an accumulation of charges is created towards the outer edges of the foil, so much so that a difference is created between the terminals “a” and “b” potential (Hall voltage) measurable. This accumulation is all the greater, the greater the magnetic field and consequently the voltage across terminals «a» and «b» will be higher. The signs of the Hall voltage depend on the magnetic polarization and the voltage applied to the foil.

This principle has several applications, especially in mechatronics, in fact by exploiting the principle that the magnetic field is influenced by the type of material in which it is applied, we can, thanks to the Hall probe, measure its variations. Thanks to these variations we can check whether a ferrous product (for example from a rolling mill) has been made correctly (no field variation), or count the number of revolutions of a crankshaft, etc.

For example, the figure above shows a measuring system, without contact with the moving part, to measure the speed of a rotating disk or the number of revolutions of a gear, even when these moving parts are inside casings in plastic or metal
Another example of application of the Hall probe is the one shown in the figure below:

Current measurement system without contact with the voltage part. This method exploits the principle that an electric current produces a magnetic field and therefore thanks to the measurement of the magnetic field we can measure the electric current without creating a contact with the live part.
Project 27 - Detection of the presence of a Magnetic Field - KY003

For this project we need:
The Hall effect sensor module – KY003 is made up of a component sensitive to the effects of the magnetic field (whose name is unspeakable! A series of nonsense numbers and letters…) and with the characteristics indicated by the manufacturer, it works well even at high temperatures.
On the module there is a 680 Ohm resistor and an LED that is activated in the event of a magnetic field.

The signal that will go to an Arduino digital PIN is available at the PIN indicated with S, the Arduino GND must be connected to the PIN indicated with “-” and at the PIN with “+” or nothing is indicated, or even indicated with Vcc, it is necessary to supply the 5V power supply from Arduino.
The wiring diagram for the connection is:

First let’s take our Arduino, some Dupont male-male cables and the sensor, which in English is called the Hall Magnetic Sensor.
We connect everything as shown in the assembly diagram below:

The signal PIN “S” is connected to the Arduino digital PIN 3, for the rest just connect the power supply to 5V and the ground to the Arduino GND.
Let’s open the IDE and after opening a “New” window, copy the sketch below:
The sketch is loaded onto Arduino and at this point, by moving a magnet near the sensor, it is possible to observe that the LED at PIN13 turns on and off, obviously depending on the distance between the magnet and the sensor. When the magnetic field has such an intensity as to be detected by the sensor, the LED of PIN 13 on the Arduino lights up, the LED on the module also lights up (but not all modules show this LED).
Video-Project 27 - Detection of the presence of a Magnetic Field - KY003
Analysis of the Sketch: Project 27 - Use of the LED on board the Arduino.
Sometimes to avoid mounting an LED and a resistor to check the functionality of a particular circuit or module, it is possible to use an LED that Arduino makes available to users. This LED is connected to digital PIN 13 and to be used it must be configured as if there was an external LED connected to PIN 13.
Therefore it is necessary to define which PIN the LED is connected to:
int Led = 13; // LED present on Arduino
In the void setup, it must be specified that it is an output PIN:
pinMode(Led, OUTPUT); // we set the LED as OUTPUT
And finally it must be switched on or off with any LED:
digitalWrite(Led, HIGH);

Most of the Arduino clones also carry this LED.

Project 28 - Analog Detection of a Magnetic Field - KY035

With the KY-035 Analog Hall magnetic sensor module it is possible to continuously detect the magnetic field as it is a sensor module that has an analog output.
For this project we need:
Module KY035 has the same pinout as KY003, with the only difference that the PIN indicated with “S” is the analog signal and not just the ON / OFF of the KY003.
The wiring diagram for the connection is as follows.

Let’s take our Arduino, some Dupont male-male cables and the sensor.
For this project we assemble the circuit as the previous one with the only difference that the signal output is brought to an analog PIN, in particular, in our case, to PIN number A5.

Let’s open the IDE and write the sketch below.
Also in this case, once the sketch has been loaded on Arduino, by moving a magnet near the sensor it is possible to observe the variation in intensity of the magnetic field and thanks to the “Serial Plotter” functionality of the IDE, we can also have the tracing of the value of the same magnetic field that it has as a function of distance.
To activate the Serial Plotter, go to “Tools” and then click on “Serial Plotter”.

Video-Project 28 - Analog Detection of a Magnetic Field - KY035
Curiosity: The Metal Detector

The operating principle of a Metal Detector is very simple; we said that a current produces a magnetic field and if this current varies over time, the magnetic field produced will also vary over time.
In 1834 the Russian physicist Emil Lenz discovered that by subjecting a conductive material (such as metals, for example) to a magnetic field that varies over time, an electric current is produced on it which in turn produces a magnetic field that goes against each other. to the one that generates the current (the so-called Lenz’s Law). Therefore, a metal detector, thanks to a coil crossed by a variable current, produces a variable magnetic field, consequently on the metal that is perhaps in the ground, a magnetic counter-field is produced and therefore it is sufficient to detect this magnetic counter-field that is find the metal we are looking for.
The more the magnetic field is variable over time, the greater the intensity of the magnetic field induced in the metal we are looking for and therefore the greater the depth of investigation into the ground that we can carry out. In this way metal objects are found, the reclamation of war devices is carried out,…. there are TREASURES!
With the knowledge gained in this lesson we are able to build a search … magnets!
Project 29 - Magnetic Detector with Linear Magnetic Hall Sensor KY024

For this project we will use neither the KY003 sensor module nor the KY035, but the KY024 sensor module, i.e. the Linear Magnetic Hall Sensor. This module differs from the first two because it has both functions, i.e. it has an analog and a digital output:

The PIN with indicated A0 is the analogue output, the one with the initials D0, on the other hand, is the digital output, while the PIN with the G stands for Ground and the one with the “+” stands for the power supply (at 5 V ).
The module also has a trimmer that can be adjusted to adjust the sensitivity of the sensor and therefore the threshold value for switching on the LED. There are two LEDs on the module, one is on when the module is powered and the other when there is a magnetic field.
For this project we need:
The wiring diagram for the connection is:

So let’s proceed with the connections as shown in the diagram below:

Let’s open the IDE and write the sketch below.
The following Arduino sketch will read the values from the sensor interfaces, both digital and analog. The digital interface will turn on the LED at PIN 13 of Arduino when a magnetic field is detected and emit a buzzer sound.
The analog interface starts from an initial value determined by the input voltage (adjusted by the trimmer), this value will increase or decrease according to the intensity and polarity of the magnetic field. For a better graphical representation, the initial value is set to zero.
Try moving the magnetic detector near a magnet to see its effect and adjust it accordingly using the trimmer.
We always use the Serial Plotter to have the graphical representation of the values.
Video-Project 29 - Magnetic Detector with Linear Magnetic Hall Sensor KY024
Sketch analysis: Project 29 - Magnetic Detector
The only observation, worthy of the name, to the sketch concerns the analogRead () instruction which will automatically set the analog PIN A0 as INPUT, so the instruction pinMode(analogPIN, INPUT) can also be omitted, but for educational reasons l ‘I have however reported this instruction, in order to avoid generating confusion.
Furthermore, as it is possible to see from the decision logic set with the if and else, if the magnetic field value is low (less than the threshold level adjusted with the trimmer), I forcefully set the reading variable of the analog signal to 0 and keep them off Led and Buzzer.
Obviously this logic can be modified at will.
If you found the lesson interesting, make a donation you will help me realize many others.
