{"id":4527,"date":"2021-09-15T16:16:10","date_gmt":"2021-09-15T14:16:10","guid":{"rendered":"https:\/\/home.et.utwente.nl\/slootenvanf\/?p=4527"},"modified":"2025-12-10T16:06:53","modified_gmt":"2025-12-10T15:06:53","slug":"distance-sensor-with-alarm","status":"publish","type":"post","link":"https:\/\/home.et.utwente.nl\/slootenvanf\/2021\/09\/15\/distance-sensor-with-alarm\/","title":{"rendered":"Tutorial: build a distance sensor with an alarm"},"content":{"rendered":"\n<p>Introduction to Arduino and programming .&nbsp;There is also a <a href=\"https:\/\/www.youtube.com\/watch?v=orzEMRrpHaM&amp;ab_channel=F.vanSlooten\" target=\"_blank\" rel=\"noreferrer noopener\">&#8220;quick walk-through video&#8221;<\/a> of this tutorial (but it is rather old).<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If you are new to using an Arduino, <a href=\"https:\/\/www.youtube.com\/watch?v=JnJIKX5J0Cc&amp;list=PLwWF-ICTWmB7-b9bsE3UcQzz-7ipI5tbR&amp;ab_channel=SunFounderMakerEducation\">maybe watch some of the intro video&#8217;s first<\/a>.<\/li>\n\n\n\n<li>For more tutorials on distance sensors, including various applications, <a href=\"https:\/\/home.et.utwente.nl\/slootenvanf\/tag\/distance\/\" target=\"_blank\" rel=\"noreferrer noopener\">look here<\/a>. <\/li>\n\n\n\n<li><a href=\"https:\/\/www.tinytronics.nl\/en\/sensors\/distance\/ultrasonic-sensor-hc-sr04\" target=\"_blank\" rel=\"noreferrer noopener\">You can buy this sensor here<\/a>. Or <a href=\"https:\/\/www.tinytronics.nl\/en\/sensors\/distance\/ultrasonic-distance-sensor-rcwl-1604\" target=\"_blank\" rel=\"noreferrer noopener\">opt for a more modern one<\/a>, for better compatibility with 3.3V logic levels (eg. Raspberry Pi Pico)<\/li>\n\n\n\n<li><a href=\"https:\/\/home.et.utwente.nl\/slootenvanf\/2024\/02\/23\/distance-sensor-raspberry-pi-pico\/\" target=\"_blank\" rel=\"noreferrer noopener\">Look here<\/a> if you would like to use the Raspberry Pi Pico with this sensor (code in Python).<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1365\" height=\"768\" src=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2020\/04\/distance_sensor.jpg\" alt=\"\" class=\"wp-image-2824\" srcset=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2020\/04\/distance_sensor.jpg 1365w, https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2020\/04\/distance_sensor-300x169.jpg 300w, https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2020\/04\/distance_sensor-1024x576.jpg 1024w, https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2020\/04\/distance_sensor-768x432.jpg 768w, https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2020\/04\/distance_sensor-100x56.jpg 100w\" sizes=\"auto, (max-width: 1365px) 100vw, 1365px\" \/><\/figure>\n\n\n\n<p>We will build a distance meter in this tutorial. It uses an ultrasonic distance sensor to sense the distance to an object nearby.<\/p>\n\n\n\n<p>It consist of the following steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Setup the Arduino and do a quick test<\/li>\n\n\n\n<li>Connect and test the OLED display<\/li>\n\n\n\n<li>Connect and test the Ultrasonic sensor<\/li>\n\n\n\n<li>Add an alarm depending on the distance<\/li>\n\n\n\n<li>Add comments to the code<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">1. Setup the Arduino and do a quick test<\/h2>\n\n\n\n<p>First, <a href=\"https:\/\/www.arduino.cc\/en\/software#download\" target=\"_blank\" rel=\"noreferrer noopener\">download and install the Arduino IDE<\/a>. And start that.<\/p>\n\n\n\n<p>Next, open the &#8220;Blink&#8221; example via the File menu: <em>File &gt; Examples &gt; 01. Basics &gt; Blink<\/em>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2021\/09\/image.png\"><img loading=\"lazy\" decoding=\"async\" width=\"867\" height=\"560\" src=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2021\/09\/image.png\" alt=\"\" class=\"wp-image-6640\" srcset=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2021\/09\/image.png 867w, https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2021\/09\/image-300x194.png 300w, https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2021\/09\/image-768x496.png 768w\" sizes=\"auto, (max-width: 867px) 100vw, 867px\" \/><\/a><\/figure>\n\n\n\n<p>Next, hook up the Arduino board: <strong>connect it to your computer with a USB cable<\/strong>. Check if at least one of the LEDs on the board is on (if not, it might be defective). Click the drop-down in the menu bar to specify your type of Arduino:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2021\/09\/image-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"762\" height=\"353\" src=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2021\/09\/image-1.png\" alt=\"\" class=\"wp-image-6641\" srcset=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2021\/09\/image-1.png 762w, https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2021\/09\/image-1-300x139.png 300w\" sizes=\"auto, (max-width: 762px) 100vw, 762px\" \/><\/a><\/figure>\n\n\n\n<p>Select your Arduino and Port:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2021\/09\/image-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"553\" height=\"295\" src=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2021\/09\/image-2.png\" alt=\"\" class=\"wp-image-6642\" srcset=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2021\/09\/image-2.png 553w, https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2021\/09\/image-2-300x160.png 300w\" sizes=\"auto, (max-width: 553px) 100vw, 553px\" \/><\/a><\/figure>\n\n\n\n<p>Do not worry if it&#8217;s not listed, in that case, select &#8220;Select other board and port&#8230;&#8221; and type &#8220;Nano&#8221; in the search field, then select &#8220;Arduino Nano&#8221; and the COM port:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2021\/09\/image-3.png\"><img loading=\"lazy\" decoding=\"async\" width=\"688\" height=\"304\" src=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2021\/09\/image-3.png\" alt=\"\" class=\"wp-image-6643\" srcset=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2021\/09\/image-3.png 688w, https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2021\/09\/image-3-300x133.png 300w\" sizes=\"auto, (max-width: 688px) 100vw, 688px\" \/><\/a><\/figure>\n\n\n\n<p>Next, select the Processor from the Tools menu:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2021\/09\/image-4.png\"><img loading=\"lazy\" decoding=\"async\" width=\"757\" height=\"564\" src=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2021\/09\/image-4.png\" alt=\"\" class=\"wp-image-6644\" srcset=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2021\/09\/image-4.png 757w, https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2021\/09\/image-4-300x224.png 300w\" sizes=\"auto, (max-width: 757px) 100vw, 757px\" \/><\/a><\/figure>\n\n\n\n<p>Make sure \u201cATmega328P (Old Bootloader)\u201d is selected for the regular style of Arduino Nano. However, if you use the BLE Nano, you should use \u201cATmega328P\u201d.<\/p>\n\n\n\n<p>Now run the example (click the arrow icon):<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2021\/09\/image-5.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1030\" height=\"580\" src=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2021\/09\/image-5-1030x580.png\" alt=\"\" class=\"wp-image-6647\" srcset=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2021\/09\/image-5-1030x580.png 1030w, https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2021\/09\/image-5-300x169.png 300w, https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2021\/09\/image-5-768x433.png 768w, https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2021\/09\/image-5.png 1266w\" sizes=\"auto, (max-width: 1030px) 100vw, 1030px\" \/><\/a><\/figure>\n\n\n\n<p>After Compiling and Uploading finishes, verify that the LED on the board is blinking. This confirms that your setup of the Arduino IDE works and that the Arduino is working.<\/p>\n\n\n\n<p>If it fails, retry with a different COM port and double-check the other settings.<\/p>\n\n\n\n<p>Get help if it still fails. Or check <strong><a href=\"https:\/\/home.et.utwente.nl\/slootenvanf\/2021\/06\/09\/error-uploading-sketch-in-arduino-ide-and-other-upload-issues\/\" target=\"_blank\" rel=\"noreferrer noopener\">these troubleshooting steps<\/a><\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Connect and test the OLED display<\/h2>\n\n\n\n<p>Before you start wiring, always disconnect power (from the board). So disconnect the USB cable.<\/p>\n\n\n\n<p class=\"has-yellow-background-color has-background\">Before you start connecting wires, make sure to read this entire paragraph! Also make sure the Arduino is not connected (to your computer), as wiring a circuit with live power is a recipe for disaster!<\/p>\n\n\n\n<p>The&nbsp;&nbsp;<a href=\"https:\/\/www.tinytronics.nl\/shop\/en\/display\/oled\/0.96-inch-oled-display-128*64-pixels-white-i2c\" target=\"_blank\" rel=\"noreferrer noopener\">0.96 inch OLED Display (128*64 pixels)<\/a> is an <a href=\"https:\/\/en.wikipedia.org\/wiki\/I%C2%B2C\" target=\"_blank\" rel=\"noreferrer noopener\">i2c<\/a> module, which makes it easy to connect as it has only 4 wires. It is assumed you have some basic understanding of using electronics and wiring a breadboard. If not, <a href=\"https:\/\/www.youtube.com\/watch?v=Cz4z393n3Xk&amp;ab_channel=F.vanSlooten\" target=\"_blank\" rel=\"noreferrer noopener\">watch this intro first.<\/a><\/p>\n\n\n\n<p><strong>Please take great care:<\/strong>&nbsp;<strong>pins on your display might be in a different order!<\/strong>&nbsp;the one below has GND-VCC-SCL-SDA, but yours might be different!<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"705\" height=\"621\" src=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/04\/0.96-oled-display-I2C-1-1000x1000.jpg\" alt=\"\" class=\"wp-image-1687\" style=\"width:269px;height:auto\" srcset=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/04\/0.96-oled-display-I2C-1-1000x1000.jpg 705w, https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/04\/0.96-oled-display-I2C-1-1000x1000-300x264.jpg 300w\" sizes=\"auto, (max-width: 705px) 100vw, 705px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"644\" height=\"487\" src=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/04\/0.96-oled-display-I2C-2-1000x1000.jpg\" alt=\"\" class=\"wp-image-1688\" style=\"width:273px;height:auto\" srcset=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/04\/0.96-oled-display-I2C-2-1000x1000.jpg 644w, https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/04\/0.96-oled-display-I2C-2-1000x1000-300x227.jpg 300w\" sizes=\"auto, (max-width: 644px) 100vw, 644px\" \/><\/figure>\n\n\n\n<p>To use this display, you can connect it to power (GND and VCC, it can be either 3.3 or 5V) and connect the SDA and SCL pins to the SDA (A4) and SCL (A5) pins of the Arduino. These are hardware i2c pins:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Display:    Arduino:\nGND         GND\nVCC         5V\nSCL         SCL (A5)\nSDA         SDA (A4)\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image has-lightbox\"><img loading=\"lazy\" decoding=\"async\" width=\"1344\" height=\"680\" src=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/11\/Arduino_Nano_Pinout_SDA_SCL.png\" alt=\"\" class=\"wp-image-2155\" srcset=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/11\/Arduino_Nano_Pinout_SDA_SCL.png 1344w, https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/11\/Arduino_Nano_Pinout_SDA_SCL-300x152.png 300w, https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/11\/Arduino_Nano_Pinout_SDA_SCL-1024x518.png 1024w, https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/11\/Arduino_Nano_Pinout_SDA_SCL-768x389.png 768w\" sizes=\"auto, (max-width: 1344px) 100vw, 1344px\" \/><\/figure>\n\n\n\n<p>You can mount this display directly onto a breadboard and connect the wires (double check the pins with the table above!). Check the second picture below to see the wiring properly:<\/p>\n\n\n\n<figure class=\"wp-block-image has-lightbox\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"563\" src=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/11\/oled_display_mounted.jpg\" alt=\"Photo of OLED display connected  to the Arduino.\" class=\"wp-image-2161\" srcset=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/11\/oled_display_mounted.jpg 1000w, https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/11\/oled_display_mounted-300x169.jpg 300w, https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/11\/oled_display_mounted-768x432.jpg 768w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full has-lightbox is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"943\" height=\"518\" src=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/11\/oled_wiring.png\" alt=\"Wiring schematics of OLED display connected  to the Arduino.\" class=\"wp-image-2203\" style=\"aspect-ratio:16\/9;object-fit:cover\"\/><\/figure>\n\n\n\n<p>Open the image in a new tab if it does not appear properly (<em>right-click &gt; open in new tab<\/em>).<\/p>\n\n\n\n<p><a href=\"https:\/\/wokwi.com\/projects\/394137411932390401\" target=\"_blank\" rel=\"noreferrer noopener\">View a simulation on Wokwi of this part of the project.<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Download example sketch<\/h4>\n\n\n\n<p>To test this display, you can use this example sketch:&nbsp;<code><a href=\"https:\/\/home.et.utwente.nl\/slootenvanf\/div\/arduino\/oled_display_distance.ino\" target=\"_blank\" rel=\"noreferrer noopener\">oled_display_distance.ino<\/a><\/code>. Open it in the Arduino IDE. You might get a question that the file needs to be inside a sketch folder. That&#8217;s fine, so click&nbsp;<em>Ok<\/em>. Please be aware that the sketch will then be saved in your Download folder. If you do not want this, Select&nbsp;<em>File &gt; Save As<\/em> to save it to another folder, for instance the&nbsp;<strong>Documents\\Arduino<\/strong>&nbsp;folder.<\/p>\n\n\n\n<p>The sketch uses the U8g2 library, which you must install before you use this sketch: In the Arduino IDE, select&nbsp;<em>Tools &gt; Manage Libraries<\/em>, then search for \u201cU8g2\u201d and install the library (the one \u201cby oliver\u201d).<\/p>\n\n\n\n<p>Next, connect the Arduino to your computer with a USB cable.<\/p>\n\n\n\n<p>Before you upload the example sketch to the Arduino, configure (or check) the Arduino IDE to use the correct setting for the type of Arduino you use: under the Tools menu, check the board settings:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized has-lightbox\"><a href=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/11\/arduino_board_settings.png\"><img loading=\"lazy\" decoding=\"async\" width=\"772\" height=\"517\" src=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/11\/arduino_board_settings.png\" alt=\"\" class=\"wp-image-2182\" style=\"object-fit:contain;width:772px;height:517px\"\/><\/a><\/figure>\n\n\n\n<p>The Port can be any active Port (eg. COM1-12, or on Mac OS something like \u201c\/dev\/tty.usbmodem\u2026\u201d or \u201c\/dev\/tty.usbserial-\u2026\u201d ). There should be one or more ports you can select here. If there is one port, select that. If there are multiple, remove the usb-cable from your computer and then check the list of ports again. The one that disappeared is the port for the Arduino. Reconnect the usb-cable and select that port. If nothing happens, or none of the ports work, you might have to <a href=\"https:\/\/learn.sparkfun.com\/tutorials\/how-to-install-ch340-drivers\/all#drivers-if-you-need-them\" target=\"_blank\" rel=\"noreferrer noopener\">install the CH340 driver<\/a> before you can use the port.<strong> <a href=\"https:\/\/home.et.utwente.nl\/slootenvanf\/2021\/06\/09\/error-uploading-sketch-in-arduino-ide-and-other-upload-issues\/\" target=\"_blank\" rel=\"noreferrer noopener\">Other troubleshooting options are here.<\/a><\/strong><\/p>\n\n\n\n<p>Make sure \u201cATmega328P (Old Bootloader)\u201d is selected at \u201cProcessor\u201d. (However, if you use the BLE Nano, you should use the regular \u201cATmega328P\u201d).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Upload the example sketch<\/h4>\n\n\n\n<p>Now upload the sketch (press the Upload button&nbsp;&nbsp;<img loading=\"lazy\" decoding=\"async\" width=\"26\" height=\"25\" src=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/11\/upload.png\" alt=\"Upload button\">&nbsp;&nbsp;) to transfer the sketch to the Arduino and run it. The display should show some text messages and the value of the variable&nbsp;<code>distance<\/code>. If things go wrong,<strong> <a href=\"https:\/\/home.et.utwente.nl\/slootenvanf\/2021\/06\/09\/error-uploading-sketch-in-arduino-ide-and-other-upload-issues\/\" target=\"_blank\" rel=\"noreferrer noopener\">checkout the troubleshooting options.<\/a><\/strong><\/p>\n\n\n\n<p>To display values of variables with this display in a simple manner, you might need code like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>display.drawString(0,0,\"Distance:\");\ndisplay.setCursor(0, 1); \/\/ go to next line\ndisplay.print(distance);<\/code><\/pre>\n\n\n\n<p>This shows the message \u201cDisplay:\u201d on line 0, then displays the value of variable&nbsp;<em>display&nbsp;<\/em>on the next line. The example sketch uses a slightly more advanced way to display the value in a larger font size. Can you find that in the function&nbsp;<em>loop()<\/em>?<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">More info about displays<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/olikraus\/u8g2\/wiki\/u8x8reference\" target=\"_blank\" rel=\"noreferrer noopener\">u8x8 library reference<\/a> (all methods of the display like the <em>drawString()<\/em> method are explained in detail here)<\/li>\n\n\n\n<li><a href=\"https:\/\/home.et.utwente.nl\/slootenvanf\/tag\/display\/\" target=\"_blank\" rel=\"noreferrer noopener\">More tutorials for displays on this site.<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">3. Connect and test the Ultrasonic sensor<\/h2>\n\n\n\n<p>This part of this tutorial introduces the HC-SR04 Ultrasonic sensor. Disconnect the USB cable before you continue.<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img decoding=\"async\" src=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/11\/hc-sr04.png\" alt=\"Picture of HR-SR04 ultrasonic sensor\" style=\"width:303px;height:auto\"\/><\/figure>\n\n\n\n<p>To use this sensor, you can connect it to power (GND and VCC,&nbsp;<strong>this must be 5V<\/strong>) and connect the Trig and Echo pins to pin 4 and 5 of the Arduino:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/11\/Arduino_Nano_Pinout_Trig_Echo.png\" alt=\"Arduino Nano Pinout diagram highlighting the Trig Echo Pins\"\/><\/figure>\n\n\n\n<p>You can mount the sensor directly onto a breadboard:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/11\/HC-SR04.jpg\" alt=\"Photo of breadboard with Arduino Nano, HC-SR04\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Download example sketch<\/h4>\n\n\n\n<p>To test this sensor, you can use this example sketch: <code><a href=\"https:\/\/home.et.utwente.nl\/slootenvanf\/div\/arduino\/ultrasonic_sensor_with_display.ino\" target=\"_blank\" rel=\"noreferrer noopener\">ultrasonic_sensor_with_display.ino<\/a><\/code>. Open the sketch in the Arduino IDE. The sketch uses the Newping library, which you must install before you use this sketch. In the Arduino IDE, select <em>Tools &gt; Manage Libraries<\/em>, then search for \u201cNewping\u201d and install the library.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Check the sketch<\/h4>\n\n\n\n<p>Before you upload the example sketch to the Arduino, verify the lines with&nbsp;<code>#define TRIGGER_PIN<\/code>&nbsp;&nbsp;and&nbsp;<code>#define ECHO_PIN<\/code>&nbsp;&nbsp;are set with the correct pins (4 and 5):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#define TRIGGER_PIN  4  \/\/ Arduino pin tied to trigger pin on the ultrasonic sensor.\n#define ECHO_PIN     5  \/\/ Arduino pin tied to echo pin on the ultrasonic sensor.<\/code><\/pre>\n\n\n\n<p>Next, hook up the Arduino to your computer with the USB cable. Configure the Arduino IDE to use the correct setting for the type of Arduino you use: under the&nbsp;<em>Tools<\/em>&nbsp;&nbsp;menu, check the board settings:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/11\/arduino_board_settings.png\" alt=\"This image has an empty alt attribute; its file name is arduino_board_settings.png\"\/><\/figure>\n\n\n\n<p>The Port can be any active Port (eg. COM1-12). After you have connected the USB cable and the Arduino has power, there should be one option you can select here. If you use the regular Arduino Nano,&nbsp;<strong>make sure you have selected the \u2018ATmega328P (Old Bootloader)\u2019 at \u201cProcessor\u201d.&nbsp;<\/strong>(for the BLE-Nano, select &#8220;ATmega328P&#8221;).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Upload the sketch<\/h4>\n\n\n\n<p>Run the sketch (press the Upload button&nbsp;&nbsp;<img decoding=\"async\" src=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/11\/upload.png\" alt=\"Upload button\">&nbsp;&nbsp;). The display should show the distance as read by the sensor. If you do not use a display, you can see the distance in the serial monitor, which you can start via the&nbsp;&nbsp;<img decoding=\"async\" src=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/11\/serial_monitor.png\" alt=\"Serial monitor\">&nbsp;&nbsp;icon in the top right corner.<\/p>\n\n\n\n<p>If things go wrong, <a href=\"https:\/\/home.et.utwente.nl\/slootenvanf\/2021\/06\/09\/error-uploading-sketch-in-arduino-ide-and-other-upload-issues\/\" target=\"_blank\" rel=\"noreferrer noopener\">checkout the troubleshooting options.<\/a> <\/p>\n\n\n\n<p>Now find the line of code which looks like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>unsigned int distance = sonar.convert_cm(sonar.ping_median(5));<\/code><\/pre>\n\n\n\n<p>This is the line which reads the distance value from the sensor and assigns it to the variable&nbsp;<code>distance<\/code>. It uses the method&nbsp;<code>ping_median(5)<\/code>, which will do 5 readings in a short time and takes the median of the five readings and returns that value. This will improve the accuracy of the readings.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">More information about distance sensors<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/bitbucket.org\/teckel12\/arduino-new-ping\/wiki\/Home\" target=\"_blank\" rel=\"noreferrer noopener\">More information about the library NewPing<\/a> (including descriptions of all methods).<\/li>\n\n\n\n<li><a href=\"https:\/\/home.et.utwente.nl\/slootenvanf\/tag\/distance\/\" target=\"_blank\" rel=\"noreferrer noopener\">Find more examples using distance sensors on this site.<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.makerguides.com\/hc-sr04-arduino-tutorial\/\" target=\"_blank\" rel=\"noreferrer noopener\">makerguides.com: How to use a HC-SR04 Ultrasonic Distance Sensor with Arduino<\/a><\/li>\n<\/ul>\n\n\n\n<p>If you are just building the sensor, you may skip the steps below.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Add an alarm depending on the distance<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"432\" height=\"444\" src=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/04\/buzzer.jpg\" alt=\"\" class=\"wp-image-1781\" srcset=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/04\/buzzer.jpg 432w, https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/04\/buzzer-292x300.jpg 292w\" sizes=\"auto, (max-width: 432px) 100vw, 432px\" \/><\/figure>\n\n\n\n<p>We are going to use a buzzer, which is a small device that can generate tones. There are different types, as explained in this video:<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/youtube.com\/watch?v=AQIayZgeqq4\n<\/div><\/figure>\n\n\n\n<p>Function wise they all work the same: a Pulse-Width-Modulated (<a href=\"https:\/\/www.arduino.cc\/en\/tutorial\/PWM\" target=\"_blank\" rel=\"noreferrer noopener\">PWM<\/a>) signal is sent to it to generate the sound. Usually, the + pin of the buzzer is indicated by a + sign on the housing or on the bottom. If it is not, the longest leg is the +. If there is no visible indication of a + pin, just connect it. You cannot break it by connecting it wrong, just give it a try. If it does not work, reverse the connection and try again.<\/p>\n\n\n\n<p>Connect the + pin of the buzzer to pin 9 of the Arduino (use <a href=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/11\/Arduino_Nano_Pinout_Trig_Echo.png\" target=\"_blank\" rel=\"noreferrer noopener\">pinout diagram<\/a>) and connect the other pin to the (blue) GND track of the breadboard. The buzzer must be connected to a <a href=\"https:\/\/www.arduino.cc\/reference\/en\/language\/functions\/analog-io\/analogwrite\/\" target=\"_blank\" rel=\"noreferrer noopener\">PWM pin<\/a> on the Arduino. Pin 9 is a PWM pin. <a href=\"https:\/\/www.arduino.cc\/en\/tutorial\/PWM\" target=\"_blank\" rel=\"noreferrer noopener\">More on PWM<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"468\" src=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2020\/04\/buzzer-1024x468.jpg\" alt=\"\" class=\"wp-image-2831\" srcset=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2020\/04\/buzzer-1024x468.jpg 1024w, https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2020\/04\/buzzer-300x137.jpg 300w, https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2020\/04\/buzzer-768x351.jpg 768w, https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2020\/04\/buzzer-100x46.jpg 100w, https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2020\/04\/buzzer.jpg 1365w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Download the sketch <code><a href=\"https:\/\/home.et.utwente.nl\/slootenvanf\/div\/arduino\/sound_active_buzzer_example.ino\" target=\"_blank\" rel=\"noreferrer noopener\">sound_active_buzzer_example.ino<\/a><\/code>, connect the usb cable and upload the sketch. It should play a tune. You can also try this example <code><a href=\"https:\/\/home.et.utwente.nl\/slootenvanf\/div\/arduino\/sound_tune_happy_birthday.ino\" target=\"_blank\" rel=\"noreferrer noopener\">sound_tune_happy_birthday.ino<\/a><\/code> if the other one does not work.<\/p>\n\n\n\n<p>If you are already an experienced programmer, you should be able to combine the previous steps with this one and make an alarm dependent on how close an object is. But combining two sketches might be a bit much for a start, so we have done this for you. The only thing we left out is the part which determines when the alarm has to sound. (This will be an if statement with a call to a function).<\/p>\n\n\n\n<p>The combined example can be downloaded here: <code><a href=\"https:\/\/home.et.utwente.nl\/slootenvanf\/div\/arduino\/ultrasonic_sensor_warning.ino\" target=\"_blank\" rel=\"noreferrer noopener\">ultrasonic_sensor_warning.ino<\/a><\/code>. Browse through the code, and check for parts you recognize from the previous examples. Try the example (upload it). As you may notice, it plays a short tone at the start. Can you find the line of code which does this? It is the last line in the <em>setup()<\/em> function. This is the line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>playTone(C, 16 * tempo ); \/\/ play a C for 16 beats<\/code><\/pre>\n\n\n\n<p>If this way of generating sound with the playTone() function does not work in your situation, try it with the basic tone() function build into the Arduino core (<a href=\"https:\/\/www.arduino.cc\/reference\/en\/language\/functions\/advanced-io\/tone\/\" target=\"_blank\" rel=\"noreferrer noopener\">more info<\/a>):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tone(speakerOut, 2000, 500);<\/code><\/pre>\n\n\n\n<p>To make an alarm, two things have to be done:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Determine if an alarm has to be sound (with a condition)<\/li>\n\n\n\n<li>Make the alarm sound<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">Determine if an alarm has to be sound (with a condition)<\/h4>\n\n\n\n<p>In the loop(), near the end, find this line of comment:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ something nearby?<\/code><\/pre>\n\n\n\n<p>After that line, add an <a href=\"https:\/\/www.arduino.cc\/en\/Tutorial\/ifStatementConditional\" target=\"_blank\" rel=\"noreferrer noopener\">if-statement<\/a>, with the next line of comment inside it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (condition) {\n     \/\/ sound alarm\n}<\/code><\/pre>\n\n\n\n<p>If something is close, the variable&nbsp;<em>distance<\/em>&nbsp;is smaller than a certain value. So an example of the condition is&nbsp;<code>distance &lt; 100<\/code>. Add this to the code (replace the \u2018condition\u2019 between the brackets of the&nbsp;<em>if<\/em>-statement).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Make the alarm sound<\/h4>\n\n\n\n<p>Inside the if-statement, add the line of code which plays a tone (copy it from the last line of the <em>setup()<\/em> method). <strong>Make it play another tone, so not a C. You can find a list of the tones that can be played at the top of the sketch.<\/strong> If you are using the basic <em>tone()<\/em> function, adjust the frequency and the duration to generate a different tone (<a href=\"https:\/\/www.arduino.cc\/reference\/en\/language\/functions\/advanced-io\/tone\/\" target=\"_blank\" rel=\"noreferrer noopener\">use this info<\/a>).<\/p>\n\n\n\n<p>Adjust the comment at the end of that line so it matches the code.<\/p>\n\n\n\n<p>Now test the sketch (upload it). Does it work? Adjust the distance checked for in the condition. It should NOT be a meter (100cm).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">A small fix<\/h4>\n\n\n\n<p>You might notice that sometimes it gives a false warning, when a 0 value is measured. A 0 value might be the result of an error, or because the reflected signal is not returned properly because there is nothing in sight (do you know how an ultrasonic sensor works?&nbsp;<a href=\"https:\/\/home.et.utwente.nl\/slootenvanf\/div\/appdev\/slides\/AppDev1%20-%20intro%20to%20practical.pdf\" target=\"_blank\" rel=\"noreferrer noopener\">Check out the slide in the presentation about this<\/a>).<\/p>\n\n\n\n<p>The condition must be adjusted so it does not sound an alarm if the distance is 0. We can use <a href=\"https:\/\/www.arduino.cc\/reference\/en\/language\/structure\/boolean-operators\/logicaland\/\" target=\"_blank\" rel=\"noreferrer noopener\">an AND condition<\/a> for this: <code>distance &gt; 0 AND distance &lt; x<\/code>. (x is the value you use in your condition). In both C and Java, the boolean AND operator is a \u2018<code>&amp;&amp;<\/code>\u2018 symbol. So the condition will be: <code>distance &gt; 0 &amp;&amp; distance &lt; x<\/code>. Modify your code accordingly. Do not forget to replace the x with your value for the distance.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Add comments to the code<\/h2>\n\n\n\n<p>Add more comments to the part of the code that you added\/modified. The comments should explain,&nbsp;in your own words, what this code does. Use multi line comments or single line comments. Some examples:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*\nExample of a multi line comment.\nThis is the second line of the comment.\n*\/\n\n\/\/ Example of a line of code with comment at the end of the line:\nSerial.println(\"Start of distance measurement\"); \/\/ display text on serial output<\/code><\/pre>\n\n\n\n<p>At the top of the sketch, add general comments. The first line of comments&nbsp;should contain your name and student number. Add at least one more line which explains the general meaning of the sketch as a whole (what does it do?).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>With this tutorial you have familiarized yourself with building an electronic circuit using a breadboard and you have used the Arduino IDE to program the Arduino with a sketch.<br>In addition, you have learned the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Adding components like a display and a sensor to a breadboard and wiring them<\/li>\n\n\n\n<li>Using an example sketch to test the components<\/li>\n\n\n\n<li>Use and setup libraries<\/li>\n\n\n\n<li>Add code to an existing sketch<\/li>\n\n\n\n<li>Explain and document code by adding comments to code<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Introduction to Arduino and programming .&nbsp;There is also a &#8220;quick walk-through video&#8221; of this tutorial (but it is rather old). We will build a distance meter in this tutorial. It uses an ultrasonic distance sensor to sense the distance to an object nearby. It consist of the following steps: 1. Setup the Arduino and do [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2824,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","footnotes":""},"categories":[3],"tags":[510,246,94,228,92,95,161,141,509,171],"class_list":["post-4527","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-education","tag-alarm","tag-appdev","tag-arduino","tag-distance","tag-electronics","tag-programming","tag-sensor","tag-sound","tag-start","tag-ultrasonic-sensor"],"blocksy_meta":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Tutorial: build a distance sensor with an alarm - vanslooten.com<\/title>\n<meta name=\"description\" content=\"Tutorial: build a distance sensor with an alarm\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/home.et.utwente.nl\/slootenvanf\/2021\/09\/15\/distance-sensor-with-alarm\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Tutorial: build a distance sensor with an alarm - vanslooten.com\" \/>\n<meta property=\"og:description\" content=\"Tutorial: build a distance sensor with an alarm\" \/>\n<meta property=\"og:url\" content=\"https:\/\/home.et.utwente.nl\/slootenvanf\/2021\/09\/15\/distance-sensor-with-alarm\/\" \/>\n<meta property=\"og:site_name\" content=\"vanslooten.com\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-15T14:16:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-12-10T15:06:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2020\/04\/distance_sensor.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1365\" \/>\n\t<meta property=\"og:image:height\" content=\"768\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Fjodor van Slooten\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@fjodorvs\" \/>\n<meta name=\"twitter:site\" content=\"@fjodorvs\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Fjodor van Slooten\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"15 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/2021\\\/09\\\/15\\\/distance-sensor-with-alarm\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/2021\\\/09\\\/15\\\/distance-sensor-with-alarm\\\/\"},\"author\":{\"name\":\"Fjodor van Slooten\",\"@id\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/#\\\/schema\\\/person\\\/e62ff2d6beaa937dc9345a023eeb05dd\"},\"headline\":\"Tutorial: build a distance sensor with an alarm\",\"datePublished\":\"2021-09-15T14:16:10+00:00\",\"dateModified\":\"2025-12-10T15:06:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/2021\\\/09\\\/15\\\/distance-sensor-with-alarm\\\/\"},\"wordCount\":2327,\"publisher\":{\"@id\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/#\\\/schema\\\/person\\\/e62ff2d6beaa937dc9345a023eeb05dd\"},\"image\":{\"@id\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/2021\\\/09\\\/15\\\/distance-sensor-with-alarm\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/distance_sensor.jpg\",\"keywords\":[\"alarm\",\"appdev\",\"arduino\",\"distance\",\"electronics\",\"programming\",\"sensor\",\"sound\",\"start\",\"ultrasonic sensor\"],\"articleSection\":[\"Education\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/2021\\\/09\\\/15\\\/distance-sensor-with-alarm\\\/\",\"url\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/2021\\\/09\\\/15\\\/distance-sensor-with-alarm\\\/\",\"name\":\"Tutorial: build a distance sensor with an alarm - vanslooten.com\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/2021\\\/09\\\/15\\\/distance-sensor-with-alarm\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/2021\\\/09\\\/15\\\/distance-sensor-with-alarm\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/distance_sensor.jpg\",\"datePublished\":\"2021-09-15T14:16:10+00:00\",\"dateModified\":\"2025-12-10T15:06:53+00:00\",\"description\":\"Tutorial: build a distance sensor with an alarm\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/2021\\\/09\\\/15\\\/distance-sensor-with-alarm\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/2021\\\/09\\\/15\\\/distance-sensor-with-alarm\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/2021\\\/09\\\/15\\\/distance-sensor-with-alarm\\\/#primaryimage\",\"url\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/distance_sensor.jpg\",\"contentUrl\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/distance_sensor.jpg\",\"width\":1365,\"height\":768},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/2021\\\/09\\\/15\\\/distance-sensor-with-alarm\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tutorial: build a distance sensor with an alarm\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/#website\",\"url\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/\",\"name\":\"vanslooten.com\",\"description\":\"Personal website of Fjodor van Slooten\",\"publisher\":{\"@id\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/#\\\/schema\\\/person\\\/e62ff2d6beaa937dc9345a023eeb05dd\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/#\\\/schema\\\/person\\\/e62ff2d6beaa937dc9345a023eeb05dd\",\"name\":\"Fjodor van Slooten\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/wp-content\\\/uploads\\\/2019\\\/06\\\/2018-08-24-13.33.38_small.jpg\",\"url\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/wp-content\\\/uploads\\\/2019\\\/06\\\/2018-08-24-13.33.38_small.jpg\",\"contentUrl\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/wp-content\\\/uploads\\\/2019\\\/06\\\/2018-08-24-13.33.38_small.jpg\",\"width\":300,\"height\":214,\"caption\":\"Fjodor van Slooten\"},\"logo\":{\"@id\":\"https:\\\/\\\/home.et.utwente.nl\\\/slootenvanf\\\/wp-content\\\/uploads\\\/2019\\\/06\\\/2018-08-24-13.33.38_small.jpg\"},\"sameAs\":[\"http:\\\/\\\/vanslooten.com\",\"https:\\\/\\\/x.com\\\/fjodorvs\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Tutorial: build a distance sensor with an alarm - vanslooten.com","description":"Tutorial: build a distance sensor with an alarm","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/home.et.utwente.nl\/slootenvanf\/2021\/09\/15\/distance-sensor-with-alarm\/","og_locale":"en_US","og_type":"article","og_title":"Tutorial: build a distance sensor with an alarm - vanslooten.com","og_description":"Tutorial: build a distance sensor with an alarm","og_url":"https:\/\/home.et.utwente.nl\/slootenvanf\/2021\/09\/15\/distance-sensor-with-alarm\/","og_site_name":"vanslooten.com","article_published_time":"2021-09-15T14:16:10+00:00","article_modified_time":"2025-12-10T15:06:53+00:00","og_image":[{"width":1365,"height":768,"url":"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2020\/04\/distance_sensor.jpg","type":"image\/jpeg"}],"author":"Fjodor van Slooten","twitter_card":"summary_large_image","twitter_creator":"@fjodorvs","twitter_site":"@fjodorvs","twitter_misc":{"Written by":"Fjodor van Slooten","Est. reading time":"15 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/home.et.utwente.nl\/slootenvanf\/2021\/09\/15\/distance-sensor-with-alarm\/#article","isPartOf":{"@id":"https:\/\/home.et.utwente.nl\/slootenvanf\/2021\/09\/15\/distance-sensor-with-alarm\/"},"author":{"name":"Fjodor van Slooten","@id":"https:\/\/home.et.utwente.nl\/slootenvanf\/#\/schema\/person\/e62ff2d6beaa937dc9345a023eeb05dd"},"headline":"Tutorial: build a distance sensor with an alarm","datePublished":"2021-09-15T14:16:10+00:00","dateModified":"2025-12-10T15:06:53+00:00","mainEntityOfPage":{"@id":"https:\/\/home.et.utwente.nl\/slootenvanf\/2021\/09\/15\/distance-sensor-with-alarm\/"},"wordCount":2327,"publisher":{"@id":"https:\/\/home.et.utwente.nl\/slootenvanf\/#\/schema\/person\/e62ff2d6beaa937dc9345a023eeb05dd"},"image":{"@id":"https:\/\/home.et.utwente.nl\/slootenvanf\/2021\/09\/15\/distance-sensor-with-alarm\/#primaryimage"},"thumbnailUrl":"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2020\/04\/distance_sensor.jpg","keywords":["alarm","appdev","arduino","distance","electronics","programming","sensor","sound","start","ultrasonic sensor"],"articleSection":["Education"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/home.et.utwente.nl\/slootenvanf\/2021\/09\/15\/distance-sensor-with-alarm\/","url":"https:\/\/home.et.utwente.nl\/slootenvanf\/2021\/09\/15\/distance-sensor-with-alarm\/","name":"Tutorial: build a distance sensor with an alarm - vanslooten.com","isPartOf":{"@id":"https:\/\/home.et.utwente.nl\/slootenvanf\/#website"},"primaryImageOfPage":{"@id":"https:\/\/home.et.utwente.nl\/slootenvanf\/2021\/09\/15\/distance-sensor-with-alarm\/#primaryimage"},"image":{"@id":"https:\/\/home.et.utwente.nl\/slootenvanf\/2021\/09\/15\/distance-sensor-with-alarm\/#primaryimage"},"thumbnailUrl":"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2020\/04\/distance_sensor.jpg","datePublished":"2021-09-15T14:16:10+00:00","dateModified":"2025-12-10T15:06:53+00:00","description":"Tutorial: build a distance sensor with an alarm","breadcrumb":{"@id":"https:\/\/home.et.utwente.nl\/slootenvanf\/2021\/09\/15\/distance-sensor-with-alarm\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/home.et.utwente.nl\/slootenvanf\/2021\/09\/15\/distance-sensor-with-alarm\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/home.et.utwente.nl\/slootenvanf\/2021\/09\/15\/distance-sensor-with-alarm\/#primaryimage","url":"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2020\/04\/distance_sensor.jpg","contentUrl":"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2020\/04\/distance_sensor.jpg","width":1365,"height":768},{"@type":"BreadcrumbList","@id":"https:\/\/home.et.utwente.nl\/slootenvanf\/2021\/09\/15\/distance-sensor-with-alarm\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/home.et.utwente.nl\/slootenvanf\/"},{"@type":"ListItem","position":2,"name":"Tutorial: build a distance sensor with an alarm"}]},{"@type":"WebSite","@id":"https:\/\/home.et.utwente.nl\/slootenvanf\/#website","url":"https:\/\/home.et.utwente.nl\/slootenvanf\/","name":"vanslooten.com","description":"Personal website of Fjodor van Slooten","publisher":{"@id":"https:\/\/home.et.utwente.nl\/slootenvanf\/#\/schema\/person\/e62ff2d6beaa937dc9345a023eeb05dd"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/home.et.utwente.nl\/slootenvanf\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/home.et.utwente.nl\/slootenvanf\/#\/schema\/person\/e62ff2d6beaa937dc9345a023eeb05dd","name":"Fjodor van Slooten","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/06\/2018-08-24-13.33.38_small.jpg","url":"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/06\/2018-08-24-13.33.38_small.jpg","contentUrl":"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/06\/2018-08-24-13.33.38_small.jpg","width":300,"height":214,"caption":"Fjodor van Slooten"},"logo":{"@id":"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-content\/uploads\/2019\/06\/2018-08-24-13.33.38_small.jpg"},"sameAs":["http:\/\/vanslooten.com","https:\/\/x.com\/fjodorvs"]}]}},"_links":{"self":[{"href":"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-json\/wp\/v2\/posts\/4527","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-json\/wp\/v2\/comments?post=4527"}],"version-history":[{"count":40,"href":"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-json\/wp\/v2\/posts\/4527\/revisions"}],"predecessor-version":[{"id":6663,"href":"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-json\/wp\/v2\/posts\/4527\/revisions\/6663"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-json\/wp\/v2\/media\/2824"}],"wp:attachment":[{"href":"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-json\/wp\/v2\/media?parent=4527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-json\/wp\/v2\/categories?post=4527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/home.et.utwente.nl\/slootenvanf\/wp-json\/wp\/v2\/tags?post=4527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}