{"id":84078,"date":"2019-08-29T07:26:44","date_gmt":"2019-08-29T07:26:44","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=84078"},"modified":"2024-06-11T14:58:03","modified_gmt":"2024-06-11T14:58:03","slug":"esp32-hall-effect-sensor","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-hall-effect-sensor\/","title":{"rendered":"ESP32 Built-In Hall Effect Sensor with Arduino IDE and MicroPython"},"content":{"rendered":"\n<p>The ESP32 development board features a built-in hall effect sensor that detects changes in the magnetic field in its surroundings. This tutorial shows how to use the ESP32 hall effect sensor with Arduino IDE and MicroPython. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/ESP32-Hall-Effect-Sensor-featured.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Built-In Hall Effect Sensor with Arduino IDE and MicroPython\" class=\"wp-image-84087\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/ESP32-Hall-Effect-Sensor-featured.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/ESP32-Hall-Effect-Sensor-featured.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/ESP32-Hall-Effect-Sensor-featured.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/ESP32-Hall-Effect-Sensor-featured.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntcred\">The <a href=\"https:\/\/randomnerdtutorials.com\/esp32-migrating-version-2-to-3-arduino\/#hall-sensor\" title=\"\">hallRead() function has been discontinued<\/a>. Reading the hall sensor is no longer supported on the ESP32 version 3.X. You can still use this function, but you need to downgrade your ESP32 board add-on to version 2.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"320\" height=\"483\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-Boards-Add-on-install-ESP32-board-version-2.png?resize=320%2C483&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Boards Add-on install ESP32 board version 2\" class=\"wp-image-158533\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-Boards-Add-on-install-ESP32-board-version-2.png?w=320&amp;quality=100&amp;strip=all&amp;ssl=1 320w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-Boards-Add-on-install-ESP32-board-version-2.png?resize=199%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 199w\" sizes=\"(max-width: 320px) 100vw, 320px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Watch the Video Tutorial<\/h2>\n\n\n\n<p>You can watch the video tutorial or keep reading this page for the written instructions to learn about the ESP32 hall effect sensor. This video covers how to use the hall effect sensor with Arduino IDE. Scroll down to learn how to use it with MicroPython firmware.<\/p>\n\n\n<p style=\"text-align:center\"><iframe width=\"720\" height=\"405\" src=\"https:\/\/www.youtube.com\/embed\/vyDZdwk76Jk?rel=0\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The ESP32 Hall Effect Sensor<\/h2>\n\n\n\n<p>The <a href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"ESP32 board (opens in a new tab)\">ESP32 board<\/a> features a built-in hall effect sensor located behind the metal lid of the ESP32 chip as shown in the following figure.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"470\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/hall_effect_sensor_esp32.jpg?resize=750%2C470&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"The ESP32 Hall Effect Sensor\" class=\"wp-image-84088\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/hall_effect_sensor_esp32.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/hall_effect_sensor_esp32.jpg?resize=300%2C188&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>A hall effect sensor can detect variations in the magnetic field in its surroundings. The greater the magnetic field, the greater the sensor&#8217;s output voltage.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"263\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/hall-effect-sensor-magnet-img.jpg?resize=750%2C263&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Hall Effect Sensor Effect\" class=\"wp-image-84089\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/hall-effect-sensor-magnet-img.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/hall-effect-sensor-magnet-img.jpg?resize=300%2C105&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>The hall effect sensor can be combined with a threshold detection to act as a switch, for example. Additionally, hall effect sensors are mainly used to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Detect proximity;<\/li>\n\n\n\n<li>Calculate positioning;<\/li>\n\n\n\n<li>Count the number of revolutions of a wheel;<\/li>\n\n\n\n<li>Detect a door closing;<\/li>\n\n\n\n<li>And much more.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Read Hall Effect Sensor &#8211; Arduino IDE<\/h2>\n\n\n\n<p>Reading the hall effect sensor measurements with the ESP32 using the Arduino IDE is as simple as using the <span class=\"rnthl rntliteral\">hallRead()<\/span> function.<\/p>\n\n\n\n<p>Use the following code.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/\/ Simple sketch to access the internal hall effect detector on the esp32.\n\/\/ values can be quite low. \n\/\/ Brian Degger \/ @sctv  \n\nint val = 0;\n\nvoid setup() {\n  Serial.begin(9600);\n}\n\n\/\/ put your main code here, to run repeatedly\nvoid loop() {\n  \/\/ read hall effect sensor value\n  val = hallRead();\n  \/\/ print the results to the serial monitor\n  Serial.println(val); \n  delay(1000);\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/ESP32-Course\/raw\/master\/code\/Hall_Effect_Sensor\/Hall_Effect_Sensor.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>This example simply reads the hall sensor measurements and displays them on the Serial monitor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>val = hallRead();\nSerial.println(val);<\/code><\/pre>\n\n\n\n<p>Add a delay of one second in the loop, so that you can actually read the values.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>delay(1000);<\/code><\/pre>\n\n\n\n<p>Upload the code to your ESP32 board:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"34\" height=\"29\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/12\/arduino-ide-upload-button.png?resize=34%2C29&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Compile and Upload Code to ESP32 using Arduino IDE\" class=\"wp-image-65439\"\/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>Once the upload is finished, open the Serial Monitor at a baud rate of 9600. Approximate a magnet to the ESP32 hall sensor and see the values increasing&#8230;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"461\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/positive-readings-hall-effect-sensor.jpg?resize=750%2C461&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Hall Effect Sensor Readings Arduino IDE serial monitor\" class=\"wp-image-84090\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/positive-readings-hall-effect-sensor.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/positive-readings-hall-effect-sensor.jpg?resize=300%2C184&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Or decreasing depending on the magnet pole that is facing the sensor:<\/p>\n\n\n\n<p>The closer the magnet is to the sensor, the greater the absolute values are.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"439\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/negative-readings-hall-effect-sensor.jpg?resize=750%2C439&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Hall Effect Sensor Readings Arduino IDE serial monitor\" class=\"wp-image-84091\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/negative-readings-hall-effect-sensor.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/negative-readings-hall-effect-sensor.jpg?resize=300%2C176&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Read Hall Effect Sensor &#8211; MicroPython<\/h2>\n\n\n\n<p>To read the ESP32 hall effect sensor using MicroPython, you just need to use the following snippet of code:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>import esp32\nesp32.hall_sensor()<\/code><\/pre>\n\n\n\n<p>You need to import the <span class=\"rnthl rntliteral\">esp32<\/span> module. Then, use the <span class=\"rnthl rntliteral\">hall_sensor()<\/span> method.<\/p>\n\n\n\n<p>If you want to print the readings on the shell, you just need to use the <span class=\"rnthl rntliteral\">print()<\/span> function:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>print(esp32.hall_sensor())<\/code><\/pre>\n\n\n\n<p>If you&#8217;re just getting started with MicroPython, you can read the following tutorial:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-micropython-esp32-esp8266\/\">Getting Started with MicroPython on ESP32<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>Throughout this tutorial you&#8217;ve learned that:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The ESP32 features a built-in hall effect sensor<\/li>\n\n\n\n<li>The hall effect sensor can detect magnetic field changes in its surroundings<\/li>\n\n\n\n<li>The measurements from the sensor can increase or become negative depending on the magnet pole facing the sensor.<\/li>\n<\/ul>\n\n\n\n<p>We hope you&#8217;ve found this tutorial useful. For more projects with the ESP32 you can check our project&#8217;s compilation: <a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\">20+ ESP32 Projects and Tutorials<\/a>.<\/p>\n\n\n\n<p class=\"rntbox rntclgray\">This tutorial is a preview of the \u201c<a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE<\/a>\u201d course. If you like this project, make sure you take a look at the&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">ESP32 course page<\/a>&nbsp;where we cover this and a lot more topics with the ESP32.<\/p>\n\n\n\n<p><em>Updated August 29, 2019<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The ESP32 development board features a built-in hall effect sensor that detects changes in the magnetic field in its surroundings. This tutorial shows how to use the ESP32 hall effect &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32 Built-In Hall Effect Sensor with Arduino IDE and MicroPython\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-hall-effect-sensor\/#more-84078\" aria-label=\"Read more about ESP32 Built-In Hall Effect Sensor with Arduino IDE and MicroPython\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":84087,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[281,276,277,299,230,264],"tags":[],"class_list":["post-84078","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32-project","category-esp32","category-esp32-arduino-ide","category-0-esp32","category-guide","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/ESP32-Hall-Effect-Sensor-featured.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/84078","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=84078"}],"version-history":[{"count":1,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/84078\/revisions"}],"predecessor-version":[{"id":158783,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/84078\/revisions\/158783"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/84087"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=84078"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=84078"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=84078"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}