<?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>Arduino Projects | Teach Me Microcontrollers!</title>
	<atom:link href="https://www.teachmemicro.com/category/projects/arduino-projects/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.teachmemicro.com/category/projects/arduino-projects/</link>
	<description>Microcontroller Tutorials and Resources</description>
	<lastBuildDate>Sun, 28 Jun 2026 01:12:36 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1</generator>

<image>
	<url>https://www.teachmemicro.com/wp-content/uploads/2019/04/blue-icon-65x65.png</url>
	<title>Arduino Projects | Teach Me Microcontrollers!</title>
	<link>https://www.teachmemicro.com/category/projects/arduino-projects/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Use LM35 Temperature Sensor with Arduino and ESP32: Complete Guide with Example Projects</title>
		<link>https://www.teachmemicro.com/how-to-use-lm35-temperature-sensor-with-arduino-and-esp32-complete-guide-with-example-projects/</link>
		
		<dc:creator><![CDATA[Roland Pelayo]]></dc:creator>
		<pubDate>Mon, 03 Nov 2025 01:00:27 +0000</pubDate>
				<category><![CDATA[Arduino Projects]]></category>
		<category><![CDATA[ESP32 Projects]]></category>
		<guid isPermaLink="false">https://www.teachmemicro.com/?p=9613</guid>

					<description><![CDATA[<p>Introduction to LM35 Temperature Sensor The LM35 is a precision temperature sensor that provides an analog voltage output proportional to the measured temperature. It's popular among electronics enthusiasts for its simplicity, accuracy, and ease of use with microcontrollers like Arduino and ESP32. In this tutorial, you’ll learn how LM35 works, how to interface it with &#8230;</p>
<p>The post <a href="https://www.teachmemicro.com/how-to-use-lm35-temperature-sensor-with-arduino-and-esp32-complete-guide-with-example-projects/">How to Use LM35 Temperature Sensor with Arduino and ESP32: Complete Guide with Example Projects</a> appeared first on <a href="https://www.teachmemicro.com">Teach Me Microcontrollers!</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h3><strong>Introduction to LM35 Temperature Sensor</strong></h3>
<p>The <strong>LM35</strong> is a precision temperature sensor that provides an analog voltage output proportional to the measured temperature. It's popular among electronics enthusiasts for its <strong>simplicity, accuracy, and ease of use</strong> with microcontrollers like <strong>Arduino and ESP32</strong>.</p>
<p>In this tutorial, you’ll learn how LM35 works, how to interface it with Arduino and ESP32, and how to build <strong>two practical projects</strong> — a basic temperature reader and an IoT-based web server display.</p>
<p><span id="more-9613"></span></p>
<hr />
<h3><strong>What Is an LM35 Sensor?</strong></h3>
<p><a href="https://www.teachmemicro.com/wp-content/uploads/2025/11/lm35-1.avif"><img data-dominant-color="beb4ab" data-has-transparency="true" style="--dominant-color: #beb4ab;" loading="lazy" decoding="async" class="aligncenter size-full wp-image-9786 has-transparency" src="https://www.teachmemicro.com/wp-content/uploads/2025/11/lm35-1.avif" alt="lm35" width="512" height="512" srcset="https://www.teachmemicro.com/wp-content/uploads/2025/11/lm35-1.avif 512w, https://www.teachmemicro.com/wp-content/uploads/2025/11/lm35-1-300x300.avif 300w, https://www.teachmemicro.com/wp-content/uploads/2025/11/lm35-1-150x150.avif 150w" sizes="auto, (max-width: 512px) 100vw, 512px" /></a></p>
<p>The <strong>LM35</strong> is an analog temperature sensor that outputs voltage linearly related to the Celsius temperature. It doesn’t require calibration and provides a direct temperature-to-voltage conversion, making it extremely beginner-friendly.</p>
<p><strong>Manufacturer:</strong> Texas Instruments<br />
<strong>Output Type:</strong> Analog (10mV/°C)<br />
<strong>Operating Range:</strong> -55°C to +150°C<br />
<strong>Accuracy:</strong> ±0.5°C (at room temperature)</p>
<hr />
<h3><strong>Key Features and Specifications of LM35</strong></h3>
<div class="_tableContainer_1rjym_1">
<div class="group _tableWrapper_1rjym_13 flex w-fit flex-col-reverse" tabindex="-1">
<table class="w-fit min-w-(--thread-content-width)">
<thead>
<tr>
<th data-col-size="sm">Feature</th>
<th data-col-size="sm">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td data-col-size="sm">Operating Voltage</td>
<td data-col-size="sm">4V to 30V</td>
</tr>
<tr>
<td data-col-size="sm">Output Voltage</td>
<td data-col-size="sm">10 mV per °C</td>
</tr>
<tr>
<td data-col-size="sm">Temperature Range</td>
<td data-col-size="sm">-55°C to +150°C</td>
</tr>
<tr>
<td data-col-size="sm">Accuracy</td>
<td data-col-size="sm">±0.5°C (at 25°C)</td>
</tr>
<tr>
<td data-col-size="sm">Output Type</td>
<td data-col-size="sm">Analog</td>
</tr>
<tr>
<td data-col-size="sm">Power Consumption</td>
<td data-col-size="sm">Low</td>
</tr>
</tbody>
</table>
</div>
</div>
<hr />
<h3><strong>How the LM35 Sensor Works: Principle of Operation</strong></h3>
<p>The LM35 works on the principle that the output voltage changes linearly with temperature.<br />
For every <strong>1°C rise</strong>, the output increases by <strong>10 mV</strong>.</p>
<p>So if the LM35 outputs <strong>250 mV</strong>, the temperature is <strong>25°C</strong>.<br />
Formula:</p>
<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary">
<div class="overflow-y-auto p-4" dir="ltr">
<p style="text-align: center;"><span class='MathJax_Preview'><img src='https://www.teachmemicro.com/wp-content/plugins/latex/cache/tex_b5484f5138f1d97c16e6b62d873cbfc4.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="T(^{0}C) = \frac{V_{out} (in\; mV)}{10}" /></span><script type='math/tex'>T(^{0}C) = \frac{V_{out} (in\; mV)}{10}</script></p>
</div>
</div>
<p>This linear relationship makes the LM35 simple to use — no complex calibration or signal conditioning is required.</p>
<p>That means:</p>
<ul>
<li>0°C → 0V</li>
<li>25°C → 250mV</li>
<li>100°C → 1.0V</li>
</ul>
<p>Since the ESP32 ADC reads voltage in a range (0–3.3V), we can compute the temperature as:</p>
<p style="text-align: center;"><span class='MathJax_Preview'><img src='https://www.teachmemicro.com/wp-content/plugins/latex/cache/tex_2f34b41f3800ba68b54f2a4506d11883.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="T(^{0}C)=(\frac{ADC_{value}}{4095})\cdot3.3\cdot100" /></span><script type='math/tex'>T(^{0}C)=(\frac{ADC_{value}}{4095})\cdot3.3\cdot100</script></p>
<hr />
<h3><strong>Understanding the LM35 Pin Configuration and Circuit Diagram</strong></h3>
<p><a href="https://www.teachmemicro.com/wp-content/uploads/2025/11/lm35-pinout.avif"><img data-dominant-color="e6e6e6" data-has-transparency="false" style="--dominant-color: #e6e6e6;" loading="lazy" decoding="async" class="aligncenter size-full wp-image-9789 not-transparent" src="https://www.teachmemicro.com/wp-content/uploads/2025/11/lm35-pinout.avif" alt="LM35 pinout" width="984" height="756" srcset="https://www.teachmemicro.com/wp-content/uploads/2025/11/lm35-pinout.avif 984w, https://www.teachmemicro.com/wp-content/uploads/2025/11/lm35-pinout-300x230.avif 300w, https://www.teachmemicro.com/wp-content/uploads/2025/11/lm35-pinout-768x590.avif 768w" sizes="auto, (max-width: 984px) 100vw, 984px" /></a></p>
<h4><strong>LM35 Pinout Explained</strong></h4>
<div class="_tableContainer_1rjym_1">
<div class="group _tableWrapper_1rjym_13 flex w-fit flex-col-reverse" tabindex="-1">
<table class="w-fit min-w-(--thread-content-width)">
<thead>
<tr>
<th data-col-size="sm">Pin</th>
<th data-col-size="sm">Function</th>
<th data-col-size="sm">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td data-col-size="sm">1</td>
<td data-col-size="sm">VCC</td>
<td data-col-size="sm">Connects to 5V (Arduino) or 3.3V (ESP32)</td>
</tr>
<tr>
<td data-col-size="sm">2</td>
<td data-col-size="sm">VOUT</td>
<td data-col-size="sm">Analog output voltage</td>
</tr>
<tr>
<td data-col-size="sm">3</td>
<td data-col-size="sm">GND</td>
<td data-col-size="sm">Ground connection</td>
</tr>
</tbody>
</table>
</div>
</div>
<hr />
<h4><strong>LM35 Wiring and Power Requirements</strong></h4>
<ul>
<li>For <strong>Arduino</strong>, power it with <strong>5V</strong>.</li>
<li>For <strong>ESP32</strong>, use <strong>3.3V</strong>.</li>
<li>Always use <strong>a common ground</strong> between LM35 and the microcontroller.</li>
<li>Optionally, add a <strong>0.1 µF capacitor</strong> between VOUT and GND to stabilize readings.</li>
</ul>
<hr />
<h3><strong>Using LM35 with Arduino: Step-by-Step Guide</strong></h3>
<h4><strong>Components Required for Arduino + LM35 Project</strong></h4>
<ul>
<li>Arduino Uno (or Nano)</li>
<li>LM35 temperature sensor</li>
<li>Jumper wires</li>
<li>Breadboard</li>
<li>(Optional) 16x2 LCD display</li>
</ul>
<hr />
<h4><strong>Circuit Diagram for LM35 with Arduino</strong></h4>
<p><a href="https://www.teachmemicro.com/wp-content/uploads/2025/11/arduino-lm35.avif"><img data-dominant-color="97acba" data-has-transparency="false" style="--dominant-color: #97acba;" loading="lazy" decoding="async" class="aligncenter wp-image-9787 size-full not-transparent" src="https://www.teachmemicro.com/wp-content/uploads/2025/11/arduino-lm35.avif" alt="Arduino and LM35 wiring diagram" width="613" height="377" srcset="https://www.teachmemicro.com/wp-content/uploads/2025/11/arduino-lm35.avif 613w, https://www.teachmemicro.com/wp-content/uploads/2025/11/arduino-lm35-300x185.avif 300w" sizes="auto, (max-width: 613px) 100vw, 613px" /></a></p>
<p><strong>Connections:</strong></p>
<ul>
<li>LM35 VCC → 5V</li>
<li>LM35 GND → GND</li>
<li>LM35 VOUT → A0 (analog pin)</li>
</ul>
<hr />
<h4><strong>Arduino Code to Read Temperature from LM35</strong></h4>
<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary">
<div class="sticky top-9">
<div class="absolute end-0 bottom-0 flex h-9 items-center pe-2">
<div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs"></div>
</div>
</div>
<div class="overflow-y-auto p-4" dir="ltr">
<pre><pre><code class="language-cpp">const int sensorPin = A0;
float temperature;

void setup() {
  Serial.begin(9600);
}

void loop() {
  int sensorValue = analogRead(sensorPin);
  float voltage = (sensorValue / 1023.0) * 5.0;
  temperature = voltage * 100; // 10mV per degree Celsius
  Serial.print(&quot;Temperature: &quot;);
  Serial.print(temperature);
  Serial.println(&quot; °C&quot;);
  delay(1000);
}</code></pre></pre>
</div>
</div>
<hr />
<h4><strong>Displaying Temperature on Serial Monitor or LCD</strong></h4>
<ul>
<li>Open <strong>Serial Monitor</strong> (Ctrl + Shift + M).</li>
<li>You’ll see continuous readings like:
<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary">
<div class="sticky top-9">
<div class="absolute end-0 bottom-0 flex h-9 items-center pe-2">
<div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs"></div>
</div>
</div>
<div class="overflow-y-auto p-4" dir="ltr">Temperature: 27.35 °C<br />
Temperature: 27.42 °C</div>
<div dir="ltr"></div>
</div>
</li>
<li>To display it on an <strong>LCD</strong>, use the <a href="https://www.teachmemicro.com/arduino-lcd-tutorial/"><em>LiquidCrystal</em> library</a> and connect pins RS, E, D4–D7 accordingly.</li>
</ul>
<hr />
<h3><strong>Using LM35 with ESP32: IoT-Based Temperature Monitoring</strong></h3>
<h4><strong>Components Required for ESP32 + LM35 Project</strong></h4>
<ul>
<li>ESP32 board</li>
<li>LM35 temperature sensor</li>
<li>Breadboard and jumper wires</li>
<li>(Optional) Wi-Fi network for IoT display</li>
</ul>
<hr />
<h4><strong>Circuit Diagram for LM35 with ESP32</strong></h4>
<p><strong>Connections:</strong></p>
<ul>
<li>LM35 VCC → 3.3V</li>
<li>LM35 GND → GND</li>
<li>LM35 VOUT → GPIO34 (analog input)</li>
</ul>
<hr />
<h4><strong>ESP32 Code to Read Temperature from LM35</strong></h4>
<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary">
<div class="overflow-y-auto p-4" dir="ltr">
<pre><pre><code class="language-cpp">const int sensorPin = 34;
float temperature;

void setup() {
  Serial.begin(115200);
}

void loop() {
  int sensorValue = analogRead(sensorPin);
  float voltage = (sensorValue / 4095.0) * 3.3;
  temperature = voltage * 100;
  Serial.print(&quot;Temperature: &quot;);
  Serial.print(temperature);
  Serial.println(&quot; °C&quot;);
  delay(2000);
}</code></pre></pre>
</div>
</div>
<hr />
<h4><strong>Displaying Temperature on a Web Server (IoT Project)</strong></h4>
<p>Below is a full example code that:</p>
<ol>
<li>Reads temperature from LM35.</li>
<li>Connects to Wi-Fi.</li>
<li>Hosts a simple webpage showing live temperature readings.</li>
</ol>
<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary">
<pre><pre><code class="language-cpp">#include &lt;WiFi.h&gt;
#include &lt;WebServer.h&gt;

// Replace with your Wi-Fi credentials
const char* ssid = &quot;YOUR_WIFI_SSID&quot;;
const char* password = &quot;YOUR_WIFI_PASSWORD&quot;;

const int lm35_pin = 34; // Analog pin for LM35
WebServer server(80);    // Web server on port 80

float read_temperature() {
  int adc_value = analogRead(lm35_pin);
  float voltage = (adc_value / 4095.0) * 3.3;  // Convert ADC reading to voltage
  float temperature_c = voltage * 100;         // 10mV per °C
  return temperature_c;
}


String html_page(float temp_c) {
  String html = &quot;&lt;!DOCTYPE html&gt;&lt;html&gt;&lt;head&gt;&lt;meta name=&#039;viewport&#039; content=&#039;width=device-width, initial-scale=1.0&#039;&gt;&quot;;
  html += &quot;&lt;title&gt;ESP32 LM35 Temperature&lt;/title&gt;&quot;;
  html += &quot;&lt;style&gt;body{font-family:Arial;text-align:center;background:#111;color:#0ff;}h1{color:#00ffff;}&lt;/style&gt;&quot;;
  html += &quot;&lt;/head&gt;&lt;body&gt;&quot;;
  html += &quot;&lt;h1&gt;ESP32 LM35 Temperature Monitor&lt;/h1&gt;&quot;;
  html += &quot;&lt;h2&gt;Current Temperature: &quot; + String(temp_c, 2) + &quot; &deg;C&lt;/h2&gt;&quot;;
  html += &quot;&lt;meta http-equiv=&#039;refresh&#039; content=&#039;2&#039;&gt;&quot;; // auto-refresh every 2 seconds
  html += &quot;&lt;/body&gt;&lt;/html&gt;&quot;;
  return html;
}


void handle_root() {
  float temp = read_temperature();
  server.send(200, &quot;text/html&quot;, html_page(temp));
}

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.print(&quot;Connecting to Wi-Fi&quot;);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(&quot;.&quot;);
  }
  Serial.println(&quot;\nConnected!&quot;);
  Serial.print(&quot;ESP32 IP address: &quot;);
  Serial.println(WiFi.localIP());

  server.on(&quot;/&quot;, handle_root);
  server.begin();
  Serial.println(&quot;Web server started!&quot;);
}

void loop() {
  server.handleClient();
}</code></pre></pre>
</div>
<hr />
<h4>Accessing the Web Page</h4>
<ol>
<li>Upload the sketch to your ESP32.</li>
<li>Open the Serial Monitor at 115200 baud.</li>
<li>Wait until you see:
<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary">
<div class="sticky top-9"></div>
<div class="overflow-y-auto p-4" dir="ltr">Connected!<br />
ESP32 IP <span class="hljs-selector-tag">address</span>: <span class="hljs-number">192.168</span>.<span class="hljs-number">1</span>.xxx</div>
</div>
</li>
<li>Open that IP in your browser (e.g., <em>http://192.168.1.45</em>)</li>
<li>You’ll see a <strong>temperature dashboard</strong> refreshing every 2 seconds!</li>
</ol>
<hr />
<h3><strong>Troubleshooting Common Issues</strong></h3>
<h4><strong>Incorrect Temperature Readings</strong></h4>
<ul>
<li>Check voltage reference (5V or 3.3V).</li>
<li>Ensure correct analog pin.</li>
<li>Add a capacitor between VOUT and GND to reduce noise.</li>
</ul>
<h4><strong>Sensor Noise and Calibration Tips</strong></h4>
<ul>
<li>Use shielded cables for long distances.</li>
<li>Calibrate by comparing with a digital thermometer.</li>
</ul>
<hr />
<h3><strong>Applications of LM35 in Real-Life Projects</strong></h3>
<h4><strong>Home Automation Systems</strong></h4>
<ul>
<li>Temperature-controlled fans or air conditioners.</li>
</ul>
<h4><strong>Environmental Monitoring and IoT Systems</strong></h4>
<ul>
<li>Smart agriculture</li>
<li>Weather monitoring stations</li>
<li>IoT dashboards (via Blynk, ThingSpeak, etc.)</li>
</ul>
<hr />
<h3><strong>Advantages and Limitations of LM35 Sensor</strong></h3>
<h4><strong>Benefits of Using LM35</strong></h4>
<ul>
<li>Linear and accurate output</li>
<li>Low cost and easy to interface</li>
<li>No external calibration needed</li>
</ul>
<h4><strong>Limitations and Alternatives</strong></h4>
<ul>
<li>Analog-only output (needs ADC)</li>
<li>Not waterproof</li>
<li>Alternatives: DHT11, DS18B20, TMP36</li>
</ul>
<hr />
<h3><strong>FAQs: How to Use LM35 Temperature Sensor with Arduino and ESP32</strong></h3>
<p><strong>1. What voltage does LM35 require?</strong><br />
It operates between <strong>4V and 30V</strong>, commonly powered by 5V (Arduino) or 3.3V (ESP32).</p>
<p><strong>2. Can I use LM35 with ESP8266 or Raspberry Pi?</strong><br />
Yes, but ensure you use analog-to-digital conversion, as some boards (like ESP8266) have limited ADC support.</p>
<p><strong>3. Why are my readings unstable?</strong><br />
Add a <strong>capacitor (0.1 µF)</strong> across output and ground to filter noise.</p>
<p><strong>4. How accurate is the LM35 sensor?</strong><br />
±0.5°C at room temperature and ±1°C across a wide range.</p>
<p><strong>5. Can LM35 measure below 0°C?</strong><br />
Yes, but output voltage may go negative, requiring offset circuitry.</p>
<p><strong>6. Which IoT platforms support LM35 + ESP32 projects?</strong><br />
Popular ones include <strong>Blynk</strong>, <strong>ThingSpeak</strong>, and <strong>Google Firebase</strong>.</p>
<hr />
<h3><strong>Conclusion</strong></h3>
<p>The <strong>LM35 temperature sensor</strong> is a simple, reliable, and precise device for temperature measurement and monitoring. Whether you’re using <strong>Arduino for basic sensor readings</strong> or <strong>ESP32 for IoT web dashboards</strong>, the LM35 offers versatility and accuracy.</p>
<p>Experimenting with LM35 is an excellent way to start learning about <strong>analog sensors, ADC conversion, and IoT data visualization</strong>.</p>
<p>For more information, visit the <a class="decorated-link cursor-pointer" target="_new" rel="noopener">Texas Instruments LM35 Datasheet</a>.</p>
<p>The post <a href="https://www.teachmemicro.com/how-to-use-lm35-temperature-sensor-with-arduino-and-esp32-complete-guide-with-example-projects/">How to Use LM35 Temperature Sensor with Arduino and ESP32: Complete Guide with Example Projects</a> appeared first on <a href="https://www.teachmemicro.com">Teach Me Microcontrollers!</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Arduino Digital Tug of War</title>
		<link>https://www.teachmemicro.com/arduino-digital-tug-of-war/</link>
		
		<dc:creator><![CDATA[Roland Pelayo]]></dc:creator>
		<pubDate>Mon, 06 Jan 2025 01:00:27 +0000</pubDate>
				<category><![CDATA[Arduino Projects]]></category>
		<guid isPermaLink="false">https://www.teachmemicro.com/?p=7597</guid>

					<description><![CDATA[<p>In this project, we'll build a fun "Tug of War" game using an Arduino, a LED bar graph display, and buttons. The game mimics a classic tug-of-war, where two players compete to press their button rapidly to pull the "rope" (represented by a LED) to their side. Components Needed Arduino Uno (or similar) – 1 &#8230;</p>
<p>The post <a href="https://www.teachmemicro.com/arduino-digital-tug-of-war/">Arduino Digital Tug of War</a> appeared first on <a href="https://www.teachmemicro.com">Teach Me Microcontrollers!</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><span style="font-weight: 400;">In this project, we'll build a fun "Tug of War" game using an </span><b>Arduino</b><span style="font-weight: 400;">, a </span><b>LED bar graph display</b><span style="font-weight: 400;">, and </span><b>buttons</b><span style="font-weight: 400;">. The game mimics a classic tug-of-war, where two players compete to press their button rapidly to pull the "rope" (represented by a LED) to their side.</span></p>
<p><span id="more-7597"></span></p>
<h3><b>Components Needed</b></h3>
<ul>
<li style="font-weight: 400;" aria-level="1"><span style="font-weight: 400;">Arduino Uno (or similar) – 1</span></li>
<li style="font-weight: 400;" aria-level="1"><span style="font-weight: 400;">LED bar graph display (10-segment) – 1</span></li>
<li style="font-weight: 400;" aria-level="1"><span style="font-weight: 400;">Push buttons – 2</span></li>
<li style="font-weight: 400;" aria-level="1"><span style="font-weight: 400;">Resistors (10kΩ for pull-downs) – 2</span></li>
<li style="font-weight: 400;" aria-level="1"><span style="font-weight: 400;">Resistors (220Ω or 330Ω for LEDs) – 10</span></li>
<li style="font-weight: 400;" aria-level="1"><span style="font-weight: 400;">Breadboard – 1</span></li>
<li style="font-weight: 400;" aria-level="1"><span style="font-weight: 400;">Jumper wires</span></li>
</ul>
<h3><b>Wiring Diagram</b></h3>
<table>
<tbody>
<tr>
<td><b>Component</b></td>
<td><b>Arduino Pin</b></td>
<td><b>Notes</b></td>
</tr>
<tr>
<td><span style="font-weight: 400;">LED bar graph pins</span></td>
<td><span style="font-weight: 400;">D2–D11</span></td>
<td><span style="font-weight: 400;">Each pin controls one LED</span></td>
</tr>
<tr>
<td><span style="font-weight: 400;">Button 1</span></td>
<td><span style="font-weight: 400;">D12</span></td>
<td><span style="font-weight: 400;">Player 1's button</span></td>
</tr>
<tr>
<td><span style="font-weight: 400;">Button 2</span></td>
<td><span style="font-weight: 400;">D13</span></td>
<td><span style="font-weight: 400;">Player 2's button</span></td>
</tr>
<tr>
<td><span style="font-weight: 400;">10kΩ resistors</span></td>
<td><span style="font-weight: 400;">GND</span></td>
<td><span style="font-weight: 400;">Pull-down for buttons</span></td>
</tr>
<tr>
<td><span style="font-weight: 400;">220Ω resistors</span></td>
<td><span style="font-weight: 400;">Series with LEDs</span></td>
<td><span style="font-weight: 400;">Protect LEDs</span></td>
</tr>
</tbody>
</table>
<ol>
<li style="font-weight: 400;" aria-level="1"><b>LED Bar Graph</b><span style="font-weight: 400;">:</span>
<ul>
<li style="font-weight: 400;" aria-level="2"><span style="font-weight: 400;">Connect the cathodes of the LEDs to GND through a 220Ω resistor.</span></li>
<li style="font-weight: 400;" aria-level="2"><span style="font-weight: 400;">Connect each anode to Arduino pins D2 to D11.</span></li>
</ul>
</li>
<li style="font-weight: 400;" aria-level="1"><b>Buttons</b><span style="font-weight: 400;">:</span>
<ul>
<li style="font-weight: 400;" aria-level="2"><span style="font-weight: 400;">Connect one leg of each button to GND through a 10kΩ resistor (pull-down configuration).</span></li>
<li style="font-weight: 400;" aria-level="2"><span style="font-weight: 400;">Connect the other leg to D12 (Player 1) and D13 (Player 2).</span></li>
</ul>
</li>
</ol>
<h3><b>Gameplay Overview</b></h3>
<ul>
<li style="font-weight: 400;" aria-level="1"><span style="font-weight: 400;">A "marker" (one lit LED) starts in the middle of the LED bar graph.</span></li>
<li style="font-weight: 400;" aria-level="1"><span style="font-weight: 400;">Players press their respective buttons to move the marker toward their end.</span></li>
<li style="font-weight: 400;" aria-level="1"><span style="font-weight: 400;">The game ends when the marker reaches one side, indicating the winner.</span></li>
</ul>
<h3><b>Code</b></h3>
<p><span style="font-weight: 400;">Here's the Arduino sketch for the Tug of War game:</span></p>
<div class="hcb_wrap">
<pre class="prism undefined-numbers lang-cpp" data-lang="C++"><pre><code class="language-cpp">// Tug of War Game for Arduino

const int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; // LED pins
const int numLeds = 10; // Number of LEDs
const int button1Pin = 12; // Player 1 button
const int button2Pin = 13; // Player 2 button

int markerPosition = numLeds / 2; // Start in the middle

void setup() {
   // Initialize LED pins as output
   for (int i = 0; i &lt; numLeds; i++) {
       pinMode(ledPins[i], OUTPUT);
   }

   // Initialize button pins as input
   pinMode(button1Pin, INPUT);
   pinMode(button2Pin, INPUT);

   // Light up the starting LED
   updateLeds();
}

void loop() {
   // Read button states
   int button1State = digitalRead(button1Pin);
   int button2State = digitalRead(button2Pin);

   // Check if Player 1 presses their button
   if (button1State == HIGH) {
      markerPosition = max(0, markerPosition - 1); // Move left
      updateLeds();
      delay(200); // Debounce delay
   }

   // Check if Player 2 presses their button
   if (button2State == HIGH) {
      markerPosition = min(numLeds - 1, markerPosition + 1); // Move right
      updateLeds();
      delay(200); // Debounce delay
   }

   // Check for win condition
   if (markerPosition == 0) {
      gameOver(1); // Player 1 wins
   } else if (markerPosition == numLeds - 1) {
      gameOver(2); // Player 2 wins
   }
}

void updateLeds() {
  for (int i = 0; i &lt; numLeds; i++) {
     digitalWrite(ledPins[i], i == markerPosition ? HIGH : LOW);
  }
}


void gameOver(int winner) {
   for (int i = 0; i &lt; 5; i++) {
   
      // Flash LEDs to indicate the winner
      for (int j = 0; j &lt; numLeds; j++) {
         digitalWrite(ledPins[j], HIGH);
      }

      delay(200);
      for (int j = 0; j &lt; numLeds; j++) {
         digitalWrite(ledPins[j], LOW);
      }

      delay(200);
   }

   // Display winner LED permanently
   if (winner == 1) {
      digitalWrite(ledPins[0], HIGH); // Player 1 wins
   } else {
      digitalWrite(ledPins[numLeds - 1], HIGH); // Player 2 wins
   }

   while (true) {} // Stop the game
}</code></pre></pre>
</div>
<h3><b>Explanation</b></h3>
<ol>
<li><b>LED Control</b><span style="font-weight: 400;">:</span>
<ul>
<li><span style="font-weight: 400;">The </span><em><span style="font-weight: 400;">updateLeds()</span></em><span style="font-weight: 400;"> function ensures only one LED (the marker) is lit at any time, based on the </span><em><span style="font-weight: 400;">markerPosition</span></em><span style="font-weight: 400;">.</span></li>
</ul>
</li>
<li><b>Button Handling</b><span style="font-weight: 400;">:</span>
<ul>
<li><span style="font-weight: 400;">The loop checks if either button is pressed and adjusts the marker's position accordingly.</span></li>
<li><span style="font-weight: 400;">A debounce delay is added to prevent multiple detections from a single press.</span></li>
</ul>
</li>
<li><b>Win Condition</b><span style="font-weight: 400;">:</span>
<ul>
<li><span style="font-weight: 400;">If the marker reaches the first or last LED, the game ends, and the LEDs flash to indicate the winner.</span></li>
</ul>
</li>
<li><b>Game Over</b><span style="font-weight: 400;">:</span>
<ul>
<li><span style="font-weight: 400;">After flashing, the winner's LED stays lit, and the game stops.</span></li>
</ul>
</li>
</ol>
<h3><b>Testing and Adjustments</b></h3>
<ul>
<li style="font-weight: 400;" aria-level="1"><span style="font-weight: 400;">Ensure proper wiring of LEDs and buttons before uploading the code.</span></li>
<li style="font-weight: 400;" aria-level="1"><span style="font-weight: 400;">Adjust the debounce delay (</span><span style="font-weight: 400;">200 ms</span><span style="font-weight: 400;">) if buttons feel too slow or too fast.</span></li>
<li style="font-weight: 400;" aria-level="1"><span style="font-weight: 400;">Try increasing the number of LEDs for a longer game.</span></li>
</ul>
<p>The post <a href="https://www.teachmemicro.com/arduino-digital-tug-of-war/">Arduino Digital Tug of War</a> appeared first on <a href="https://www.teachmemicro.com">Teach Me Microcontrollers!</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Arduino Parking Sensor Project</title>
		<link>https://www.teachmemicro.com/arduino-parking-sensor-project/</link>
		
		<dc:creator><![CDATA[Roland Pelayo]]></dc:creator>
		<pubDate>Fri, 11 Oct 2024 01:00:59 +0000</pubDate>
				<category><![CDATA[Arduino Projects]]></category>
		<guid isPermaLink="false">https://www.teachmemicro.com/?p=7373</guid>

					<description><![CDATA[<p>Parking in tight spaces can be challenging, especially with limited visibility. To help avoid collisions with obstacles, this project will show you how to build an Arduino Parking Sensor using an ultrasonic sensor and a buzzer. The ultrasonic sensor detects the distance to an obstacle, and the buzzer produces warning beeps, which become faster as &#8230;</p>
<p>The post <a href="https://www.teachmemicro.com/arduino-parking-sensor-project/">Arduino Parking Sensor Project</a> appeared first on <a href="https://www.teachmemicro.com">Teach Me Microcontrollers!</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><span style="font-weight: 400;">Parking in tight spaces can be challenging, especially with limited visibility. To help avoid collisions with obstacles, this project will show you how to build an </span><b>Arduino Parking Sensor</b><span style="font-weight: 400;"> using an ultrasonic sensor and a buzzer. The ultrasonic sensor detects the distance to an obstacle, and the buzzer produces warning beeps, which become faster as the object gets closer.</span></p>
<p><span id="more-7373"></span></p>
<h2><b>Project Overview</b></h2>
<p><span style="font-weight: 400;">In this project, we'll use the following components:</span></p>
<ul>
<li style="font-weight: 400;" aria-level="1"><b>Arduino UNO (or similar) board</b><span style="font-weight: 400;">: The brain of the system.</span></li>
<li style="font-weight: 400;" aria-level="1"><a href="https://www.teachmemicro.com/hc-sr04-arduino-interfacing/"><b>Ultrasonic sensor (HC-SR04)</b></a><span style="font-weight: 400;">: Measures the distance to an object by sending out ultrasonic waves and calculating the time it takes for the echo to return.</span></li>
<li style="font-weight: 400;" aria-level="1"><b>Buzzer</b><span style="font-weight: 400;">: Produces beeping sounds that speed up as the distance to an obstacle decreases.</span></li>
</ul>
<h3><b>How It Works</b></h3>
<p><span style="font-weight: 400;">The ultrasonic sensor sends a pulse and waits for it to bounce back from an obstacle. Based on the time taken for the pulse to return, the Arduino calculates the distance to the object. Depending on the distance, the buzzer emits beeps at different rates — the closer the object, the faster the beeping.</span></p>
<p>When the trigger pin is held high, the ultrasonic sensor module begins emitting a signal until that pin is pulled low. If the signal encounters an obstacle, it will bounce back to the sensor. As a result, the "echo" of the signal is used to measure the distance: the duration between the sending of the signal and the return of the signal can be read on the echo pin as a pulse.</p>
<p style="text-align: left;">The distance in centimeters between the HC-SR04 and the obstacle can then be calculated using</p>
<p style="text-align: center;"><img decoding="async" src="https://latex.codecogs.com/gif.latex?d%20%3D%20%5Cfrac%7Bt%7D%7B2%7D%20*%200.034" /></p>
<p>where 0.034 is the speed of sound: 340 m/s</p>
<p><span style="font-weight: 400;">This system mimics the parking sensors commonly found in modern cars, providing audible feedback to the driver as they approach a barrier or obstacle.</span></p>
<h2><b>Components Needed</b></h2>
<ol>
<li style="font-weight: 400;" aria-level="1"><b>Arduino UNO or any other Arduino board</b></li>
<li style="font-weight: 400;" aria-level="1"><b>Ultrasonic sensor (HC-SR04)</b></li>
<li style="font-weight: 400;" aria-level="1"><b>Active Buzzer</b></li>
<li style="font-weight: 400;" aria-level="1"><b>Jumper wires</b></li>
<li style="font-weight: 400;" aria-level="1"><b>Breadboard</b></li>
<li style="font-weight: 400;" aria-level="1"><b>USB cable</b><span style="font-weight: 400;"> (to connect Arduino to a computer)</span></li>
<li style="font-weight: 400;" aria-level="1"><b>Power source</b><span style="font-weight: 400;"> (if needed)</span></li>
</ol>
<h2><b>Circuit Diagram</b></h2>
<p><span style="font-weight: 400;">To set up the Arduino parking sensor, connect the components as follows:</span></p>
<ul>
<li style="font-weight: 400;" aria-level="1"><b>Ultrasonic Sensor (HC-SR04)</b><span style="font-weight: 400;">:</span>
<ul>
<li style="font-weight: 400;" aria-level="2"><b>VCC</b><span style="font-weight: 400;"> to Arduino </span><b>5V</b></li>
<li style="font-weight: 400;" aria-level="2"><b>GND</b><span style="font-weight: 400;"> to Arduino </span><b>GND</b></li>
<li style="font-weight: 400;" aria-level="2"><b>Trig</b><span style="font-weight: 400;"> to Arduino </span><b>Pin 9</b></li>
<li style="font-weight: 400;" aria-level="2"><b>Echo</b><span style="font-weight: 400;"> to Arduino </span><b>Pin 10</b></li>
</ul>
</li>
<li style="font-weight: 400;" aria-level="1"><b>Buzzer</b><span style="font-weight: 400;">:</span>
<ul>
<li style="font-weight: 400;" aria-level="2"><b>Positive terminal</b><span style="font-weight: 400;"> to Arduino </span><b>Pin 11</b></li>
<li style="font-weight: 400;" aria-level="2"><b>Negative terminal</b><span style="font-weight: 400;"> to Arduino </span><b>GND</b></li>
</ul>
</li>
</ul>
<h2><b>Code</b></h2>
<p><span style="font-weight: 400;">Here’s the Arduino sketch to make the parking sensor functional:</span></p>
<div class="hcb_wrap">
<pre class="prism undefined-numbers lang-cpp" data-lang="C++"><pre><code class="language-cpp">// Define pins for ultrasonic sensor and buzzer
const int trigPin = 9;
const int echoPin = 10;
const int buzzerPin = 11;

// Variables for distance calculation
long duration;
int distance;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  
  Serial.begin(9600); // Initialize serial communication for debugging
}


void loop() {
  // Clear the trigPin by setting it LOW
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
 
  // Trigger the sensor by setting trigPin HIGH for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the echoPin and calculate the duration
  duration = pulseIn(echoPin, HIGH);

  // Calculate distance in centimeters (speed of sound = 343 m/s)
  distance = duration * 0.034 / 2;

  // Print the distance for debugging purposes
  Serial.print(&quot;Distance: &quot;);
  Serial.println(distance);

  // Control the buzzer based on distance
  if (distance &lt;= 10) {
    // Very close, fast beeping
    tone(buzzerPin, 1000);
    delay(100);
    noTone(buzzerPin);
    delay(100);
  } else if (distance &lt;= 30) {
    // Medium distance, slower beeping
    tone(buzzerPin, 1000);
    delay(300);
    noTone(buzzerPin);
    delay(300);
  } else if (distance &lt;= 50) {
    // Farther away, even slower beeping
    tone(buzzerPin, 1000);
    delay(500);
    noTone(buzzerPin);
    delay(500);
  } else {
    // No obstacle nearby, no beeping
    noTone(buzzerPin);
  }

  delay(100); // Short delay before the next reading
}</code></pre></pre>
</div>
<h3><b>Code Explanation</b></h3>
<ol>
<li style="font-weight: 400;" aria-level="1"><b>Pin Setup</b><span style="font-weight: 400;">:</span>
<ul>
<li style="font-weight: 400;" aria-level="2"><i><span style="font-weight: 400;">trigPin</span></i><span style="font-weight: 400;"> sends the ultrasonic pulse.</span></li>
<li style="font-weight: 400;" aria-level="2"><i><span style="font-weight: 400;">echoPin</span></i><span style="font-weight: 400;"> receives the reflected pulse.</span></li>
<li style="font-weight: 400;" aria-level="2"><i><span style="font-weight: 400;">buzzerPin</span></i><span style="font-weight: 400;"> controls the buzzer sound.</span></li>
</ul>
</li>
<li style="font-weight: 400;" aria-level="1"><b>Distance Calculation</b><span style="font-weight: 400;">:</span>
<ul>
<li style="font-weight: 400;" aria-level="2"><span style="font-weight: 400;">The ultrasonic sensor sends out a pulse from the </span><i><span style="font-weight: 400;">trigPin</span></i><span style="font-weight: 400;">, and the time it takes for the pulse to bounce back is measured via the </span><i><span style="font-weight: 400;">echoPin</span></i><span style="font-weight: 400;">. The duration is used to calculate the distance in centimeters.</span></li>
</ul>
</li>
<li style="font-weight: 400;" aria-level="1"><b>Buzzer Control</b><span style="font-weight: 400;">:</span>
<ul>
<li style="font-weight: 400;" aria-level="2"><span style="font-weight: 400;">If the distance is less than or equal to 10 cm, the buzzer emits fast beeps.</span></li>
<li style="font-weight: 400;" aria-level="2"><span style="font-weight: 400;">If the distance is between 10 and 30 cm, the beeps are slower.</span></li>
<li style="font-weight: 400;" aria-level="2"><span style="font-weight: 400;">At distances between 30 and 50 cm, the beeping is even slower.</span></li>
<li style="font-weight: 400;" aria-level="2"><span style="font-weight: 400;">Beyond 50 cm, the buzzer remains silent.</span></li>
</ul>
</li>
</ol>
<h3><b>Modifying the Code</b></h3>
<p><span style="font-weight: 400;">You can adjust the distance thresholds or beep speed to suit your needs. For example, if you want to increase the beeping rate for farther distances, you can modify the </span><i><span style="font-weight: 400;">if</span></i><span style="font-weight: 400;"> statements and </span><i><span style="font-weight: 400;">delay()</span></i><span style="font-weight: 400;"> values accordingly.</span></p>
<h2><b>Assembly Instructions</b></h2>
<ol>
<li style="font-weight: 400;" aria-level="1"><span style="font-weight: 400;">Connect the ultrasonic sensor to the Arduino using jumper wires as described in the circuit diagram.</span></li>
<li style="font-weight: 400;" aria-level="1"><span style="font-weight: 400;">Connect the buzzer to the Arduino.</span></li>
<li style="font-weight: 400;" aria-level="1"><span style="font-weight: 400;">Upload the code to your Arduino using the Arduino IDE.</span></li>
<li style="font-weight: 400;" aria-level="1"><span style="font-weight: 400;">Power the Arduino using a USB cable or external power supply.</span></li>
</ol>
<h2><b>Testing the System</b></h2>
<p><span style="font-weight: 400;">After assembling the circuit and uploading the code, place the sensor in front of a wall or another obstacle. As you move the obstacle closer, you should hear the buzzer beeping faster.</span></p>
<p><span style="font-weight: 400;">If the sensor does not detect the object correctly, check the wiring of the ultrasonic sensor and ensure the pins are connected properly.</span></p>
<h2><b>Applications</b></h2>
<ul>
<li style="font-weight: 400;" aria-level="1"><b>Car parking assist</b><span style="font-weight: 400;">: A small, low-cost system to help drivers detect nearby objects when parking.</span></li>
<li style="font-weight: 400;" aria-level="1"><b>Garage parking</b><span style="font-weight: 400;">: Mount this system in your garage to detect the position of your car and ensure it’s parked safely.</span></li>
<li style="font-weight: 400;" aria-level="1"><b>Object detection</b><span style="font-weight: 400;">: Useful in robotics or automation projects to detect the presence of nearby objects.</span></li>
</ul>
<h2><b>Conclusion</b></h2>
<p><span style="font-weight: 400;">This Arduino Parking Sensor project is a great way to understand how ultrasonic sensors work and how they can be integrated into real-world applications. You now have a functional parking sensor system that mimics the capabilities of modern car parking assistance technologies. With a few tweaks, you can expand this project further by adding visual feedback using LEDs or an LCD screen. Happy tinkering!</span></p>
<p>The post <a href="https://www.teachmemicro.com/arduino-parking-sensor-project/">Arduino Parking Sensor Project</a> appeared first on <a href="https://www.teachmemicro.com">Teach Me Microcontrollers!</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Arduino Flappy Bird Dot Matrix Project</title>
		<link>https://www.teachmemicro.com/arduino-flappy-bird-dot-matrix-project/</link>
		
		<dc:creator><![CDATA[Roland Pelayo]]></dc:creator>
		<pubDate>Mon, 22 Jan 2024 02:45:08 +0000</pubDate>
				<category><![CDATA[Arduino Projects]]></category>
		<guid isPermaLink="false">https://www.teachmemicro.com/?p=7034</guid>

					<description><![CDATA[<p>Start your DIY gaming experience with this "Arduino Flappy Bird Project with Dot Matrix." This project merges the timeless appeal of Flappy Bird with the dynamic capabilities of Arduino and a Dot Matrix display. This article includes hardware setup, gameplay mechanics, and of course, the source code. This project is a simple clone of the &#8230;</p>
<p>The post <a href="https://www.teachmemicro.com/arduino-flappy-bird-dot-matrix-project/">Arduino Flappy Bird Dot Matrix Project</a> appeared first on <a href="https://www.teachmemicro.com">Teach Me Microcontrollers!</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Start your DIY gaming experience with this "<em>Arduino Flappy Bird Project with Dot Matrix</em>." This project merges the timeless appeal of Flappy Bird with the dynamic capabilities of Arduino and a <a href="https://www.teachmemicro.com/max7219-cascaded-dot-matrix-module/">Dot Matrix display</a>. This article includes hardware setup, gameplay mechanics, and of course, the source code.</p>
<p><span id="more-7034"></span></p>
<p>This project is a simple clone of the popular Flappy Bird game. The original app isn't available in the app store anymore, although there are a lot of clones. Here is the original:</p>
<p><img loading="lazy" decoding="async" src="https://www.teachmemicro.com/wp-content/uploads/2024/01/original_flappy_bird.gif" alt="" width="500" height="500" class="size-large wp-image-7039 aligncenter" /></p>
<p>The objective of the game, for those who are unfamiliar, is to control the bird as it avoids “pipes”. The amusing part is that the bird jumps in the air with every push of the button and falls when left unattended, making it harder to avoid the pipes.</p>
<p>This is a good starting project for those who want to dive into game design. You’ll be familiar with two-dimensional coordinates, randomization, and button handling.</p>
<p>ChatGPT suggested the idea for this game. Then I tried googling for any similar project that uses a dot matrix. To my surprise, there was none so that affirmed my decision to make one.</p>
<h3><strong>Bill of Materials</strong></h3>
<p>1. <a href="https://amzn.to/426R00E">Arduino board</a><br />
2. <a href="https://amzn.to/48Cf79Y">Dot Matrix display</a><br />
3. <a href="https://amzn.to/429RjYt">Buzzer</a></p>
<p>Any Arduino board with an ATMega microcontroller can be used in this project. The dot matrix orientation is such that the DIN pin is on top. A buzzer or piezo speaker can be optionally added for simple game sounds.</p>
<h3><strong>Wiring diagram</strong></h3>
<p><img loading="lazy" decoding="async" src="https://www.teachmemicro.com/wp-content/uploads/2024/01/arduino-flappy-bird-wiring-diagram.png" alt="Arduino Flappy Bird Wiring Diagram" width="576" height="433" class="aligncenter wp-image-7036 size-full" srcset="https://www.teachmemicro.com/wp-content/uploads/2024/01/arduino-flappy-bird-wiring-diagram.png 576w, https://www.teachmemicro.com/wp-content/uploads/2024/01/arduino-flappy-bird-wiring-diagram-300x226.png 300w" sizes="auto, (max-width: 576px) 100vw, 576px" /></p>
<style type="text/css">
.tg  {border-collapse:collapse;border-color:#9ABAD9;border-spacing:0;}<br />.tg td{background-color:#EBF5FF;border-color:#9ABAD9;border-style:solid;border-width:0px;color:#444;<br />  font-family:Arial, sans-serif;font-size:14px;overflow:hidden;padding:10px 5px;word-break:normal;}<br />.tg th{background-color:#409cff;border-color:#9ABAD9;border-style:solid;border-width:0px;color:#fff;<br />  font-family:Arial, sans-serif;font-size:14px;font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}<br />.tg .tg-1wig{font-weight:bold;text-align:left;vertical-align:top}<br />.tg .tg-baqh{text-align:center;vertical-align:top}<br /></style>
<table class="tg">
<thead>
<tr>
<th class="tg-1wig">Arduino UNO</th>
<th class="tg-1wig">Dot Matrix (MAX7219)</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tg-baqh">11</td>
<td class="tg-baqh">DIN</td>
</tr>
<tr>
<td class="tg-baqh">9</td>
<td class="tg-baqh">CS</td>
</tr>
<tr>
<td class="tg-baqh">13</td>
<td class="tg-baqh">CLK</td>
</tr>
<tr>
<td class="tg-baqh">5V</td>
<td class="tg-baqh">VCC</td>
</tr>
<tr>
<td class="tg-baqh">GND</td>
<td class="tg-baqh">GND</td>
</tr>
</tbody>
</table>
<h3><strong>Gameplay</strong></h3>
<p>The main screen is a simple marquee of the text “Arduino Flappy Bird”. The game starts when the user presses the button. The “bird” starts in the middle and traverses left to right. Obstacles or pipes appear in its path with the passage location randomly generated. The player scores a point every time the bird passes a pipe. Thus, the player must press the button correctly to avoid a pipe. The game is over if the bird hits a pipe or the ground.</p>
<p><img loading="lazy" decoding="async" src="https://www.teachmemicro.com/wp-content/uploads/2024/01/dot-matrix-animation.gif" alt="Arduino flappy bird animation" width="496" height="503" class="size-full wp-image-7037 aligncenter" /></p>
<p>Each frame takes 1 second to execute. As one frame passes, so does one movement of the bird and the pipe. The bird may move up two pixels when the button is pressed or down until the button is pressed. Similarly, the pipe moves 1-pixel position from right to left.</p>
<p>The button press serves as an external interrupt. In the interrupt service routine, a flag is either set or clear depending on the current state of the bird. If the bird is falling, then the flag is set, indicating that the bird is now rising. When the bird is already rising, the flag clears to indicate the bird is falling. This flag is continuously checked inside the loop to create the appropriate bird movement.</p>
<h3><strong>Code/Sketch</strong></h3>
<p>The full code can be downloaded from <a href="https://github.com/kurimawxx00/arduino-flappy-bird">this repository</a>.</p>
<h3><strong>Video</strong><br />
<iframe loading="lazy" width="640" height="480" src="https://drive.google.com/file/d/1Z8ghXlI3PP5Jyhxuuj7Q5nPsUBbk8bw8/preview" allow="autoplay"></iframe></h3>
<p>The post <a href="https://www.teachmemicro.com/arduino-flappy-bird-dot-matrix-project/">Arduino Flappy Bird Dot Matrix Project</a> appeared first on <a href="https://www.teachmemicro.com">Teach Me Microcontrollers!</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Dot Matrix Droplets Project</title>
		<link>https://www.teachmemicro.com/led-dot-matrix-droplets/</link>
					<comments>https://www.teachmemicro.com/led-dot-matrix-droplets/#comments</comments>
		
		<dc:creator><![CDATA[Roland Pelayo]]></dc:creator>
		<pubDate>Fri, 19 Jun 2020 08:59:41 +0000</pubDate>
				<category><![CDATA[Arduino Projects]]></category>
		<guid isPermaLink="false">https://www.teachmemicro.com/?p=4553</guid>

					<description><![CDATA[<p>This project makes the “dots” on a dot matrix look like they are pebbles in a container. Tilt the device on one side and the dots fall toward that side. The same goes if the device tilts to the other side. Materials Arduino (Uno/Nano/Micro) Single LED Dot Matrix MPU6050 Breakout Board Breadboard, connecting wires Theory/Concept &#8230;</p>
<p>The post <a href="https://www.teachmemicro.com/led-dot-matrix-droplets/">Dot Matrix Droplets Project</a> appeared first on <a href="https://www.teachmemicro.com">Teach Me Microcontrollers!</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>This project makes the “dots” on a dot matrix look like they are pebbles in a container. Tilt the device on one side and the dots fall toward that side. The same goes if the device tilts to the other side.</p>
<p><img loading="lazy" decoding="async" class="aligncenter" src="https://www.teachmemicro.com/wp-content/uploads/2020/06/joined_video_6c8f0dee524744dfae98b2ccc2a47abc.00_00_12_29.Still001.jpg" width="531" height="531" /></p>
<p><span id="more-4553"></span></p>
<h3><strong>Materials</strong></h3>
<ul>
<li>Arduino (Uno/Nano/Micro)</li>
<li>Single <a href="https://www.win-source.net/category/optoelectronics/display-modules/display-modules-led-dot-matrix-and-cluster">LED Dot Matrix</a></li>
<li>MPU6050 Breakout Board</li>
<li>Breadboard, connecting wires</li>
</ul>
<h3><strong>Theory/Concept</strong></h3>
<p>The dots in a single <a href="https://www.teachmemicro.com/max7219-cascaded-dot-matrix-module/">LED matrix</a> follow a coordinate system, with the horizontal the x-axis and the vertical the y-axis. You can set your own origin point by mounting the dot matrix whichever your like; In my case, the origin point is the upper right corner.</p>
<p><img loading="lazy" decoding="async" class="aligncenter" src="https://www.teachmemicro.com/wp-content/uploads/2020/06/dot-matrix-coordinate.png" alt="LED dot matrix coordinates" width="581" height="696" /></p>
<p>The device may only tilt to the left or to the right. This means it’s only tilting on one axis.</p>
<p>I used an <a href="https://www.teachmemicro.com/orientation-arduino-mpu6050/">MPU6050 accelerometer + gyroscope</a> to detect the movement. <a href="https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/MPU6050">Jeff Rowberg’s library</a> provides examples on how to measure the yaw, pitch and roll movements of the device. This is again, depending on how you mount the MPU6050 against the dot matrix. On my setup, I only need to read the roll of the MPU6050.</p>
<p>It’s much easier to analyze first if I just select a single row of dots. Then I divided the movements into two steps. First is from the initial position to the 45 inclined position:</p>
<p><img loading="lazy" decoding="async" class="aligncenter" src="https://www.teachmemicro.com/wp-content/uploads/2020/06/initial-position.png" alt="LED Dot matrix first position" width="303" height="418" /><img loading="lazy" decoding="async" class="aligncenter" src="https://www.teachmemicro.com/wp-content/uploads/2020/06/2nd-position.png" alt="LED dot matrix 2nd position" width="298" height="418" /></p>
<p>The rightmost dot would have to move four positions to get to its final position on the 2nd image. The second from the right would have to move three positions and so on. The farther the dot from the center, the greater the distance it has to travel to get to its final position.</p>
<p>My algorithm is to set the movements of the dots against their initial position, using Arduino’s map() function:</p>
<pre class="lang:arduino decode:true "><pre><code class="language-cpp">for(int i=0;i&lt;8;i++){
  int a = u[i][1]+ u[i][0] - 4;
  m[i] = map(angle,90,45,u[i][1],a);
  n[i] = u[i][0];
  for(int j=0;j&lt;8;j++){
    lmd.setPixel(m[i]+j,n[i],true);
  }
}</code></pre></pre>
<p>Here, <em>setPixel()</em> is a function that turns on a dot in a specific coordinate. This function is from <a href="https://github.com/bartoszbielawski/LEDMatrixDriver"><em>LedMatrixDriver library</em></a> I am using for this project.</p>
<p>The second step is from the 45 degree inclined position to the upright position:</p>
<p><img loading="lazy" decoding="async" class="aligncenter" src="https://www.teachmemicro.com/wp-content/uploads/2020/06/2nd-position.png" width="298" height="418" /><img loading="lazy" decoding="async" class="aligncenter" src="https://www.teachmemicro.com/wp-content/uploads/2020/06/final-position.png" width="295" height="418" /></p>
<p>Here, instead of moving up in the y-axis, the dots need to move right in the x-axis to reach their final destination.</p>
<p>The algorithm now is:</p>
<pre class="lang:arduino decode:true "><pre><code class="language-cpp">for(int i=0;i&lt;8;i++){
  p[i] = map(angle,45,0,u[i][0],4);
  q[i] = u[i][0];
  for(int j=1;j&lt;8;j++){
    lmd.setPixel(q[i],p[i]-j,true); 
  }
}</code></pre></pre>
<p>To fill up the dot matrix, I just increment/decrement the position of the line of dots:</p>
<p><img loading="lazy" decoding="async" class="aligncenter" src="https://www.teachmemicro.com/wp-content/uploads/2020/06/initial-position-multi.png" width="344" height="419" /><img loading="lazy" decoding="async" class="aligncenter" src="https://www.teachmemicro.com/wp-content/uploads/2020/06/2nd-position-multi.png" width="341" height="418" /><img loading="lazy" decoding="async" class="aligncenter" src="https://www.teachmemicro.com/wp-content/uploads/2020/06/final-position-multi.png" width="298" height="422" /></p>
<p>The same concept follows if the device is tilted to the left.</p>
<h3><strong>Wiring</strong></h3>
<p><img decoding="async" class="aligncenter" src="https://www.teachmemicro.com/wp-content/uploads/2020/06/dot-matrix-droplets-wiring.jpg" /></p>
<style type="text/css">
.tg  {border-collapse:collapse;border-spacing:0;}<br />.tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;<br />  overflow:hidden;padding:10px 5px;word-break:normal;}<br />.tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;<br />  font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}<br />.tg .tg-0lax{text-align:left;vertical-align:top}<br /></style>
<table class="tg">
<thead>
<tr>
<th class="tg-0lax">Arduino UNO</th>
<th class="tg-0lax">MPU6050</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tg-0lax">A4</td>
<td class="tg-0lax">SDA</td>
</tr>
<tr>
<td class="tg-0lax">A5</td>
<td class="tg-0lax">SCL</td>
</tr>
<tr>
<td class="tg-0lax">D2</td>
<td class="tg-0lax">INT</td>
</tr>
<tr>
<td class="tg-0lax">5V</td>
<td class="tg-0lax">VCC</td>
</tr>
<tr>
<td class="tg-0lax">GND</td>
<td class="tg-0lax">GND</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<style type="text/css">
.tg  {border-collapse:collapse;border-color:#9ABAD9;border-spacing:0;}<br />.tg td{background-color:#EBF5FF;border-color:#9ABAD9;border-style:solid;border-width:0px;color:#444;<br />  font-family:Arial, sans-serif;font-size:14px;overflow:hidden;padding:10px 5px;word-break:normal;}<br />.tg th{background-color:#409cff;border-color:#9ABAD9;border-style:solid;border-width:0px;color:#fff;<br />  font-family:Arial, sans-serif;font-size:14px;font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}<br />.tg .tg-1wig{font-weight:bold;text-align:left;vertical-align:top}<br />.tg .tg-baqh{text-align:center;vertical-align:top}<br /></style>
<table class="tg">
<thead>
<tr>
<th class="tg-1wig">Arduino UNO</th>
<th class="tg-1wig">Dot Matrix (MAX7219)</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tg-baqh">11</td>
<td class="tg-baqh">DIN</td>
</tr>
<tr>
<td class="tg-baqh">9</td>
<td class="tg-baqh">CS</td>
</tr>
<tr>
<td class="tg-baqh">13</td>
<td class="tg-baqh">CLK</td>
</tr>
<tr>
<td class="tg-baqh">5V</td>
<td class="tg-baqh">VCC</td>
</tr>
<tr>
<td class="tg-baqh">GND</td>
<td class="tg-baqh">GND</td>
</tr>
</tbody>
</table>
<h3><strong>Sketch/Code</strong></h3>
<p>The code uses the <a href="https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/MPU6050">MPU6050 library from Jeff Rowberg</a> and the <a href="https://github.com/bartoszbielawski/LEDMatrixDriver">LedMatrixDriver library</a>. You can read my <a href="https://www.teachmemicro.com/orientation-arduino-mpu6050/">article on MPU6050</a> for some basic understanding of the accelerometer and gyroscope. I also wrote a tutorial for using the <a href="https://www.teachmemicro.com/max7219-cascaded-dot-matrix-module/">MAX7219 driver with the LED dot matrix</a>.</p>
<p>The full sketch/code is in my <a href="https://github.com/kurimawxx00/dot-matrix-droplets">repository</a>.</p>
<h3><strong>Output Video</strong></h3>
<p><iframe loading="lazy" width="560" height="315" src="https://www.youtube.com/embed/18lrUn5GK5Y" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="allowfullscreen"></iframe></p>
<p>I am still looking to improve this project, particularly adding gravity and momentum effects to make the movements more convincing.</p>
<p>If you have any questions, suggestions or reactions about this project, kindly drop a comment below!</p>
<p>The post <a href="https://www.teachmemicro.com/led-dot-matrix-droplets/">Dot Matrix Droplets Project</a> appeared first on <a href="https://www.teachmemicro.com">Teach Me Microcontrollers!</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.teachmemicro.com/led-dot-matrix-droplets/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>Clap Switch for RGB LEDs using Arduino</title>
		<link>https://www.teachmemicro.com/clap-switch-rgb-led-arduino/</link>
					<comments>https://www.teachmemicro.com/clap-switch-rgb-led-arduino/#respond</comments>
		
		<dc:creator><![CDATA[Roland Pelayo]]></dc:creator>
		<pubDate>Mon, 17 Feb 2020 04:10:55 +0000</pubDate>
				<category><![CDATA[Arduino Projects]]></category>
		<category><![CDATA[microphone]]></category>
		<category><![CDATA[rgb]]></category>
		<category><![CDATA[sound]]></category>
		<guid isPermaLink="false">https://www.teachmemicro.com/?p=4265</guid>

					<description><![CDATA[<p>Introduction This week’s Arduino project is a classic one and ideal for beginners. Here I will be showing how to build a simple clap switch for an RGB LED strip. Basically, a microphone captures the sound of the clap and converts it to a voltage. The Arduino processes that voltage and then changes the color &#8230;</p>
<p>The post <a href="https://www.teachmemicro.com/clap-switch-rgb-led-arduino/">Clap Switch for RGB LEDs using Arduino</a> appeared first on <a href="https://www.teachmemicro.com">Teach Me Microcontrollers!</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h3><strong>Introduction</strong></h3>
<p>This week’s Arduino project is a classic one and ideal for beginners. Here I will be showing how to build a simple clap switch for an RGB LED strip. Basically, a microphone captures the sound of the clap and converts it to a voltage. The Arduino processes that voltage and then changes the color of the RGB LED strip.</p>
<h3><strong>What You’ll Need</strong></h3>
<ul>
<li>Arduino <a href="https://www.win-source.net/category/integrated-circuits-ics/embedded/embedded-microcontrollers">Microcontroller</a> (<a href="https://s.click.aliexpress.com/e/_skx2d3">Nano</a> in this case but <a href="https://s.click.aliexpress.com/e/_sKPsMD">UNO</a> is possible too)</li>
<li><a href="https://s.click.aliexpress.com/e/_sDKbNr">Microphone breakout board</a></li>
<li><a href="https://s.click.aliexpress.com/e/_rQz7wD">Connecting wires (jumper)</a></li>
<li><a href="https://s.click.aliexpress.com/e/_sT31pB">RGB LED strip (12V)</a></li>
<li>3 x <a href="https://s.click.aliexpress.com/e/_smtTBL">2N2222A NPN Transistor</a></li>
<li><a href="https://s.click.aliexpress.com/e/_srmVID">12V power source</a></li>
</ul>
<h3><strong>Wiring Diagram</strong></h3>
<p><a href="https://www.teachmemicro.com/wp-content/uploads/2020/02/arduino-clap-switch-RGB_bb-2.jpg"><img loading="lazy" decoding="async" class="aligncenter size-large wp-image-4268" src="https://www.teachmemicro.com/wp-content/uploads/2020/02/arduino-clap-switch-RGB_bb-2-1024x464.jpg" alt="Arduino clap switch for RGB " width="640" height="290" srcset="https://www.teachmemicro.com/wp-content/uploads/2020/02/arduino-clap-switch-RGB_bb-2-1024x464.jpg 1024w, https://www.teachmemicro.com/wp-content/uploads/2020/02/arduino-clap-switch-RGB_bb-2-300x136.jpg 300w, https://www.teachmemicro.com/wp-content/uploads/2020/02/arduino-clap-switch-RGB_bb-2-768x348.jpg 768w, https://www.teachmemicro.com/wp-content/uploads/2020/02/arduino-clap-switch-RGB_bb-2.jpg 1563w" sizes="auto, (max-width: 640px) 100vw, 640px" /></a></p>
<p>The condenser microphone converts sound to an analog voltage. My <a href="https://www.teachmemicro.com/arduino-microphone/">Arduino microphone tutorial</a> covers the basics of how condenser microphones work. The voltage from the microphone is read by the <a href="https://www.teachmemicro.com/arduino-sensor-tutorial/">ADC</a> of the Arduino and converted it to a digital value.</p>
<p>You can use the <em>analogReadSerial</em> sketch (<em>File&gt; Examples&gt;Basics</em>) first to know the value of your clap sound. After that, replace the value of the threshold variable in the sketch below.</p>
<p>This clap switch project listens to three claps 1 second apart. When this condition is met, the RGB LED strip changes color. Moreover, the sequence of colors follows R-O-Y-G-B-V.</p>
<p>A particular LED turns on when the voltage drop across it is zero. For example, a high pulse from Arduino's D9 will turn on the transistor resulting in the green LED turning on. The 2N2222A transistor in this project was enough to handle the current requirements of the LED strip.</p>
<p>You can use a 5V or a 12V LED strip as long as you have the power source for it. My diagram uses a 12V LED strip so I needed to have a separate supply for the Arduino board.</p>
<h3><strong>Sketch/Code</strong></h3>
<div class="hcb_wrap">
<pre class="prism undefined-numbers lang-cpp" data-lang="C++"><pre><code class="language-cpp">/*
 * Clap Switch for RGB LEDs using Arduino
 * 
 * by: R. Pelayo
 * Created: February 13, 2020
 * 
 * Full project tutorial on: teachmemicro.com/clap-switch-rgb-led-arduino
 */

#define threshold 200  //change this according to your clap sound
int red = 10;
int green = 9;
int blue = 11;
int mic = A0;
int sound_value;
int clap_counter = 0;
int color_counter = 0;

void setup() {
  pinMode(red,OUTPUT);
  pinMode(green,OUTPUT);
  pinMode(blue,OUTPUT);
}

void loop() {
  int sound_value = analogRead(mic);
  if(sound_value &gt; threshold){
    clap_counter++;
    if(clap_counter &gt; 2){
      clap_counter = 0;
      color_counter++;
      if(color_counter &gt; 5) color_counter = 0;
      changeColor();
    }
  }
  delay(1000);
}

void changeColor(){
  switch(color_counter){
    case 0: //display red
      analogWrite(red,255);
      analogWrite(green,0);
      analogWrite(blue,0);
    case 1: //display orange
      analogWrite(red,210);
      analogWrite(green,100);
      analogWrite(blue,0);
    case 2: //display yellow
      analogWrite(red,255);
      analogWrite(green,255);
      analogWrite(blue,0);
    case 3: //display green
      analogWrite(red,0);
      analogWrite(green,255);
      analogWrite(blue,0);
    case 4: //display blue
      analogWrite(red,0);
      analogWrite(green,0);
      analogWrite(blue,255);
    case 5: //display violet
      analogWrite(red,255);
      analogWrite(green,0);
      analogWrite(blue,255);
  }
}</code></pre></pre>
</div>
<p>Here, I set the threshold value to 200. Every time the voltage from the microphone reaches this level, a variable (<em>clap_counter</em>) increments. When this variable reaches a value of three, the function to change color is invoked, and another variable (<em>color_counter</em>) increments. The <em>color_counter</em> variable is used for knowing which color in the sequence to be displayed.</p>
<p>&nbsp;</p>
<p>The post <a href="https://www.teachmemicro.com/clap-switch-rgb-led-arduino/">Clap Switch for RGB LEDs using Arduino</a> appeared first on <a href="https://www.teachmemicro.com">Teach Me Microcontrollers!</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.teachmemicro.com/clap-switch-rgb-led-arduino/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Flame Sensor Project: Comprehensive Guide</title>
		<link>https://www.teachmemicro.com/flame-sensor-project-comprehensive-guide/</link>
					<comments>https://www.teachmemicro.com/flame-sensor-project-comprehensive-guide/#respond</comments>
		
		<dc:creator><![CDATA[Roland Pelayo]]></dc:creator>
		<pubDate>Thu, 23 Jan 2020 03:22:51 +0000</pubDate>
				<category><![CDATA[Arduino Projects]]></category>
		<category><![CDATA[ESP8266 Projects]]></category>
		<category><![CDATA[PIC Projects]]></category>
		<category><![CDATA[Sensor Tutorial]]></category>
		<category><![CDATA[fire detector]]></category>
		<category><![CDATA[fire sensor]]></category>
		<category><![CDATA[flame sensor for furnace]]></category>
		<category><![CDATA[iot]]></category>
		<category><![CDATA[push notification]]></category>
		<category><![CDATA[pushbullet]]></category>
		<category><![CDATA[pushingbox]]></category>
		<category><![CDATA[sensor]]></category>
		<guid isPermaLink="false">https://www.teachmemicro.com/?p=4081</guid>

					<description><![CDATA[<p>Flame sensors are found from the simplest gas stoves to huge industrial plants. There are different types of fire sensors -- some cheap, some expensive. Yet they’re all built for one single purpose: detect fire. In this article, I will guide you on how to build a flame sensor with a few extra bucks and &#8230;</p>
<p>The post <a href="https://www.teachmemicro.com/flame-sensor-project-comprehensive-guide/">Flame Sensor Project: Comprehensive Guide</a> appeared first on <a href="https://www.teachmemicro.com">Teach Me Microcontrollers!</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><span style="font-weight: 400;">Flame sensors are found from the simplest gas stoves to huge industrial plants. There are different types of fire sensors -- some cheap, some expensive. Yet they’re all built for one single purpose: detect fire. In this article, I will guide you on how to build a flame sensor with a few extra bucks and some microcontroller programming knowledge.</span></p>
<p><span id="more-4081"></span></p>
<h3><b>What You’ll Need</b></h3>
<ul>
<li style="font-weight: 400;"><a href="http://s.click.aliexpress.com/e/_shLsn8"><span style="font-weight: 400;">Flame sensor module</span></a></li>
<li style="font-weight: 400;"><span style="font-weight: 400;">Microcontroller (<a href="http://s.click.aliexpress.com/e/_swUr9G">Arduino</a> or <a href="http://s.click.aliexpress.com/e/_sjwNOI">NodeMCU ESP8266</a> or <a href="http://s.click.aliexpress.com/e/_rwLdlk">PIC</a>)</span></li>
<li style="font-weight: 400;"><span style="font-weight: 400;">Power source</span></li>
<li style="font-weight: 400;"><span style="font-weight: 400;">Output device (<a href="http://s.click.aliexpress.com/e/_sJNkJ4">buzzer</a> or smartphone)</span></li>
</ul>
<h3><b>Flame Sensor Module</b></h3>
<p><span style="font-weight: 400;">The star of this project is this flame sensor module:</span></p>
<p><img loading="lazy" decoding="async" class="aligncenter" src="https://lh3.googleusercontent.com/UXqpd7DvjX9XaJgMK5vgvEzS2Xl67oAI2kZ_0juCLcn3UGK7t9laSC6FsKtlTnfCJDP7ATFQFZM8cossm01jBggvcdgcXg0uu7Zv0Y98Zdlnvg6hRWrgQNFQ6L2ZE8B58Y8OjPI8" alt="flame sensor module" width="401" height="401" /></p>
<p id="ureLAlb">This module contains a <a href="https://www.win-source.net/category/sensors-transducers/optical-sensors/optical-sensors-phototransistors">phototransistor</a> and <a href="https://en.wikipedia.org/wiki/Signal_conditioning"><i>signal conditioning</i></a> electronics. A phototransistor conducts more electrical current when exposed to light. Physics taught us that (visible) light comprises of all colors, from red to violet. By coating the phototransistor with black epoxy, it becomes more sensitive to red or even invisible <i>below red</i> or <a href="https://www.merriam-webster.com/dictionary/infra">infra</a>red. Interestingly, flame emits infrared radiation. Thus, when this sensor <i>sees</i> flame, it conducts more current.</p>
<p><a href="https://www.teachmemicro.com/wp-content/uploads/2020/01/flame-sensor-range.jpg"><img loading="lazy" decoding="async" class="aligncenter wp-image-4101 size-large" src="https://www.teachmemicro.com/wp-content/uploads/2020/01/flame-sensor-range-1024x486.jpg" alt="flame sensor range" width="640" height="304" srcset="https://www.teachmemicro.com/wp-content/uploads/2020/01/flame-sensor-range-1024x486.jpg 1024w, https://www.teachmemicro.com/wp-content/uploads/2020/01/flame-sensor-range-300x142.jpg 300w, https://www.teachmemicro.com/wp-content/uploads/2020/01/flame-sensor-range-768x365.jpg 768w, https://www.teachmemicro.com/wp-content/uploads/2020/01/flame-sensor-range.jpg 1089w" sizes="auto, (max-width: 640px) 100vw, 640px" /></a></p>
<p><span style="font-weight: 400;">Microcontrollers read voltage, not current. Thus, the signal conditioning circuit in the module converts the current into levels of voltage that is considered safe for the microcontroller (typically, the maximum voltage should be the supply voltage to the microcontroller).</span></p>
<p><span style="font-weight: 400;">The flame sensor module also contains indicator LEDs. One LED lights up (PWR-LED) when the module is powered up while another lights up (D0-LED) when the sensor detects fire.</span></p>
<p><span style="font-weight: 400;">Moreover, the voltage from the D0 pin falls to zero in the vicinity of fire and rises to the supply voltage when there is none. We use this as a trigger for the microcontroller.</span></p>
<h3><b>Arduino - Flame Sensor Interface</b></h3>
<p><span style="font-weight: 400;">I aim to use three different kinds of microcontroller for this project. First, I’ll show how to build an Arduino flame sensor with a buzzer as indicator.</span></p>
<p><span style="font-weight: 400;">Since the module only provides two values, on and off, we don’t need to use any of the </span><i><span style="font-weight: 400;">analog</span></i><span style="font-weight: 400;"> pins of the Arduino. The wiring diagram below shows that the D0 pin of the module connects to the digital pin 2 of the <a href="https://www.teachmemicro.com/arduino-uno-pinout-diagram/">Arduino UNO</a>:</span></p>
<p><img loading="lazy" decoding="async" class="aligncenter" src="https://lh6.googleusercontent.com/PranlMnwtShfCJ8lP3_YPz9tnJPyhji4pLBnISEus-tZ_0V_Hy3aY6vkLm9fjXCaEVDj8vCSHQ37fpplFfSMCBZPTthgpA34qsKnvCvS3ssqPHwZSWAe0iTB9eUBHm0drPoA82Px" alt="arduino flame sensor" width="1600" height="810" /></p>
<p id="qxiAFCd">Moreover, the VCC or + pin connects to 5V and the G pin is connected to GND. Note that the fire detector module can also run on 3.3V. Finally, the buzzer’s red wire is connected to pin 5 while its black pin is wired to GND. In this project, I am using a buzzer that can run from voltages 3 to 24 V.</p>
<p><span style="font-weight: 400;">The sketch is straightforward:</span></p>
<pre class="lang:arduino decode:true "><pre><code class="language-cpp">const int sensor_pin = 2;
const int buzzer = 5;

void setup() {
  pinMode(sensor_pin,INPUT_PULLUP);
  pinMode(buzzer,OUTPUT);
}

void loop() {
  if(digitalRead(sensor_pin) == LOW){
    digitalWrite(buzzer,HIGH);
  }else{
    digitalWrite(buzzer,LOW);
  }
}</code></pre></pre>
<p><span style="font-weight: 400;">I gave pins 2 and 5 appropriate aliases:</span></p>
<pre class="lang:arduino decode:true"><pre><code class="language-cpp">const int sensor_pin = 2;
const int buzzer = 5;</code></pre></pre>
<p><span style="font-weight: 400;">Inside <em>setup()</em>, we declare sensor_pin as an input pin while buzzer is an output pin:</span></p>
<pre class="lang:arduino decode:true "><pre><code class="language-cpp">pinMode(sensor_pin,INPUT_PULLUP);
pinMode(buzzer,OUTPUT);</code></pre></pre>
<p>In <em>loop()</em>, we check if the sensor_pin voltage is zero. If it is, we sound the buzzer. If not, then turn off the buzzer.</p>
<pre class="lang:arduino decode:true"><pre><code class="language-cpp">void loop() {
  if(digitalRead(sensor_pin) == LOW){
    digitalWrite(buzzer,HIGH);
  }else{
    digitalWrite(buzzer,LOW);
  }
}</code></pre></pre>
<p><span style="font-weight: 400;">The fire sensor can detect flame within its 60 degree capture angle. The distance of the flame varies with ambient light. In my tests, the sensor was able to detect a match’s flame from around 5 cm.</span></p>
<h3><b>Using the Flame Sensor with PIC16F877A</b></h3>
<p><span style="font-weight: 400;">The difference here is that we are using a different microcontroller and thus, a different compiler. Nevertheless, the concept is the same: the <a href="https://www.teachmemicro.com/pic16f877a-more-microcontroller-features/">PIC16F877A</a> reads the D0 pin and if its low, fire up the buzzer and so on.</span></p>
<p><span style="font-weight: 400;">Here’s the wiring diagram:</span></p>
<p><img loading="lazy" decoding="async" class="alignnone" src="https://lh4.googleusercontent.com/dUHgVWYPYGKTBkkIi98OV0o0ohNzCquUNk7_t-gBAgCKJnwVBsFRA1-RQi_3v4Ow7o5SrNTauA4MCofZkgznBr7v6YK6oa85DZ1fmmQC3dUe1EENyWSN8JwlgUMkJ4shVRYXh4ql" alt="PIC flame sensor" width="1600" height="843" /></p>
<p id="SpHuJQC">The D0 pin of the fire detector module connects to RB0 while the buzzer’s red wire is connected to RB1.</p>
<p><span style="font-weight: 400;">The C code (XC8) for the circuit above is shown below:</span></p>
<pre class="lang:arduino decode:true "><pre><code class="language-cpp">#define _XTAL_FREQ 4000000
#include &lt;xc.h&gt;

void main(void) {
    
    TRISBbits.TRISB0 = 1;
    TRISBbits.TRISB1 = 0;
    while(1){
        if(PORTBbits.RB0 == 0){
            PORTBbits.RB1 = 1;
        }else{
            PORTBbits.RB1 = 0;
        }
    }
    return;
}</code></pre></pre>
<p>The pin RB0 is an input pin while RB1 is an output pin. Hence their corresponding TRIS bits should be:</p>
<pre class="lang:arduino decode:true "><pre><code class="language-cpp">TRISBbits.TRISB0 = 1; 
TRISBbits.TRISB1 = 0;</code></pre></pre>
<p>We then use simple if-else statements to check if the sensor detects a fire and then turn on or off the buzzer:</p>
<pre class="lang:arduino decode:true "><pre><code class="language-cpp">while(1){
 if(PORTBbits.RB0 == 0){
    PORTBbits.RB1 = 1; 
 }else{ PORTBbits.RB1 = 0; 
 } 
}</code></pre></pre>
<p>There's no <em>loop()</em> function in XC8 so we create our own infinite loop using the <em>while(1)</em> statement.</p>
<p>That's it for interfacing the flame detector with a PIC microcontroller.</p>
<h3><b>Flame Sensor and ESP8266</b></h3>
<p><span style="font-weight: 400;">Finally we are down to our last microcontroller, the <a href="https://www.teachmemicro.com/category/tutorials/esp8266-tutorial/">ESP8266</a>. Of course, we will be using the flame sensor with the ESP8266 according to the latter’s capabilities. In this version, the user will receive a push notification on his or her smartphone everytime the fire detector is triggered.</span></p>
<p><span style="font-weight: 400;">Here’s the wiring diagram:</span></p>
<p><img loading="lazy" decoding="async" class="aligncenter" src="https://lh6.googleusercontent.com/jPuaGEAok0DvjF24L3ULtL-xCwLtbK6yiYEPgqj7w-BtBtZTVdHNKBFwePp68rLr-ScEfE1tPyygRPoDe5Odc5_ZnkM3hdkdtufbeq2wCUl-6FPlcdaEfoMaAng9-CwgWTGc-EA1" alt="nodemcu esp8266 flame sensor" width="951" height="780" /></p>
<p id="XsNDRWV">This diagram is the simplest since we don’t need the buzzer anymore. Note that I am using a <a href="https://www.teachmemicro.com/nodemcu-pinout/">NodeMCU ESP8266</a> board for this project. If this is your first time using the NodeMCU, please follow the <a href="https://www.teachmemicro.com/intro-nodemcu-arduino/">instructions to program it using the Arduino IDE</a>.</p>
<p><span style="font-weight: 400;">Here is the full sketch:</span></p>
<pre class="lang:arduino decode:true "><pre><code class="language-cpp">/*
 *  This sketch sends push notifications via Pushbullet App through PushingBox.
 *
 *  You need to create a service and scenario at pushingbox.com to acquire
 *  a device ID. Also change lines 12 and 13 to your own WiFi SSID and password
 *
 */
#include &lt;ESP8266WiFi.h&gt;
#include &lt;WiFiClient.h&gt;
#include &lt;ESP8266WebServer.h&gt;

const char* ssid     = &quot;&lt;your WiFi SSID&gt;&quot;;
const char* password = &quot;&lt;your WiFi password&gt;&quot;;

const char* host = &quot;api.pushingbox.com&quot;;
const char* devid = &quot;vADAB279A6A4DA5A&quot;;

const int sensor_pin = 14;

void setup()
{
    pinMode(sensor_pin,INPUT_PULLUP);
    pinMode(buzzer,OUTPUT);
    
    Serial.begin(115200);
    delay(10);

    // We start by connecting to a WiFi network

    Serial.println();
    Serial.println();
    Serial.print(&quot;Connecting to &quot;);
    Serial.println(ssid);

    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(&quot;.&quot;);
    }

    Serial.println(&quot;&quot;);
    Serial.println(&quot;WiFi connected&quot;);
    Serial.println(&quot;IP address: &quot;);
    Serial.println(WiFi.localIP());
}

int value = 0;

void loop()
{
    delay(5000);

    if(digitalRead(sensor_pin) == LOW){  //flame sensor triggered

    Serial.print(&quot;connecting to &quot;);
    Serial.println(host);

    // Use WiFiClient class to create TCP connections
    WiFiClient client;
    const int httpPort = 80;
    if (!client.connect(host, httpPort)) {
        Serial.println(&quot;connection failed&quot;);
        return;
    }

    // We now create a URI for the request
    String url = &quot;/pushingbox&quot;;
    url += &quot;?devid=&quot;;
    url += devid;
    
    Serial.print(&quot;Requesting URL: &quot;);
    Serial.println(url);

    // This will send the request to the server
    client.print(String(&quot;GET &quot;) + url + &quot; HTTP/1.1\r\n&quot; +
                 &quot;Host: &quot; + host + &quot;\r\n&quot; +
                 &quot;Connection: close\r\n\r\n&quot;);
    unsigned long timeout = millis();
    while (client.available() == 0) {
        if (millis() - timeout &gt; 5000) {
            Serial.println(&quot;&gt;&gt;&gt; Client Timeout !&quot;);
            client.stop();
            return;
        }
    }

    // Read all the lines of the reply from server and print them to Serial
    while(client.available()) {
        String line = client.readStringUntil(&#039;\r&#039;);
        Serial.print(line);
    }

    Serial.println();
    Serial.println(&quot;closing connection&quot;);
    }else{;}
}</code></pre></pre>
<p><span style="font-weight: 400;">Before uploading this sketch, w</span><span style="font-weight: 400;">e need to configure <strong>Pushingbox</strong> and <strong>Pushbullet</strong> for this project to work. </span></p>
<p><span style="font-weight: 400;">First, go to <a href="http://pushingbox.com">Pushingbox</a> and signup or login to your account. You will then be redirected to the Dashboard page:</span></p>
<p><img decoding="async" class="aligncenter" src="https://lh3.googleusercontent.com/onidtVcEdB_-AD1N1hmd_U--Ugx_GuzftOOyi5w8TTPTqRhUsjVMeRMR6p6JqMimVi-vBtUCL99LkUj_blpx_Gt288EM4MFhF8tVVGvD5QDWhh2Yy0x1mbwSW7YEiX6X6BwbQBPT" /></p>
<p id="rwLevPd">Click the My Services tab and click “Add a service”:</p>
<p><img decoding="async" class="aligncenter" src="https://lh6.googleusercontent.com/7MLzXVKaxj_6xpC78ViRap8_tLodfaUrre6CDgHloPfnRW7EAc8TD2ll4DroYBgl2woBst32iE3uLW6O4W-DiVSCpasPornGV2nGZcF6J_mRErikaVSpIPdp_yre0Diq3sd2xlQ_" /></p>
<p id="WxAnCWK">Next, look for the <strong>Pushbullet</strong> service:</p>
<p><img decoding="async" class="aligncenter" src="https://lh4.googleusercontent.com/e8wWY2MwPV0Z6qqTO94GiIrpeiqaLFLR6i2fonsrNSWcGUEG8TP88ogMw3UXHsepGfd25LoU3Y2ROQGxa3ij6afPtaXdWGuhtrWf4qYVAFy88nmIth50tDN6wEzuzx1PaNeuFpdr" /></p>
<p id="RNORYDo">However, before your press the “Select this service” button, you need to have a Pushbullet account first.</p>
<p><span style="font-weight: 400;">Go to <a href="http://pushbullet.com">Pushbullet</a>:</span></p>
<p><img decoding="async" class="aligncenter" src="https://lh5.googleusercontent.com/B-9xPNHGioDovDHk7N0KuQiLvKsliBGQKHR9YeJErvbV8jZfQT1dmDbVAaTyyS2OhdwS3Mzs5Z4S9LVKkqZNY1Rp04Ih6sVb0qh-48QGJu37MaD6olFSVW674hWg7QZOOEFGO7oS" /></p>
<p id="phKrJrS">Sign up or login to your account. After that, find <i>Settings</i> on the dashboard. There you’ll find your access token:</p>
<p><img decoding="async" class="aligncenter" src="https://lh5.googleusercontent.com/9X0Yh57X6kKZTlxaA4vNpklb_VMOT9zzSohc8NYgA8y39g9zlo9in-szfwCD0b8SITQlbLAlq7jnQGn6AwTF2GIkL2EuZxjvYYfzDGhYj0DRTObwRr1UPxCtkatamWtlFxGNyono" /></p>
<p id="fPFfvEA">Go back to Pushingbox, click add a service again and this time click the “Select this service” button for pushbullet. A window appears:</p>
<p><img decoding="async" class="aligncenter" src="https://lh4.googleusercontent.com/uvIReILZSWdPHVwQvxeOUIQcJdArIZvx4IMd3VLN0HYhesHwkFISfT_kvY7N4nr5GiWaczJ1uoZmoaSYE6O1z_xx37T1_JJV6mIyl9wBlOoMy7Wvfisp5T5EDx6Nv4JYp3X7EVYG" /></p>
<p id="oQUGmGo">Give the configuration a name, paste your Pushbullet token then click submit.</p>
<p><span style="font-weight: 400;">Next, click the My Scenarios tab to add a scenario linked to the service just created:</span></p>
<p><img decoding="async" class="aligncenter" src="https://lh5.googleusercontent.com/s2ggarqvw_j8pYkbsUILpx8OwN4K79FVGl4103dIym8VyTRM-n4uQujiiDbIGxnTTnINR2qycaWjDsuKK7WqFgD_nnxZ8vtPfjINg18MPTJD--iUfR4uMUSOyWEyYghq-CbDjhAZ" /></p>
<p id="uUFvHoo">After creating the scenario, add an action:</p>
<p><img decoding="async" class="aligncenter" src="https://lh6.googleusercontent.com/LtbubUNpaf77sVNZwprFZZu1h772RvoPnsVmjWCFoHCwvCGe_QBUOKW14faxf_dFXKXKoZ_Tx-IuTegm67b9H5a0QXoMgquGU1x0ogRXWZEP6TQuYbOw2Bovxzzsow5srodB2aUw" /></p>
<p id="SqmKfkc">Make sure you select the appropriate scenario:</p>
<p><img decoding="async" class="aligncenter" src="https://lh3.googleusercontent.com/ZMtFdP_6dKMRC6ruvdydLsJjRrRRtxSam_LtvyQcoSoHrgOrDZGCTqCcRgsDSfbqbsTDaea_5ArY3gXzuYVxgBselD9w9DjguWlm9at2bS5ThenE5DhwyB8ufwYROm83hU1XXhZA" /></p>
<p id="BUFdKUn">A window appears; give the action title, push notification message and then click submit.</p>
<p><img decoding="async" class="aligncenter" src="https://lh4.googleusercontent.com/7YccBkESlKjf06DxCvKe_S4v0I_DmhCk8cO48EhPtYrC4igr5ko_39D76FE1iTBOZHXA5kFkzOPMNEt8zwqI_RzkSZaCG-x3Oz2tWm5fCS3bgB2QVV2wLgl1nDE_7Y229s0lY9Ci" /></p>
<p id="ICjEFEN">Finally, copy the d<em>evice ID</em> whose location is shown:</p>
<p><img decoding="async" class="aligncenter" src="https://lh5.googleusercontent.com/tScPkXAptG6vu4v3K3ezYKz_yn-gzTtaDO7AksWWUoISwe1xlsl_8F3aZGzyz8EsmJciGmsKTehxKaEzhmtw_zJUZzOsM8HWRG4HKtlYfG9mDSgZoNmLs0uWOhTF_KG6k6xnt5OD" /></p>
<p id="XcDpIRZ">Now on the sketch, locate line 16 and change the device ID:</p>
<pre class="lang:arduino decode:true"><pre><code class="language-cpp">const char* devid = &quot;vADAB279A6A4DA5A&quot;;</code></pre></pre>
<p><span style="font-weight: 400;">Also, don’t forget to provide your own WiFi’s SSID and password on lines 12 and 13:</span></p>
<pre class="lang:arduino decode:true"><pre><code class="language-cpp">const char* ssid     = &quot;&lt;your WiFi SSID&gt;&quot;;
const char* password = &quot;&lt;your WiFi password&gt;&quot;;</code></pre></pre>
<p><span style="font-weight: 400;">Upload the sketch and you're good to go! Now, install the Pushbullet app on your smartphone and login to your account. Everytime the fire detector is triggered, you will receive a push notification!</span></p>
<h3><b>Conclusion</b></h3>
<p>The flame sensor project here is limited in capture angle and range. However, there's a special module that contains five phototransistors:</p>
<p><a href="https://www.teachmemicro.com/wp-content/uploads/2020/01/multi-flame-sensor.jpg"><img loading="lazy" decoding="async" class="aligncenter wp-image-4104" src="https://www.teachmemicro.com/wp-content/uploads/2020/01/multi-flame-sensor.jpg" alt="wide angle flame sensor" width="562" height="442" srcset="https://www.teachmemicro.com/wp-content/uploads/2020/01/multi-flame-sensor.jpg 905w, https://www.teachmemicro.com/wp-content/uploads/2020/01/multi-flame-sensor-300x236.jpg 300w, https://www.teachmemicro.com/wp-content/uploads/2020/01/multi-flame-sensor-768x603.jpg 768w" sizes="auto, (max-width: 562px) 100vw, 562px" /></a>This 5-way fire detector module has a capture angle of 120°. <a href="http://s.click.aliexpress.com/e/_sKoiI2">Check prices here</a>.</p>
<p id="kaAcODv">Hopefully, this article helps you in any way. For any questions, reactions or suggestions, kindly drop comments below.</p>
<p>The post <a href="https://www.teachmemicro.com/flame-sensor-project-comprehensive-guide/">Flame Sensor Project: Comprehensive Guide</a> appeared first on <a href="https://www.teachmemicro.com">Teach Me Microcontrollers!</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.teachmemicro.com/flame-sensor-project-comprehensive-guide/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Arduino Compass with HMC5883L Magnetometer</title>
		<link>https://www.teachmemicro.com/arduino-compass-hmc5883l-magnetometer/</link>
					<comments>https://www.teachmemicro.com/arduino-compass-hmc5883l-magnetometer/#comments</comments>
		
		<dc:creator><![CDATA[Roland Pelayo]]></dc:creator>
		<pubDate>Mon, 18 Feb 2019 22:17:56 +0000</pubDate>
				<category><![CDATA[Arduino Projects]]></category>
		<guid isPermaLink="false">https://www.teachmemicro.com/?p=2923</guid>

					<description><![CDATA[<p>In general terms, a magnetometer measures the magnetism of objects or places. In fact, a compass is a type of magnetometer. This project demonstrates how to use the HMC5883L magnetometer IC as a compass with an Arduino microcontroller. Hardware Setup I have setup this project on a mini breadboard using the parts: HMC5883L Magnetometer Nokia &#8230;</p>
<p>The post <a href="https://www.teachmemicro.com/arduino-compass-hmc5883l-magnetometer/">Arduino Compass with HMC5883L Magnetometer</a> appeared first on <a href="https://www.teachmemicro.com">Teach Me Microcontrollers!</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In general terms, a <strong>magnetometer</strong> measures the magnetism of objects or places. In fact, a compass is a type of magnetometer. This project demonstrates how to use the HMC5883L <a href="https://www.win-source.net/category/sensors-transducers/magnetic-sensors/magnetic-sensors-linear-compass-ics">magnetometer IC</a> as a compass with an Arduino <a href="https://www.ersaelectronics.com/c/microcontrollers">microcontroller</a>.</p>
<p><span id="more-2923"></span></p>
<h3><strong>Hardware Setup</strong></h3>
<p><a href="https://www.teachmemicro.com/wp-content/uploads/2019/02/arduino-compass-hmc5883l-magnetomete.jpg"><img loading="lazy" decoding="async" class="size-large wp-image-2925 aligncenter" src="https://www.teachmemicro.com/wp-content/uploads/2019/02/arduino-compass-hmc5883l-magnetomete-1024x497.jpg" alt="Arduino Compass with HMC5883L Magnetometer" width="618" height="300" srcset="https://www.teachmemicro.com/wp-content/uploads/2019/02/arduino-compass-hmc5883l-magnetomete-1024x497.jpg 1024w, https://www.teachmemicro.com/wp-content/uploads/2019/02/arduino-compass-hmc5883l-magnetomete-300x146.jpg 300w, https://www.teachmemicro.com/wp-content/uploads/2019/02/arduino-compass-hmc5883l-magnetomete-768x373.jpg 768w, https://www.teachmemicro.com/wp-content/uploads/2019/02/arduino-compass-hmc5883l-magnetomete.jpg 1971w" sizes="auto, (max-width: 618px) 100vw, 618px" /></a></p>
<p>I have setup this project on a mini breadboard using the parts:</p>
<ul>
<li>HMC5883L Magnetometer</li>
<li><a href="http://s.click.aliexpress.com/e/ceXKLoss">Nokia 3310/5110 LCD Breakout Board</a></li>
<li><a href="http://s.click.aliexpress.com/e/bIeyZbpA">Arduino Pro Mini 5V</a></li>
</ul>
<p>The HMC5883L magnetometer is fairly common but for this project, I used the <a href="https://www.dfrobot.com/product-818.html">10 DOF IMU</a> from DFRobot. This module contains not only the HMC5883L but also a BMP280 barometric pressure sensor, ITG-3205 gyroscope and <a href="https://www.ersaelectronics.com/p/analog-devices-inc-adxl345bccz-2034829">ADXL345 accelerometer</a> into one device. Note that the <a href="http://s.click.aliexpress.com/e/FUbHYrq">ordinary HMC5883L magnetometer module</a> will still work with this project. Moreover, the Nokia 3310/5110 LCD is used for display and the Arduino Pro Mini is the brains of the project.</p>
<h3><strong>Needed Libraries</strong></h3>
<p>For this project's sketch to run, you'll need the following libraries:</p>
<ul>
<li>Adafruit's <a href="https://github.com/adafruit/Adafruit-PCD8544-Nokia-5110-LCD-library">PCD8544</a> and <a href="https://github.com/adafruit/Adafruit-GFX-Library">GFX</a></li>
<li>DFRobot's <a href="https://github.com/adafruit/Adafruit-GFX-Library">HMC5883L</a></li>
</ul>
<p>You can learn more about using the Nokia 3310/5110 LCD and its libraries in a <a href="https://www.teachmemicro.com/arduino-nokia-3310-lcd-interfacing/">separate article.</a></p>
<h3><strong>Developing the Project</strong></h3>
<h4><strong>Wiring</strong></h4>
<p>The project is not that hard because of the readily available libraries. The wiring is also straightforward:</p>
<p><a href="https://www.teachmemicro.com/wp-content/uploads/2019/02/arduino-compass-hmc5833L-magnetometer-fritzing-diagram.jpg"><img loading="lazy" decoding="async" class="alignnone size-full wp-image-2927" src="https://www.teachmemicro.com/wp-content/uploads/2019/02/arduino-compass-hmc5833L-magnetometer-fritzing-diagram.jpg" alt="Arduino Compass with HMC5883L Magnetometer" width="954" height="491" srcset="https://www.teachmemicro.com/wp-content/uploads/2019/02/arduino-compass-hmc5833L-magnetometer-fritzing-diagram.jpg 954w, https://www.teachmemicro.com/wp-content/uploads/2019/02/arduino-compass-hmc5833L-magnetometer-fritzing-diagram-300x154.jpg 300w, https://www.teachmemicro.com/wp-content/uploads/2019/02/arduino-compass-hmc5833L-magnetometer-fritzing-diagram-768x395.jpg 768w" sizes="auto, (max-width: 954px) 100vw, 954px" /></a></p>
<p><strong>Nokia LCD to Pro Mini Connection:</strong></p>
<table>
<tbody>
<tr>
<th>Nokia 3310 LCD</th>
<th>Arduino Pro Mini</th>
</tr>
<tr>
<td>1 (RST)</td>
<td>D3</td>
</tr>
<tr>
<td>2 (CE)</td>
<td>D4</td>
</tr>
<tr>
<td>3 (DC)</td>
<td>D5</td>
</tr>
<tr>
<td>4 (DIN)</td>
<td>D6</td>
</tr>
<tr>
<td>5 (CLK)</td>
<td>D7</td>
</tr>
<tr>
<td>6 (VCC)</td>
<td>VCC</td>
</tr>
<tr>
<td>7 (LIGHT)</td>
<td>GND</td>
</tr>
<tr>
<td>8 (GND)</td>
<td>GND</td>
</tr>
</tbody>
</table>
<p><strong>HMC5883L to Pro Mini Connection:</strong></p>
<table>
<tbody>
<tr>
<th>HMC5883L</th>
<th>Arduino Pro Mini</th>
</tr>
<tr>
<td>SCL</td>
<td>A5</td>
</tr>
<tr>
<td>SDA</td>
<td>A4</td>
</tr>
<tr>
<td>VIN</td>
<td>VCC</td>
</tr>
<tr>
<td>GND</td>
<td>GND</td>
</tr>
</tbody>
</table>
<h4><strong>Reading Data from HMC5883L</strong></h4>
<p>The first step was to make sure the HMC5883L is giving out the correct heading. According to the example provided in the HMC5883L library, the range, measurement mode, data rate and samples must be set before reading out data from the magnetometer. Thus, I created this function:</p>
<div class="hcb_wrap">
<pre class="prism undefined-numbers lang-c" data-lang="C"><pre><code class="language-cpp">void initCompass(){ 
 while (!compass.begin()) { 
  Serial.println(&quot;Could not find a valid QMC5883 sensor, check wiring!&quot;); 
  delay(500); 
 } 

 Serial.println(&quot;Initialize QMC5883&quot;); 
 compass.setRange(QMC5883_RANGE_2GA); 
 compass.setMeasurementMode(QMC5883_CONTINOUS); 
 compass.setDataRate(QMC5883_DATARATE_50HZ); 
 compass.setSamples(QMC5883_SAMPLES_8); 
}</code></pre></pre>
</div>
<p>The library has a neat <em>begin()</em> function which checks out if the sensor is connected or not, which I used as seen above.</p>
<h4><strong>Correcting for Magnetic Declination</strong></h4>
<p>Heading is the term used to measure your position from the north pole. North pole is actually the <em>north geographic pole</em> while compasses detect the <em>north magnetic pole </em>which is a different place although not that far from the other north pole. The distance between each pole is really not the issue; it's the <a href="https://en.wikipedia.org/wiki/Magnetic_declination">magnetic declination</a> between the two. Thus, there's a need to correct for this declination but first you must find out your area's declination through <a href="http://magnetic-declination.com">here</a>.</p>
<p>For example, the magnetic declination in our area is - 0<span class="st">°</span> 53' (negative 0 degrees and 53' minutes). This declination must be converted into radians:</p>
<p><img decoding="async" id="equationview" class="aligncenter" title="This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program." src="https://latex.codecogs.com/gif.latex?declination%20%3D%20-%280%20&amp;plus;%2053/60%29%20*%20%5Cpi%20/%20180%20%3D%20-0.0154" name="equationview" /></p>
<p>Let's say the reading of the compass is 2.0 radians. The correct heading, accounting declination, is now:</p>
<p><img decoding="async" id="equationview" class="aligncenter" title="This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program." src="https://latex.codecogs.com/gif.latex?correctheading%20%3D%202%20&amp;plus;%20%28-0.0154%29%20%3D%201.9846%5C%3A%20rad" name="equationview" /></p>
<h4><strong>Drawing the Compass</strong></h4>
<p>Now that I have the correct heading, the next part is to display that heading on the LCD. Showing the heading as a number is too easy; I wanted an arrow that will always point to the north like a real compass.</p>
<p>The GFX library I pointed out above has a line function:</p>
<div class="hcb_wrap">
<pre class="prism undefined-numbers lang-c" data-lang="C"><pre><code class="language-cpp">drawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t, y1, uint16_t color);</code></pre></pre>
</div>
<p>Here, the x0, y0 are the coordinates of the origin of the line while x1, y1 are those of the end point.</p>
<p>Specifying the origin of the arrow for the compass is just writing the center of the LCD which is (41, 23). The end point of the line is based on the heading. To illustrate:</p>
<p><a href="https://www.teachmemicro.com/wp-content/uploads/2019/02/heading-calculation-1.jpg"><img loading="lazy" decoding="async" class="alignnone size-full wp-image-2934" src="https://www.teachmemicro.com/wp-content/uploads/2019/02/heading-calculation-1.jpg" alt="Heading Calculation" width="800" height="500" srcset="https://www.teachmemicro.com/wp-content/uploads/2019/02/heading-calculation-1.jpg 800w, https://www.teachmemicro.com/wp-content/uploads/2019/02/heading-calculation-1-300x188.jpg 300w, https://www.teachmemicro.com/wp-content/uploads/2019/02/heading-calculation-1-768x480.jpg 768w" sizes="auto, (max-width: 800px) 100vw, 800px" /></a></p>
<p>The end point could be computed as:</p>
<p><img decoding="async" id="equationview" class="aligncenter" title="This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program." src="https://latex.codecogs.com/gif.latex?x%20%3D%20rsin%5Ctheta%5C%3B%3By%3Drcos%5Ctheta" name="equationview" /></p>
<p>In geometry, coordinate (0,0) is at the center. But here, our center is at (41, 23) thus the axes must be translated. Moreover, I wanted to draw the North line, not the heading line. Finally, the direction of the arrow must be corrected according to how the HMC5883L board is connected to my breadboard.</p>
<h3><strong>Full Sketch</strong></h3>
<p>Here is now the full Arduino Sketch for this project:</p>
<div class="hcb_wrap">
<pre class="prism undefined-numbers lang-c" data-lang="C"><pre><code class="language-cpp">#include &lt;SPI.h&gt;
#include &lt;Wire.h&gt;
#include &lt;DFRobot_QMC5883.h&gt;
#include &lt;Adafruit_GFX.h&gt;
#include &lt;Adafruit_PCD8544.h&gt;

// Software SPI (slower updates, more flexible pin options):
// pin 7 - Serial clock out (SCLK)
// pin 6 - Serial data out (DIN)
// pin 5 - Data/Command select (D/C)
// pin 4 - LCD chip select (CS)
// pin 3 - LCD reset (RST)
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);

DFRobot_QMC5883 compass;

float arrDirection;
uint16_t centerX = 41;
uint16_t centerY = 23;
float endX;
float endY;
float hyp = 23;
float sum;

#define SAMPLE_SIZE 3

void setup()   {
  Serial.begin(9600);
  initDisplay();  

  display.println(&quot;Make figure&quot;);
  display.println(&quot;8&#039;s to calibrate sensor&quot;);
  display.display();
  initCompass();
  delay(5000);
  display.clearDisplay();
  
}

void initCompass(){
  while (!compass.begin())
  {
    Serial.println(&quot;Could not find a valid QMC5883 sensor, check wiring!&quot;);
    delay(500);
  }
  Serial.println(&quot;Initialize QMC5883&quot;);
  compass.setRange(QMC5883_RANGE_2GA);
  compass.setMeasurementMode(QMC5883_CONTINOUS); 
  compass.setDataRate(QMC5883_DATARATE_50HZ);
  compass.setSamples(QMC5883_SAMPLES_8);
}

void initDisplay(){
  display.begin();

  display.setContrast(50);

  display.display(); // show splashscreen
  delay(2000);
  display.clearDisplay();   // clears the screen and buffer

  display.setTextSize(1);
  display.setTextColor(BLACK);
  display.setCursor(0,0); 
}

float calcPosition(){
  
  for (int i=0;i&lt;SAMPLE_SIZE;i++){
   Vector norm = compass.readNormalize();

   // Calculate heading
   float heading = atan2(norm.YAxis, norm.XAxis);
   //Calculate magnetic declination according to your location
   //See http://magnetic-declination.com
   float declinationAngle = (0.0 + (53.0 / 60.0)) / (180 / PI);
   heading -= declinationAngle;

   // Correct for heading &lt; 0deg and heading &gt; 360deg
   if (heading &lt; 0){
     heading += 2 * PI;
   }

   if (heading &gt; 2 * PI){
     heading -= 2 * PI;
   }
   sum += heading;
  }
  
  float output = sum/SAMPLE_SIZE;
  sum = 0;
  float outDegrees = output*180/PI;
  
  Serial.println(outDegrees);
  
  return output;
  
}

void loop() {
   
   display.drawCircle(centerX, centerY, 23, BLACK);
   endX = 41 - (hyp * sin(calcPosition()-PI/2));
   endY = 23 - (hyp * cos(calcPosition()-PI/2));
   
   display.drawLine(centerX, centerY, endX, endY, BLACK);
   display.drawLine(centerX+1, centerY+1, endX+1, endY+1, BLACK);
   display.drawLine(centerX+2, centerY+2, endX+2, endY+2, BLACK);
   display.setCursor(endX + 4, endY - 4);
   display.println(&quot;N&quot;);
   display.display();
   delay(500);
   display.clearDisplay();
}</code></pre></pre>
</div>
<p>Here you can see how I calculated the heading in degrees accounting magnetic declination and how I drew a line that always point north. I had to make the line thicker so I needed to draw the line three times with each coordinates incremented by a pixel. Also, I drew a circle to make it more compass-like.</p>
<h3><strong>Video</strong></h3>
<p>Here's the demonstration of the project:</p>
<p><iframe loading="lazy" width="560" height="315" src="https://www.youtube.com/embed/V00U6ViXkpM" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="allowfullscreen"></iframe></p>
<p>Note that the HMC5833L must be calibrated before acquiring a more stable data. To calibrate, <strong>make figure 8's with the device</strong>.</p>
<p>That's it! Feel free to comment if you have any questions about this project. Happy building!</p>
<p>The post <a href="https://www.teachmemicro.com/arduino-compass-hmc5883l-magnetometer/">Arduino Compass with HMC5883L Magnetometer</a> appeared first on <a href="https://www.teachmemicro.com">Teach Me Microcontrollers!</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.teachmemicro.com/arduino-compass-hmc5883l-magnetometer/feed/</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
		<item>
		<title>Wireless Controller for Bluetooth RC Car</title>
		<link>https://www.teachmemicro.com/wireless-controller-bluetooth-rc-car/</link>
					<comments>https://www.teachmemicro.com/wireless-controller-bluetooth-rc-car/#respond</comments>
		
		<dc:creator><![CDATA[Roland Pelayo]]></dc:creator>
		<pubDate>Mon, 04 Feb 2019 03:29:06 +0000</pubDate>
				<category><![CDATA[Arduino Projects]]></category>
		<guid isPermaLink="false">https://www.teachmemicro.com/?p=2835</guid>

					<description><![CDATA[<p>This project demonstrates how two Bluetooth modules connect with each other. I’ll be featuring DFRobot’s Input Shield V2 as the transmitter/controller and Seeed’s ShieldBot, which was seen on my Bluetooth RC Car project, will act as the receiver. Setup The Bluetooth modules I’ll be using are the old but still popular HC-05 modules. The first &#8230;</p>
<p>The post <a href="https://www.teachmemicro.com/wireless-controller-bluetooth-rc-car/">Wireless Controller for Bluetooth RC Car</a> appeared first on <a href="https://www.teachmemicro.com">Teach Me Microcontrollers!</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>This project demonstrates how two Bluetooth modules connect with each other. I’ll be featuring <a href="https://www.dfrobot.com/product-62.html">DFRobot’s Input Shield V2</a> as the <a href="https://www.win-source.net/category/rf-if-and-rfid/rf-receiver-transmitter-and-transceiver-finished-units">transmitter/controller</a> and <a href="https://www.seeedstudio.com/Shield-Bot-p-1380.html">Seeed’s ShieldBot</a>, which was seen on my <a href="https://www.teachmemicro.com/bluetooth-rc-car/">Bluetooth RC Car</a> project, will act as the receiver.</p>
<p><span id="more-2835"></span></p>
<h3><strong>Setup</strong></h3>
<p>The Bluetooth modules I’ll be using are the old but still popular <a href="http://s.click.aliexpress.com/e/cWiMjE3m">HC-05 modules</a>. The first step is to make one module as master and another as slave. The module is slave by default so we only need to reconfigure one to be the master.</p>
<p>The HC-05 modules I own is the one without the KEY pin. In case you don’t know, the KEY pin, when driven high, gets the module into AT command mode.</p>
<p>My module contains a button for supposedly getting it to command mode by pressing and holding it before applying power to the module. It works but only during startup. To keep the module in command mode, pin 34 must be kept high. This is shown below:</p>
<p><a href="https://www.teachmemicro.com/wp-content/uploads/2019/02/modified_hc-05.jpg"><img loading="lazy" decoding="async" class="size-full wp-image-2836 aligncenter" src="https://www.teachmemicro.com/wp-content/uploads/2019/02/modified_hc-05.jpg" alt="Modified HC-05 Module" width="452" height="720" srcset="https://www.teachmemicro.com/wp-content/uploads/2019/02/modified_hc-05.jpg 452w, https://www.teachmemicro.com/wp-content/uploads/2019/02/modified_hc-05-188x300.jpg 188w" sizes="auto, (max-width: 452px) 100vw, 452px" /></a></p>
<p>The brown wire shown must be connected to the 3.3V pin. If successful, the red LED will flash once every two seconds.</p>
<p>I used an <a href="http://s.click.aliexpress.com/e/gebOYdS">FTDI USB to Serial converter</a> to configure the Bluetooth module once it’s in command mode. Then I used Arduino’s serial monitor to send out commands. The default baud rate for command mode is 38400.</p>
<p>To check if the HC-05 is indeed in command mode, I sent:</p>
<pre class="lang:arduino decode:true"><pre><code class="language-cpp">AT 
&gt; OK</code></pre></pre>
<p>"OK" here is the module's reply.</p>
<p>Next, I reset the configurations to its default values:</p>
<pre class="lang:arduino decode:true "><pre><code class="language-cpp">AT+ORGL
&gt; OK</code></pre></pre>
<p>Then, I set the module to master:</p>
<pre class="lang:arduino decode:true "><pre><code class="language-cpp">AT+ROLE=1 
&gt; OK</code></pre></pre>
<p>Next, reset the module:</p>
<pre class="lang:arduino decode:true "><pre><code class="language-cpp">AT+RESET 
&gt; OK</code></pre></pre>
<p>Then when its back, initialize:</p>
<pre class="lang:arduino decode:true "><pre><code class="language-cpp">AT+INIT 
&gt; OK</code></pre></pre>
<p>Now, I want the module to link with only one device: the slave HC-05 on my robot car. To forget all the previous connections, I sent:</p>
<pre class="lang:arduino decode:true "><pre><code class="language-cpp">AT+RMAAD 
&gt; OK</code></pre></pre>
<p>After this, I turned on the slave HC-05 because I needed to know its MAC address. Knowing the MAC address of available devices is simple. Just issue:</p>
<pre class="lang:arduino decode:true "><pre><code class="language-cpp">AT+INQ 
&gt; +INQ:98D3:31:FC20A9,1F00,7FFF</code></pre></pre>
<p>Here, it shows that my slave device has a MAC address of 98D3:31:FC20A9. The 1F00 is the device class while the 7FFF is the received signal strength indicator (RSSI).</p>
<p>To pair with the slave device:</p>
<pre class="lang:arduino decode:true "><pre><code class="language-cpp">AT+PAIR=98D3,31,FC20A9,20 
&gt; OK</code></pre></pre>
<p>The parameter for the AT+PAIR command is just the address of the slave address but is now separated by the comma instead of the colon and the timeout period (20 seconds in this example). After pairing, the LED on the master module will start flashing with about two seconds of pause.</p>
<p>Next, we bind the master and slave devices by using:</p>
<pre class="lang:arduino decode:true "><pre><code class="language-cpp">AT+BIND=98D3,31,FC20A9 
&gt; OK</code></pre></pre>
<p>Finally, we link the two devices through:</p>
<pre class="lang:arduino decode:true "><pre><code class="language-cpp">AT+LINK=98D3,31,FC20A9 
&gt; OK</code></pre></pre>
<p>If successful, both the master and slave device's LED will now blink twice followed by about two seconds of pause.</p>
<p>The good thing is even if I remove power from either master or slave, they will automatically connect with each other when both are turned on!</p>
<h3><strong>Wiring Up the Wireless Controller</strong></h3>
<p>Now that I’ve linked two Bluetooth modules together, it’s time to attach the master device to DFRobot’s Input shield. Here’s my setup:</p>
<p><a href="https://www.teachmemicro.com/wp-content/uploads/2019/02/image_from_ios.jpg"><img loading="lazy" decoding="async" class="size-large wp-image-2837 aligncenter" src="https://www.teachmemicro.com/wp-content/uploads/2019/02/image_from_ios-1024x704.jpg" alt="Setup for Bluetooth Controller" width="618" height="425" srcset="https://www.teachmemicro.com/wp-content/uploads/2019/02/image_from_ios-1024x704.jpg 1024w, https://www.teachmemicro.com/wp-content/uploads/2019/02/image_from_ios-300x206.jpg 300w, https://www.teachmemicro.com/wp-content/uploads/2019/02/image_from_ios-768x528.jpg 768w, https://www.teachmemicro.com/wp-content/uploads/2019/02/image_from_ios-110x75.jpg 110w" sizes="auto, (max-width: 618px) 100vw, 618px" /></a></p>
<p>I used an Arduino Mega with the shield but a UNO could work too. The Shield has an eight-pin female header on the top right side. It would’ve been awesome if the HC-05 fits there but it doesn’t. Naturally, I had to improvise. I still used the header but only its 5V and GND pins. The TX and RX pins of the module are connected to D13 and D13 respectively, via the ICSP header. The power to the controller is provided by a small power bank.</p>
<p>Here is the controller with the Shield-Bot:</p>
<p><a href="https://www.teachmemicro.com/wp-content/uploads/2019/02/bluetooth_controller_with_robot.jpg"><img loading="lazy" decoding="async" class="size-full wp-image-2838 aligncenter" src="https://www.teachmemicro.com/wp-content/uploads/2019/02/bluetooth_controller_with_robot.jpg" alt="Wireless Controller with Robot" width="596" height="539" srcset="https://www.teachmemicro.com/wp-content/uploads/2019/02/bluetooth_controller_with_robot.jpg 596w, https://www.teachmemicro.com/wp-content/uploads/2019/02/bluetooth_controller_with_robot-300x271.jpg 300w" sizes="auto, (max-width: 596px) 100vw, 596px" /></a></p>
<h3><strong>Arduino Sketch</strong></h3>
<p>The shield bot setup is the same as that in my <a href="https://www.teachmemicro.com/bluetooth-rc-car/">Bluetooth RC car project</a>. You can view the Arduino sketch for the robot there. In that project, the robot moves when the Bluetooth module receives the following commands:</p>
<ul>
<li>"f" -&gt; forward</li>
<li>"b" -&gt; backward</li>
<li>"l" -&gt; left</li>
<li>"r" -&gt; right</li>
</ul>
<p>So all I need to do is make the controller send these characters wirelessly. I decided to use only the controller's joystick for the moment.</p>
<p>After testing, I found out that the joystick Y-axis is connected to A2, and the X-axis is connected to A3. When the joystick is all the way to the right, the A3 value is 0. Similarly, when the joystick is all the way up, A2 is zero. This means all the way left and down could be 1023 for A3 and A2.</p>
<p>Here is my sketch for the controller:</p>
<pre class="lang:arduino decode:true "><pre><code class="language-cpp">#include &lt;SoftwareSerial.h&gt;

SoftwareSerial Bluetooth(13, 12); // RX, TX

#define up_button  8
#define down_button A1
#define left_button 9
#define right_button  12

#define stick_button  A0
#define level_stick A3
#define vertical_stick  A2

void setup() {
  Bluetooth.begin(9600);
  Serial.begin(9600);
  Serial.println(&quot;Waiting for command...&quot;);
  Bluetooth.println(&quot;Waiting for command&quot;);
  
  pinMode (left_button, INPUT);
  pinMode (right_button, INPUT);
  pinMode (up_button, INPUT);
  pinMode (down_button, INPUT);

  pinMode (stick_button  , INPUT);
  pinMode (level_stick   , INPUT);
  pinMode (vertical_stick, INPUT);
}

void loop() {
  int left_state = digitalRead (left_button);
  int right_state = digitalRead (right_button);
  int up_state = digitalRead (up_button);
  int down_state = digitalRead (down_button);
  int stick_state = digitalRead (stick_button);
  int level_value = analogRead (level_stick);
  int vertical_value = analogRead (vertical_stick);
  if(level_value &gt; 800){
    Bluetooth.println(&quot;l&quot;);
    Serial.println(&quot;l&quot;);
  }else if(level_value == 0){
    Bluetooth.println(&quot;r&quot;);
    Serial.println(&quot;r&quot;);
  }else if(vertical_value &gt; 800){
    Bluetooth.println(&quot;b&quot;);
    Serial.println(&quot;b&quot;);
  }else if(vertical_value == 0){
    Bluetooth.println(&quot;f&quot;);
    Serial.println(&quot;f&quot;);
  }else{
    Bluetooth.println(&quot;s&quot;);
    Serial.println(&quot;s&quot;);
  }
  delay (500);
}</code></pre></pre>
<p>That’s it! Thanks for viewing this project. If you have any concerns, kindly drop a comment!</p>
<p>The post <a href="https://www.teachmemicro.com/wireless-controller-bluetooth-rc-car/">Wireless Controller for Bluetooth RC Car</a> appeared first on <a href="https://www.teachmemicro.com">Teach Me Microcontrollers!</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.teachmemicro.com/wireless-controller-bluetooth-rc-car/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Obstacle-Avoiding Arduino Robot</title>
		<link>https://www.teachmemicro.com/arduino-robot-obstacle-avoiding/</link>
					<comments>https://www.teachmemicro.com/arduino-robot-obstacle-avoiding/#respond</comments>
		
		<dc:creator><![CDATA[Roland Pelayo]]></dc:creator>
		<pubDate>Tue, 29 Jan 2019 08:59:53 +0000</pubDate>
				<category><![CDATA[Arduino Projects]]></category>
		<guid isPermaLink="false">https://www.teachmemicro.com/?p=2744</guid>

					<description><![CDATA[<p>To kick off 2019, I would like to create a series of Arduino Robot projects that anyone can learn how to build. The first part of the series is the popular obstacle-avoiding Arduino robot. An obstacle-avoiding robot is a simple wheeled machine that avoids any obstacle in front of it. The obstacle can be detected &#8230;</p>
<p>The post <a href="https://www.teachmemicro.com/arduino-robot-obstacle-avoiding/">Obstacle-Avoiding Arduino Robot</a> appeared first on <a href="https://www.teachmemicro.com">Teach Me Microcontrollers!</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>To kick off 2019, I would like to create a series of Arduino Robot projects that anyone can learn how to build. The first part of the series is the popular obstacle-avoiding Arduino robot.</p>
<p><span id="more-2744"></span></p>
<p>An obstacle-avoiding robot is a simple wheeled machine that avoids any obstacle in front of it. The obstacle can be detected using ultrasonic or infrared signals; these signals can be reflected back to their source by solid objects. For this project, I will be using an infrared sensor to detect the obstacle.</p>
<h3><strong>Materials</strong></h3>
<p>I chose to buy my own robot body primarily to reduce building time and also because it's very cheap. The circular robot body comes with two geared DC motors with wheels and an AA battery holder. There are also holes on the chassis for mounting the Arduino board and other needed modules.</p>
<p><img loading="lazy" decoding="async" class="irc_mi aligncenter" src="https://ae01.alicdn.com/kf/HTB1R6VwbGSWBuNjSsrbq6y0mVXaJ/Smart-Car-Kit-2WD-Smart-Round-Robot-Car-Double-Layer-Chassis-Kit-With-Speed-Encoder-For.jpg" alt="Related image" width="369" height="369" /></p>
<p id="CbbBYMU">I decided to use three 18650 Li-Po batteries as power source which gives around 11 volts. The L298N module isn't like the one I featured in my tutorial but it's basically the same except for a power button which makes it better (because I don't have to include a switch on this robot).</p>
<p><a href="https://www.teachmemicro.com/wp-content/uploads/2019/01/arduino-robot-obstacle-avoiding-parts-list.jpg"><img loading="lazy" decoding="async" class="size-large wp-image-2805 aligncenter" src="https://www.teachmemicro.com/wp-content/uploads/2019/01/arduino-robot-obstacle-avoiding-parts-list-1024x562.jpg" alt="Arduino robot obstacle avoiding parts list" width="618" height="339" srcset="https://www.teachmemicro.com/wp-content/uploads/2019/01/arduino-robot-obstacle-avoiding-parts-list-1024x562.jpg 1024w, https://www.teachmemicro.com/wp-content/uploads/2019/01/arduino-robot-obstacle-avoiding-parts-list-300x165.jpg 300w, https://www.teachmemicro.com/wp-content/uploads/2019/01/arduino-robot-obstacle-avoiding-parts-list-768x422.jpg 768w, https://www.teachmemicro.com/wp-content/uploads/2019/01/arduino-robot-obstacle-avoiding-parts-list.jpg 1075w" sizes="auto, (max-width: 618px) 100vw, 618px" /></a></p>
<ul>
<li>Arduino UNO Rev3 or similar</li>
<li>Circular Robot Kit which contains:
<ul>
<li>2 x Geared DC Motors</li>
<li>Top and Bottom Chassis</li>
<li>2 x Caster Wheels</li>
<li>Metal Standoffs</li>
<li>Screws and Nuts</li>
</ul>
</li>
<li>Infrared Avoidance Sensor Module</li>
<li>L298N <a href="https://www.win-source.net/category/integrated-circuits-ics/pmic/pmic-motor-drivers-controllers">Motor Driver Module</a></li>
<li>3 x 18650 Batteries with Holder</li>
<li>Connecting Wires</li>
</ul>
<p>&nbsp;</p>
<p>Of course, any robot body can be used with this project, as long as we have the same type of obstacle sensor (IR), motor driver (L298N) and microcontroller (Arduino).</p>
<h3>Construction</h3>
<p>As mentioned, the robot chassis already has holes in it which made the assembly rather easy. However, it seems the chassis was not meant to be used with the materials I listed above so I had to improvise.</p>
<h4><strong>Step 1: Assembling the Robot Base</strong></h4>
<p>The robot chassis has two main parts: the bottom and the top part. The bottom part is where the main wheels and swivel caster wheels are attached. It is also the part where I attached the four IR sensors for obstacle detection.</p>
<p><a href="https://www.teachmemicro.com/wp-content/uploads/2019/01/step-1-assemble-robot-base.jpg"><img loading="lazy" decoding="async" class=" wp-image-2799 aligncenter" src="https://www.teachmemicro.com/wp-content/uploads/2019/01/step-1-assemble-robot-base.jpg" alt="" width="396" height="288" srcset="https://www.teachmemicro.com/wp-content/uploads/2019/01/step-1-assemble-robot-base.jpg 745w, https://www.teachmemicro.com/wp-content/uploads/2019/01/step-1-assemble-robot-base-300x218.jpg 300w" sizes="auto, (max-width: 396px) 100vw, 396px" /></a></p>
<p>As you can see, I've soldered wires to the motor. These are male-male jumper wires with the other end cut off for easy soldering.</p>
<p>The chassis comes with two caster wheels and has holes for both but I found out that the front caster wheel tends to lift up the base, hence, not allowing the bigger wheels to touch the ground properly. So I had to remove it (can be seen in the video below).</p>
<h4><strong>Step 2: Assembling the Robot Top</strong></h4>
<p>Next is to attach the rest of the components in the top part of the robot chassis. As I've mentioned, the top part was not meant to be used with the modules I've chosen for this project so I had to improvise a bit. I attached the battery holder, L298N module and IR sensor "motherboard" on one side:</p>
<p><a href="https://www.teachmemicro.com/wp-content/uploads/2019/01/step-2-assemble-robot-top.jpg"><img loading="lazy" decoding="async" class=" wp-image-2800 aligncenter" src="https://www.teachmemicro.com/wp-content/uploads/2019/01/step-2-assemble-robot-top.jpg" alt="" width="441" height="313" srcset="https://www.teachmemicro.com/wp-content/uploads/2019/01/step-2-assemble-robot-top.jpg 635w, https://www.teachmemicro.com/wp-content/uploads/2019/01/step-2-assemble-robot-top-300x213.jpg 300w" sizes="auto, (max-width: 441px) 100vw, 441px" /></a></p>
<p>I ran out of space! So I had to attach the Arduino UNO to the other side:</p>
<p><a href="https://www.teachmemicro.com/wp-content/uploads/2019/01/step-2-assemble-robot-top-other-side.jpg"><img loading="lazy" decoding="async" class=" wp-image-2801 aligncenter" src="https://www.teachmemicro.com/wp-content/uploads/2019/01/step-2-assemble-robot-top-other-side.jpg" alt="" width="379" height="310" srcset="https://www.teachmemicro.com/wp-content/uploads/2019/01/step-2-assemble-robot-top-other-side.jpg 530w, https://www.teachmemicro.com/wp-content/uploads/2019/01/step-2-assemble-robot-top-other-side-300x246.jpg 300w" sizes="auto, (max-width: 379px) 100vw, 379px" /></a></p>
<h4><strong>Step 3: Connect Some Wires</strong></h4>
<p>I used the following Fritzing and schematic diagrams for the wiring:</p>
<p><a href="https://www.teachmemicro.com/wp-content/uploads/2019/01/obstacle-avoiding-robot-fritzing-diagram.jpg"><img loading="lazy" decoding="async" class=" wp-image-2802 aligncenter" src="https://www.teachmemicro.com/wp-content/uploads/2019/01/obstacle-avoiding-robot-fritzing-diagram.jpg" alt="Obstacle avoiding robot Fritzing diagram" width="641" height="391" srcset="https://www.teachmemicro.com/wp-content/uploads/2019/01/obstacle-avoiding-robot-fritzing-diagram.jpg 838w, https://www.teachmemicro.com/wp-content/uploads/2019/01/obstacle-avoiding-robot-fritzing-diagram-300x183.jpg 300w, https://www.teachmemicro.com/wp-content/uploads/2019/01/obstacle-avoiding-robot-fritzing-diagram-768x468.jpg 768w" sizes="auto, (max-width: 641px) 100vw, 641px" /></a><a href="https://www.teachmemicro.com/wp-content/uploads/2019/01/obstacle-avoiding-robot-schematic-diagram.jpg"><img loading="lazy" decoding="async" class="size-full wp-image-2803 aligncenter" src="https://www.teachmemicro.com/wp-content/uploads/2019/01/obstacle-avoiding-robot-schematic-diagram.jpg" alt="Obstacle avoiding robot schematic diagram" width="771" height="487" srcset="https://www.teachmemicro.com/wp-content/uploads/2019/01/obstacle-avoiding-robot-schematic-diagram.jpg 771w, https://www.teachmemicro.com/wp-content/uploads/2019/01/obstacle-avoiding-robot-schematic-diagram-300x189.jpg 300w, https://www.teachmemicro.com/wp-content/uploads/2019/01/obstacle-avoiding-robot-schematic-diagram-768x485.jpg 768w" sizes="auto, (max-width: 771px) 100vw, 771px" /></a></p>
<h4><strong>Step 4: Connect the Bottom and Top Parts</strong></h4>
<p>I used the extra holes on the top part for the wires to go through, making the wiring a bit tidier. The finished Arduino robot now looks like this:</p>
<p><a href="https://www.teachmemicro.com/wp-content/uploads/2019/01/arduino-robot-obstacle-avoiding-full.jpg"><img loading="lazy" decoding="async" class="wp-image-2868 size-large aligncenter" src="https://www.teachmemicro.com/wp-content/uploads/2019/01/arduino-robot-obstacle-avoiding-full-1024x772.jpg" alt="Arduino Obstacle Avoiding Robot" width="618" height="466" srcset="https://www.teachmemicro.com/wp-content/uploads/2019/01/arduino-robot-obstacle-avoiding-full-1024x772.jpg 1024w, https://www.teachmemicro.com/wp-content/uploads/2019/01/arduino-robot-obstacle-avoiding-full-300x226.jpg 300w, https://www.teachmemicro.com/wp-content/uploads/2019/01/arduino-robot-obstacle-avoiding-full-768x579.jpg 768w, https://www.teachmemicro.com/wp-content/uploads/2019/01/arduino-robot-obstacle-avoiding-full-320x240.jpg 320w" sizes="auto, (max-width: 618px) 100vw, 618px" /></a></p>
<h4><strong>Step 5: Upload the Code</strong></h4>
<p>The Arduino sketch for the robot is not that complicated. The idea is that the robot should go away from the obstacle detected by the IR sensors. For example, when the left most sensor is triggered, the robot must backpedal, turn right and then go forward.</p>
<div class="hcb_wrap">
<pre class="prism undefined-numbers lang-cpp" data-lang="C++"><pre><code class="language-cpp">/* 
Obstacle Avoiding Arduino Robot Sketch
by Roland
www.teachmemicro.com/arduino-robot-obstacle-avoiding
*/

//Motor Driver Pins
int Motor_INA = 3;
int Motor_INB = 5;
int Motor_INC = 6;
int Motor_IND = 9;
//Sensor Pins
int Sensor_IN1 = 10;
int Sensor_IN2 = 11;
int Sensor_IN3 = 12;
int Sensor_IN4 = 13;

void setup() {
 pinMode(Motor_INA,OUTPUT);
 pinMode(Motor_INB,OUTPUT);
 pinMode(Motor_INC,OUTPUT);
 pinMode(Motor_IND,OUTPUT);

 pinMode(Sensor_IN1,INPUT);
 pinMode(Sensor_IN2,INPUT);
 pinMode(Sensor_IN3,INPUT);
 pinMode(Sensor_IN4,INPUT);
}

void goReverse(){
  analogWrite(Motor_INA,0);
  analogWrite(Motor_INB,50);
  analogWrite(Motor_INC,0);
  analogWrite(Motor_IND,50);
  delay(500);
}

void goRight(){
  analogWrite(Motor_INA,0);
  analogWrite(Motor_INB,0);
  analogWrite(Motor_INC,50);
  analogWrite(Motor_IND,0);
}

void goLeft(){
  analogWrite(Motor_INA,50);
  analogWrite(Motor_INB,0);
  analogWrite(Motor_INC,0);
  analogWrite(Motor_IND,0);
}

void stopBot(){
  analogWrite(Motor_INA,0);
  analogWrite(Motor_INB,0);
  analogWrite(Motor_INC,0);
  analogWrite(Motor_IND,0);
}

void goStraight(){
  analogWrite(Motor_INA,50);
  analogWrite(Motor_INB,0);
  analogWrite(Motor_INC,50);
  analogWrite(Motor_IND,0);
}
void loop() {
  delay(500);
  if(digitalRead(Sensor_IN1) == 0 || digitalRead(Sensor_IN2) == 0){
    goReverse();
    delay(100);
    stopBot();
    delay(100);
    goRight();
    delay(100);
    stopBot();
    delay(100);
    goStraight();
  }else if(digitalRead(Sensor_IN3) == 0 || digitalRead(Sensor_IN4) == 0){
    goReverse();
    delay(100);
    stopBot();
    delay(100);
    goLeft();
    delay(100);
    stopBot();
    delay(100);
    goStraight();
  }else if(digitalRead(Sensor_IN1) == 0 &amp;&amp; digitalRead(Sensor_IN2) == 0 &amp;&amp; digitalRead(Sensor_IN3) == 0 &amp;&amp; digitalRead(Sensor_IN4) == 0){
    stopBot();
  }else{
    goStraight();
  }
  
}</code></pre></pre>
</div>
<p>The IR sensor is basically an IR LED and receiver pair. When something is in front of it, the radiation from the IR LED bounces back to the receiver. If nothing is in front, the receiver will receive nothing. This concept is a simple "on and off" scheme.</p>
<p>So to detect an obstacle, I only had to read the pin where an IR sensor is connected. If it's high, there's something there. Otherwise, there's nothing in front of the sensor.</p>
<p>To move forward, I need to rotate the wheels in the same direction. I wanted the robot to not move too fast. In order to control its speed, I used PWM to drive the motors through the L298N driver module. Producing PWM is just using analogWrite() but of course, I had to make sure that the Arduino pins for motor control are PWM pins.</p>
<h3><strong>Video</strong></h3>
<p>The video below shows the whole construction and assembly process as well as a demonstration on how the Arduino robot runs:</p>
<p><iframe loading="lazy" title="Arduino Obstacle Avoiding Robot" width="618" height="348" src="https://www.youtube.com/embed/j8jiMq6HlZU?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe></p>
<p>Got any questions, tips or reactions? Kindly comment below or create a thread on our community page!</p>
<p>&nbsp;</p>
<p>The post <a href="https://www.teachmemicro.com/arduino-robot-obstacle-avoiding/">Obstacle-Avoiding Arduino Robot</a> appeared first on <a href="https://www.teachmemicro.com">Teach Me Microcontrollers!</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.teachmemicro.com/arduino-robot-obstacle-avoiding/feed/</wfw:commentRss>
			<slash:comments>0</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-09-04 16:18:33 by W3 Total Cache
-->