<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>web server Archives | Microcontroller Tutorials</title>
	<atom:link href="https://www.teachmemicro.com/tag/web-server/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.teachmemicro.com/tag/web-server/</link>
	<description>Microcontroller Tutorials and Resources</description>
	<lastBuildDate>Sun, 28 Jun 2026 01:20:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.2</generator>

<image>
	<url>https://www.teachmemicro.com/wp-content/uploads/2019/04/blue-icon-65x65.png</url>
	<title>web server Archives | Microcontroller Tutorials</title>
	<link>https://www.teachmemicro.com/tag/web-server/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Use PIR Sensor with NodeMCU ESP8266</title>
		<link>https://www.teachmemicro.com/pir-sensor-nodemcu-esp8266/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=pir-sensor-nodemcu-esp8266</link>
					<comments>https://www.teachmemicro.com/pir-sensor-nodemcu-esp8266/#comments</comments>
		
		<dc:creator><![CDATA[Roland Pelayo]]></dc:creator>
		<pubDate>Sat, 04 May 2019 12:36:23 +0000</pubDate>
				<category><![CDATA[ESP8266 Tutorial]]></category>
		<category><![CDATA[Sensor Tutorial]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[motion sensor]]></category>
		<category><![CDATA[web server]]></category>
		<guid isPermaLink="false">https://www.teachmemicro.com/?p=3294</guid>

					<description><![CDATA[<p>A Passive Infrared (PIR) sensor is a popular sensor for detecting the presence of a person. This is a type of proximity sensor although you may also call it a motion sensor. This tutorial aims to guide you in using a PIR sensor with your NodeMCU ESP8266 WiFi board. Introduction The human body emits infrared &#8230;</p>
<p>The post <a href="https://www.teachmemicro.com/pir-sensor-nodemcu-esp8266/">How to Use PIR Sensor with NodeMCU ESP8266</a> appeared first on <a href="https://www.teachmemicro.com">Microcontroller Tutorials</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>A Passive Infrared (PIR) sensor is a popular sensor for detecting the presence of a person. This is a type of <a href="https://en.wikipedia.org/wiki/Proximity_sensor">proximity sensor</a> although you may also call it a motion sensor. This tutorial aims to guide you in using a PIR sensor with your NodeMCU <a href="https://www.win-source.net/products/detail/espressif/esp8266ex.html">ESP8266</a> WiFi board.</p>
<p><span id="more-3294"></span></p>
<h3><strong>Introduction</strong></h3>
<p><a href="https://www.teachmemicro.com/wp-content/uploads/2019/05/nodemcu-pir-sensor-featured.jpg"><img loading="lazy" decoding="async" class="wp-image-3297 aligncenter" src="https://www.teachmemicro.com/wp-content/uploads/2019/05/nodemcu-pir-sensor-featured.jpg" alt="NodeMCU PIR Sensor" width="649" height="487" srcset="https://www.teachmemicro.com/wp-content/uploads/2019/05/nodemcu-pir-sensor-featured.jpg 800w, https://www.teachmemicro.com/wp-content/uploads/2019/05/nodemcu-pir-sensor-featured-300x225.jpg 300w, https://www.teachmemicro.com/wp-content/uploads/2019/05/nodemcu-pir-sensor-featured-768x576.jpg 768w, https://www.teachmemicro.com/wp-content/uploads/2019/05/nodemcu-pir-sensor-featured-320x240.jpg 320w" sizes="auto, (max-width: 649px) 100vw, 649px" /></a></p>
<p>The human body emits infrared signals by virtue of its temperature. When a person is near a PIR sensor, the infrared detector triggers and the voltage on the output pin goes low.</p>
<p>To enhance the reception of infrared signals, PIR sensors often come with a Fresnel dome. Most of the time, the dome is removable as shown below:</p>
<p><a href="https://www.teachmemicro.com/wp-content/uploads/2019/05/nodemcu-pir-sensor-without-dome.jpg"><img loading="lazy" decoding="async" class="wp-image-3298 aligncenter" src="https://www.teachmemicro.com/wp-content/uploads/2019/05/nodemcu-pir-sensor-without-dome.jpg" alt="NodeMCU PIR Sensor without dome" width="649" height="487" srcset="https://www.teachmemicro.com/wp-content/uploads/2019/05/nodemcu-pir-sensor-without-dome.jpg 800w, https://www.teachmemicro.com/wp-content/uploads/2019/05/nodemcu-pir-sensor-without-dome-300x225.jpg 300w, https://www.teachmemicro.com/wp-content/uploads/2019/05/nodemcu-pir-sensor-without-dome-768x576.jpg 768w, https://www.teachmemicro.com/wp-content/uploads/2019/05/nodemcu-pir-sensor-without-dome-320x240.jpg 320w" sizes="auto, (max-width: 649px) 100vw, 649px" /></a></p>
<p>Removing the dome reveals the infrared receiver. The sensor will still work but will only detect persons directly in front of the receiver.</p>
<h3><strong>How to Connect to a PIR Sensor</strong></h3>
<p>PIR sensor modules commonly have three output pins: VCC, OUT and GND.</p>
<p><em>For DFRobot's PIR Sensor Board:</em></p>
<p><img decoding="async" class="aligncenter" title="Infrared_Sensor_new1.jpg" src="https://raw.githubusercontent.com/DFRobot/DFRobotMediaWikiImage/master/Image/Infrared_Sensor_new1.jpg" alt="Infrared_Sensor_new1.jpg" /></p>
<blockquote><p>1 - OUT<br />
2 - VCC<br />
3 - GND<br />
4 - Trigger Select<br />
5 - Sensitivity Adjust</p></blockquote>
<p><em>For HC-SR501 Board:</em></p>
<p><img loading="lazy" decoding="async" class="size-full wp-image-1702 aligncenter" src="https://i2.wp.com/www.teachmemicro.com/wp-content/uploads/2018/03/PIR-Motion-Sensor-underside-annotated.jpg?resize=640%2C640&amp;ssl=1" sizes="auto, (max-width: 640px) 100vw, 640px" srcset="https://i2.wp.com/www.teachmemicro.com/wp-content/uploads/2018/03/PIR-Motion-Sensor-underside-annotated.jpg?w=800&amp;ssl=1 800w, https://i2.wp.com/www.teachmemicro.com/wp-content/uploads/2018/03/PIR-Motion-Sensor-underside-annotated.jpg?resize=150%2C150&amp;ssl=1 150w, https://i2.wp.com/www.teachmemicro.com/wp-content/uploads/2018/03/PIR-Motion-Sensor-underside-annotated.jpg?resize=300%2C300&amp;ssl=1 300w, https://i2.wp.com/www.teachmemicro.com/wp-content/uploads/2018/03/PIR-Motion-Sensor-underside-annotated.jpg?resize=768%2C768&amp;ssl=1 768w, https://i2.wp.com/www.teachmemicro.com/wp-content/uploads/2018/03/PIR-Motion-Sensor-underside-annotated.jpg?resize=65%2C65&amp;ssl=1 65w, https://i2.wp.com/www.teachmemicro.com/wp-content/uploads/2018/03/PIR-Motion-Sensor-underside-annotated.jpg?resize=24%2C24&amp;ssl=1 24w, https://i2.wp.com/www.teachmemicro.com/wp-content/uploads/2018/03/PIR-Motion-Sensor-underside-annotated.jpg?resize=36%2C36&amp;ssl=1 36w, https://i2.wp.com/www.teachmemicro.com/wp-content/uploads/2018/03/PIR-Motion-Sensor-underside-annotated.jpg?resize=48%2C48&amp;ssl=1 48w, https://i2.wp.com/www.teachmemicro.com/wp-content/uploads/2018/03/PIR-Motion-Sensor-underside-annotated.jpg?resize=120%2C120&amp;ssl=1 120w" alt="HC-SR501 PIR motion sensor underside" width="585" height="585" data-attachment-id="1702" data-permalink="https://www.teachmemicro.com/pir-motion-sensor/pir-motion-sensor-underside-annotated/" data-orig-file="https://i2.wp.com/www.teachmemicro.com/wp-content/uploads/2018/03/PIR-Motion-Sensor-underside-annotated.jpg?fit=800%2C800&amp;ssl=1" data-orig-size="800,800" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;1&quot;}" data-image-title="PIR-Motion-Sensor-underside-annotated" data-image-description="" data-medium-file="https://i2.wp.com/www.teachmemicro.com/wp-content/uploads/2018/03/PIR-Motion-Sensor-underside-annotated.jpg?fit=300%2C300&amp;ssl=1" data-large-file="https://i2.wp.com/www.teachmemicro.com/wp-content/uploads/2018/03/PIR-Motion-Sensor-underside-annotated.jpg?fit=640%2C640&amp;ssl=1" /></p>
<p>The VCC pin connects to the positive source voltage of any microcontroller board. PIR sensor modules will work with both 3.3V and 5V levels. Thus, it is safe to connect the VCC pin of the PIR sensor to any of the 3.3V of the NodeMCU. The GND pin, of course, connects to the GND pin of the NodeMCU.</p>
<p>The OUT pin connects to any digital pin of the NodeMCU. This pin will go low when the sensor triggers.</p>
<p>Remember that the numbers on the board is different from the numbers used in programming. The <a href="https://www.teachmemicro.com/nodemcu-pinout/">NodeMCU pinout guide</a> will help you with the correct pin numbers.</p>
<p>Most modules contain a knob for adjusting sensitivity and a jumper for trigger selection. These are covered in a separate <a href="https://www.teachmemicro.com/pir-motion-sensor/">PIR sensor article</a>.</p>
<p>For the rest of this tutorial, we will follow this wiring diagram:</p>
<p><a href="https://www.teachmemicro.com/wp-content/uploads/2019/05/nodemcu-pir-sensor-1.jpg"><img loading="lazy" decoding="async" class="size-full wp-image-3296 aligncenter" src="https://www.teachmemicro.com/wp-content/uploads/2019/05/nodemcu-pir-sensor-1.jpg" alt="NodeMCU PIR Sensor" width="558" height="522" srcset="https://www.teachmemicro.com/wp-content/uploads/2019/05/nodemcu-pir-sensor-1.jpg 558w, https://www.teachmemicro.com/wp-content/uploads/2019/05/nodemcu-pir-sensor-1-300x281.jpg 300w" sizes="auto, (max-width: 558px) 100vw, 558px" /></a></p>
<h3><strong>Programming the NodeMCU with PIR Sensor</strong></h3>
<p>Before continuing, make sure to read <a href="https://www.teachmemicro.com/intro-nodemcu-arduino/">how to use an Arduino IDE to program the NodeMCU</a>.</p>
<p>We’ll start with a basic sketch that will give an output on the serial monitor every time the PIR sensor triggers.</p>
<pre class="lang:arduino decode:true"><pre><code class="language-cpp">const int PIRSensorOutPin = 2;    //PIR Sensor OUT Pin
 void setup() {
    Serial.begin(9600);
    pinMode(PIRSensorOutPin, INPUT);
 }
 void loop()
 {
    if (digitalRead(PIRSensorOutPin) == LOW)
    {
       Serial.println(&quot;Person detected!&quot;);    //Print to serial monitor
    }
    else {;}
 }</code></pre></pre>
<p>As you can see, the sketch is straightforward. We use the same function we used to read a button:</p>
<pre class="lang:arduino decode:true "><pre><code class="language-cpp">digitalRead(PIRSensorOutPin)</code></pre></pre>
<p>We then insert the function above in an if-else statement and execute a print to serial if the if-else returns a zero.</p>
<pre class="lang:arduino decode:true "><pre><code class="language-cpp">if (digitalRead(PIRSensorOutPin) == LOW) 
 { 
    Serial.println(&quot;Person detected!&quot;); //Print to serial monitor 
 }else{;}</code></pre></pre>
<p>The full sketch above is useful for testing if your PIR sensor is working.</p>
<p>However, since we are using a NodeMCU instead of an Arduino in this tutorial, it is best to use the board as it is meant to.</p>
<p>Instead of giving an output to the serial monitor every time a person is detected, we will give an output to a web page hosted by the NodeMCU.</p>
<p>For this example project, we will modify the example sketch that comes with <a href="https://www.teachmemicro.com/nodemcu-ajax-dynamic-sensor-data-web-page/">this tutorial</a>. AJAX is helpful here so that the page will update automatically when the PIR sensor triggers.</p>
<p>This is now the modified sketch:</p>
<pre class="lang:arduino decode:true "><pre><code class="language-cpp">#include &lt;ESP8266WiFi.h&gt;
#include &lt;WiFiClient.h&gt;
#include &lt;ESP8266WebServer.h&gt;
 
// Replace with your network credentials
const char* ssid = &quot;&lt;Your-WiFi-SSID&gt;&quot;;
const char* password = &quot;&lt;Your-WiFi-Password&gt;&quot;;
ESP8266WebServer server(80);   //instantiate server at port 80 (http port)
 
String page = &quot;&quot;;
String text = &quot;&quot;;

int PIRSensorOutputPin = 2;
bool isTriggered = false;
 
void setup(void){
 pinMode(PIRSensorOutputPin, INPUT);
 delay(1000);
 Serial.begin(115200);
 WiFi.begin(ssid, password); //begin WiFi connection
 Serial.println(&quot;&quot;);
 
 // Wait for connection
 while (WiFi.status() != WL_CONNECTED) {
 delay(500);
 Serial.print(&quot;.&quot;);
}
 
 Serial.println(&quot;&quot;);
 Serial.print(&quot;Connected to &quot;);
 Serial.println(ssid);
 Serial.print(&quot;IP address: &quot;);
 Serial.println(WiFi.localIP());
 server.on(&quot;/data.txt&quot;, [](){
   if(isTriggered){
     text = &quot;Person Detected!&quot;;
   }else{
     text = &quot;&quot;;
   }
   server.send(200, &quot;text/html&quot;, text);
 });
 server.on(&quot;/&quot;, [](){
   page = &quot;&lt;h1&gt;PIR Sensor to NodeMCU/h1&gt;&lt;h1&gt;Data:&lt;/h1&gt; &lt;h1 id=\&quot;data\&quot;&gt;&quot;&quot;&lt;/h1&gt;\r\n&quot;;
   page += &quot;&lt;script&gt;\r\n&quot;;
   page += &quot;var x = setInterval(function() {loadData(\&quot;data.txt\&quot;,updateData)}, 1000);\r\n&quot;;
   page += &quot;function loadData(url, callback){\r\n&quot;;
   page += &quot;var xhttp = new XMLHttpRequest();\r\n&quot;;
   page += &quot;xhttp.onreadystatechange = function(){\r\n&quot;;
   page += &quot; if(this.readyState == 4 &amp;&amp; this.status == 200){\r\n&quot;;
   page += &quot; callback.apply(xhttp);\r\n&quot;;
   page += &quot; }\r\n&quot;;
   page += &quot;};\r\n&quot;;
   page += &quot;xhttp.open(\&quot;GET\&quot;, url, true);\r\n&quot;;
   page += &quot;xhttp.send();\r\n&quot;;
   page += &quot;}\r\n&quot;;
   page += &quot;function updateData(){\r\n&quot;;
   page += &quot; document.getElementById(\&quot;data\&quot;).innerHTML = this.responseText;\r\n&quot;;
   page += &quot;}\r\n&quot;;
   page += &quot;&lt;/script&gt;\r\n&quot;;
   server.send(200, &quot;text/html&quot;, page);
});
 
 server.begin();
 Serial.println(&quot;Web server started!&quot;);
}
 
void loop(void){
 if(digitalRead(PIRSensorOutputPin) == 0){
     isTriggered = true;
 }else{
     isTriggered = false;
 }
 server.handleClient();
}</code></pre></pre>
<p>Upload this sketch to the NodeMCU, providing your own WiFi SSID and password on lines 6 and 7. When successful, the serial monitor will give you an IP address. Use a browser and visit that IP address. The page should display "Person detected!" when the sensor is triggered.</p>
<p>Was the tutorial helpful? Kindly comment below for any questions or suggestions you have for this tutorial.</p>
<p>The post <a href="https://www.teachmemicro.com/pir-sensor-nodemcu-esp8266/">How to Use PIR Sensor with NodeMCU ESP8266</a> appeared first on <a href="https://www.teachmemicro.com">Microcontroller Tutorials</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.teachmemicro.com/pir-sensor-nodemcu-esp8266/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 

Served from: www.teachmemicro.com @ 2026-07-26 07:41:32 by W3 Total Cache
-->