<?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 Archives | DMC, Inc.</title>
	<atom:link href="https://static.dmcinfo.com/blog/tag/arduino/feed/index.xml" rel="self" type="application/rss+xml" />
	<link></link>
	<description></description>
	<lastBuildDate>Thu, 06 Aug 2026 14:57:34 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.2</generator>

<image>
	<url>https://static.dmcinfo.com/wp-content/uploads/2025/04/site-icon-150x150.png</url>
	<title>Arduino Archives | DMC, Inc.</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Arduino Programming with VSCode</title>
		<link>https://static.dmcinfo.com/blog/24043/arduino-programming-with-vscode/</link>
		
		<dc:creator><![CDATA[DMC]]></dc:creator>
		<pubDate>Tue, 01 Aug 2017 12:27:47 +0000</pubDate>
				<category><![CDATA[Embedded Development & Programming]]></category>
		<category><![CDATA[Product Development]]></category>
		<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Visual Studio Code]]></category>
		<category><![CDATA[VSCode]]></category>
		<guid isPermaLink="false">https://static.dmcinfo.com/blog/24043/arduino-programming-with-vscode-2/</guid>

					<description><![CDATA[<p>One of our core business areas here at DMC is Embedded Development, where we assist customers in bringing their ideas into a functional device. Typically, these projects are done in a staged approach where an initial prototype is designed and built. This prototype can then be used to secure funding for additional stages of design, [&#8230;]</p>
<p>The post <a href="https://static.dmcinfo.com/blog/24043/arduino-programming-with-vscode/">Arduino Programming with VSCode</a> appeared first on <a href="https://static.dmcinfo.com/">DMC, Inc.</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">One of our core business areas here at DMC is <a href="https://static.dmcinfo.com/services/embedded-development-and-embedded-programming">Embedded Development</a>, where we assist customers in bringing their ideas into a functional device.</p>



<p class="wp-block-paragraph">Typically, these projects are done in a staged approach where an initial prototype is designed and built. This prototype can then be used to secure funding for additional stages of design, as well as ensure the product meets all of the requirements.</p>



<p class="wp-block-paragraph">For this type of rapid design and prototyping, we often utilize the <a href="https://www.arduino.cc/" target="_blank" rel="noreferrer noopener">Arduino</a>&nbsp;platform.</p>



<h2 id="h-what-s-up-with-arduino" class="wp-block-heading">What&#8217;s Up With Arduino?</h2>



<p class="wp-block-paragraph">Arduino&#8217;s come in a variety of configurations targeted at various <strong>Performance</strong>, <strong>IO</strong>, and <strong>Communication Requirements</strong>, so there is usually a model that has already been tried and tested that would work for your specific application. Arduino also provides a free IDE and C style programming language that allows you to start running custom code within a matter of minutes.</p>



<p class="wp-block-paragraph">However, once your program starts to stretch beyond a few hundred lines of code, or across more than a handful of files, the IDE can start to become cumbersome and difficult to work with, especially if you&#8217;re used to more advanced IDE&#8217;s or text editors. After dealing with the annoyances of the Arduino IDE for years, I&#8217;ve finally upgraded to a more user-friendly option: Visual Studio Code.</p>



<h2 id="h-what-is-vscode" class="wp-block-heading">What is VSCode?</h2>



<p class="wp-block-paragraph"><a href="https://code.visualstudio.com/" target="_blank" rel="noreferrer noopener">Visual Studio Code</a>, or VSCode, is an open source, lightweight, extensible text editor, that is designed and supported by <a href="https://www.microsoft.com/en-us/" target="_blank" rel="noreferrer noopener">Microsoft</a>, and runs on <a href="https://www.linux.org/" target="_blank" rel="noreferrer noopener">Linux</a> and Mac in addition to Windows. The extensibility allows you to write extensions to customize VSCode to best suit your needs, and programming language.</p>



<p class="wp-block-paragraph">With a broad user base, there are a lot of free extensions already available, including an extension to support Arduino programming without ever leaving the VSCode editor. VSCode is designed to handle multiple folders and files within the editor and utilized <a href="https://code.visualstudio.com/docs/editor/intellisense" target="_blank" rel="noreferrer noopener">Microsoft&#8217;s IntelliSense</a> auto-completion tool to help speed up your programming and reduce typos.</p>



<h2 id="h-using-vscode-for-arduino-projects" class="wp-block-heading">Using VSCode for Arduino Projects</h2>



<p class="wp-block-paragraph">To start using VSCode for your Arduino projects, follow the steps below.</p>



<h2 id="h-step-1-download-and-install-drivers" class="wp-block-heading">Step 1 &#8211; Download and Install Drivers</h2>



<p class="wp-block-paragraph">VSCode uses the drivers that are installed with the Arduino IDE, so we need to download and install that first.</p>



<ol class="wp-block-list">
<li><a href="https://www.arduino.cc/en/software/" type="link" id="https://www.arduino.cc/en/software/" target="_blank" rel="noreferrer noopener">Download the <strong>IDE</strong></a><strong> </strong>for your OS.<br> <br> <img decoding="async" src="https://static.dmcinfo.com/wp-content/uploads/2025/05/Picture-1-Download-the-Arduino-1.jpg" alt="Screenshot of Arduino IDE download page."><br>  </li>



<li>After the download is complete, run the installer, leaving all the default selections and options.<br>
 <br>
 <img decoding="async" alt="Screenshot of Arduino Setup Options." src="https://static.dmcinfo.com/wp-content/uploads/2025/05/Picture-2-Arduino-Setup-Installation-Options-1.jpg"><br>
 &nbsp;</li>



<li>Be sure to install the <strong>COM port</strong> and <strong>USB drivers</strong> if prompted.<br>
 <br>
 <img decoding="async" alt="Screenshot of permissions for COM port and USB driver installation." src="https://static.dmcinfo.com/wp-content/uploads/2025/05/Picture-3-Would-you-like-to-install-this-device-software-1.jpg"></li>
</ol>



<h2 id="h-step-2-download-vscode" class="wp-block-heading">Step 2 &#8211; Download VSCode</h2>



<ol class="wp-block-list">
<li>While the Arduino IDE is installing, <a href="https://code.visualstudio.com/Download?_exp_download=fb315fc982" type="link" id="https://code.visualstudio.com/Download?_exp_download=fb315fc982" target="_blank" rel="noreferrer noopener">download <strong>VSCode</strong></a>.<br> &nbsp;</li>



<li>After the download is complete, run the installer.<br>
 <br>
 <img decoding="async" alt="Screenshot of installer in Visual Studio Code - Select Additional Tasks." src="https://static.dmcinfo.com/wp-content/uploads/2025/05/Picture-4-Select-Additional-Tasks-1.jpg"></li>
</ol>



<h2 id="h-next-steps" class="wp-block-heading">Next Steps</h2>



<p class="wp-block-paragraph">I recommend the following configuration for the additional tasks during the install.</p>



<ol class="wp-block-list">
<li>Once installed, Launch VSCode.<br>
 &nbsp;</li>



<li>Click the <strong>Extensions</strong>&nbsp;button on the left side of the editor, (or Ctrl+Shift+X) to display the extension marketplace.<br>
 <br>
 <img decoding="async" alt="Screenshot of Visual Studio Code Extensions Marketplace." src="https://static.dmcinfo.com/wp-content/uploads/2025/05/Picture-5-Extension-Marketplace-1.jpg"><br>
 &nbsp;</li>



<li>Type <strong>Arduino</strong>&nbsp;in the search bar, to filter the extensions to only those related to the Arduino platform.<br>
 &nbsp;</li>



<li>There are a few extensions available for Arduino, but I prefer the one supported by Microsoft.<br>
 &nbsp;</li>



<li>Click the <strong>Install</strong>&nbsp;button&nbsp;and when prompted allow installation of dependencies.<br>
 &nbsp;</li>



<li>Click the <strong>Reload</strong>&nbsp;button to relaunch VSCode with the Arduino extension enabled.<br>
 <br>
 <img decoding="async" alt="Screenshot of Visual Studio Code Extensions Marketplace - Arduino Extension enabled." src="https://static.dmcinfo.com/wp-content/uploads/2025/05/Picture-6-Reload-With-Arduino-Extension-1.jpg"><br>
 &nbsp;</li>



<li>Depending on your firewall settings, you may be prompted to give VSCode firewall access.<br>
 <br>
 <img decoding="async" alt="Screenshot of Visual Studio Code - Firewall settings - Firewall Access." src="https://static.dmcinfo.com/wp-content/uploads/2025/05/Picture-7-VSCode-Firewall-Permission-1.jpg"></li>
</ol>



<h2 id="h-programming-in-vscode" class="wp-block-heading">Programming in VSCode</h2>



<p class="wp-block-paragraph">One more <strong>Reload</strong>&nbsp;and now, we&#8217;re set to start programming. To make sure everything is installed properly, we&#8217;ll start by opening up the example&nbsp;<strong>Blinky</strong>.</p>



<p class="wp-block-paragraph">Open VSCode&#8217;s <strong>Command Palette</strong>&nbsp;with <strong>Ctrl+Shift+P</strong>. Once the command palette is open, you can start typing to filter all the options or to search for useful commands.</p>



<ol class="wp-block-list">
<li>
<p class="wp-block-paragraph">Open the Command Palette and type <strong>Arduino</strong>, and select <strong>Arduino:Examples</strong>&nbsp;in the results list.<br> <br> <img decoding="async" src="https://static.dmcinfo.com/wp-content/uploads/2025/05/Picture-8-Command-Palette-Arduino-Examples-1.jpg" alt="Screenshot of Visual Studio Code Command Palette, selecting Arduino:Examples"></p>
</li>



<li>In the new pane to the right, select <strong>Built-in Examples&gt;&gt;01.Basics&gt;&gt;Blink</strong>. By default, this will open a new VSCode window, with the Blink sketch and a Blink.txt help file listed in the explorer pane on the left.<br>
 &nbsp;</li>



<li>Select <strong>Blink.ino</strong> from the explorer pane to open the sketch in the editor.<br>
 &nbsp;</li>



<li>Connect your Arduino board to your computer, and then, in the bottom bar of VSCode, you can specify the COM port for your Arduino and the board type.<br>
 <br>
 <img decoding="async" alt="Screenshot of VSCode permissions." src="https://static.dmcinfo.com/wp-content/uploads/2025/05/Picture-9-Firewall-for-Java-platform-SE-binary-1.jpg"><br>
 &nbsp;</li>



<li>Clicking <strong>COM </strong>opens a small selection window at the top of VSCode that should display your connected Arduino.<br>
 <br>
 <img decoding="async" alt="Screenshot of VSCode Blink test - Open COM window." src="https://static.dmcinfo.com/wp-content/uploads/2025/05/Picture-10-Visual-studio-code-blink-COM3-1.jpg"><br>
 &nbsp;</li>



<li>Clicking <strong>&lt;Select Board Type&gt;</strong> opens a new pane on the right where you can select the board type. If various configurations are available for your board, then you need to specify those as well. I&#8217;m using the Arduino Mega with the ATMega-2560.<br>
 <br>
 <img decoding="async" alt="Screenshot of board selection in VSCode." src="https://static.dmcinfo.com/wp-content/uploads/2025/05/Picture-11-Select-board-type-configuations-1.jpg"><br>
 &nbsp;</li>



<li>Open the <strong>Command Palette</strong> again (Ctrl+Shift+P) and type <strong>Arduino:Upload</strong>&nbsp;(Ctrl+Alt+U) to upload the Blink sketch to your Arduino. You should see LED13 start blinking about once per second.</li>
</ol>



<p class="wp-block-paragraph">Now you&#8217;re all set to start writing new Arduino sketches in VSCode and enjoy the features of a more advanced text editor.&nbsp;<a href="https://static.dmcinfo.com/services/embedded-development-and-embedded-programming"><strong>Learn more about DMC&#8217;s </strong>Embedded Development and Embedded Programming</a>&nbsp;or <a href="https://static.dmcinfo.com/contact">Contact Us</a> to get started on a project today.</p>
<p>The post <a href="https://static.dmcinfo.com/blog/24043/arduino-programming-with-vscode/">Arduino Programming with VSCode</a> appeared first on <a href="https://static.dmcinfo.com/">DMC, Inc.</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Turning an Arduino into a Modbus Device</title>
		<link>https://static.dmcinfo.com/blog/24183/turning-an-arduino-into-a-modbus-device/</link>
		
		<dc:creator><![CDATA[DMC]]></dc:creator>
		<pubDate>Mon, 26 Jun 2017 13:47:28 +0000</pubDate>
				<category><![CDATA[Embedded Development & Programming]]></category>
		<category><![CDATA[IoT]]></category>
		<category><![CDATA[Manufacturing Automation & Intelligence]]></category>
		<category><![CDATA[PLC]]></category>
		<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Modbus]]></category>
		<guid isPermaLink="false">https://static.dmcinfo.com/blog/24183/turning-an-arduino-into-a-modbus-device-2/</guid>

					<description><![CDATA[<p>A situation arises&#160;where you have a system that includes a PLC, an HMI, and some peripheral devices.&#160;The HMI displays some information to a user and allows for some basic control&#160;and the PLC manages data collection and communication through an RS485 network using a Modbus RTU protocol. Easy, right? Many PLCs already support RS485 and Modbus [&#8230;]</p>
<p>The post <a href="https://static.dmcinfo.com/blog/24183/turning-an-arduino-into-a-modbus-device/">Turning an Arduino into a Modbus Device</a> appeared first on <a href="https://static.dmcinfo.com/">DMC, Inc.</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph"><b>A situation arises&nbsp;</b>where you have a system that includes a PLC, an HMI, and some peripheral devices.&nbsp;The HMI displays some information to a user and allows for some basic control&nbsp;and the PLC manages data collection and communication through an RS485 network using a Modbus RTU protocol.</p>



<p class="wp-block-paragraph">Easy, right? Many PLCs already support RS485 and Modbus communication and only require a few functions blocks.</p>



<p class="wp-block-paragraph"><strong>There is a twist</strong>, however.&nbsp;The devices with which the PLC is communicating are Arduinos. <strong>Lots of them.</strong></p>



<p class="wp-block-paragraph"><em>So, how can you program an Arduino to act as a Modbus device?</em></p>



<p class="wp-block-paragraph">Turns out the answer is easy.</p>



<p class="wp-block-paragraph"><strong>In this post, I&#8217;ll talk about adding an Arduino to an RS485 network and programming it to function as a Modbus slave device.</strong></p>



<h2 id="h-how-an-arduino-can-speak-over-an-rs485" class="wp-block-heading">How an Arduino can Speak over an RS485</h2>



<p class="wp-block-paragraph">First, let’s talk about Arduino and RS485.</p>



<p class="wp-block-paragraph">Although Arduino supports serial communication through its built-in UART (Universally Asynchronous Receiver/Transmitter), it uses TTL (Transistor-Transistor Logic), not RS485. Both signaling types use serial communication. Serial communication<strong>&nbsp;means data is sent one bit at a time at a specified BAUD rate</strong> (bits per second).&nbsp;</p>



<p class="wp-block-paragraph">However,&nbsp;TTL is single-ended, whereas RS485 relies on a differential signal (<a href="https://community.plm.automation.siemens.com/t5/Testing-Knowledge-Base/Single-Ended-vs-Differential-Inputs/ta-p/355022" rel="no follow" target="_blank">what&#8217;s the difference?</a>). This is far more useful in an industrial setting where signals can be influenced by electrical noise, and devices can be separated dozens or hundreds of meters.</p>



<p class="wp-block-paragraph">To allow an Arduino to speak over an RS485 network, <strong>an additional device must be used to convert TTL to RS485</strong>. There are several devices on the market that do this, but I used this <em>RS485 Transceiver Breakout Board</em>.</p>



<h2 id="h-hardware-set-up-and-serial-ports" class="wp-block-heading">Hardware Set-Up and Serial Ports</h2>



<p class="wp-block-paragraph">This picture shows my hardware setup.</p>



<h2 id="h-" class="wp-block-heading"></h2>



<p class="wp-block-paragraph">Because I’m using an Arduino Mega, 4 serial ports available. If you are unsure how many ports you have, the <a href="https://www.arduino.cc/en/Reference/Serial" rel="no follow" target="_blank">Arduino website</a>&nbsp;provides a description of serial ports on all board models.</p>



<p class="wp-block-paragraph">Did you run out of Serial ports? Don’t forget about <a href="https://www.arduino.cc/en/Reference/SoftwareSerial" rel="no follow" target="_blank">Software Serial</a>!</p>



<figure class="wp-block-image"><img decoding="async" src="https://static.dmcinfo.com/wp-content/uploads/2025/05/Setup-2.jpg" alt=""/></figure>



<p class="wp-block-paragraph">Great! Your Arduino is now&nbsp;networked to the PLC (or another master device) and other non-master devices.</p>



<h2 id="h-installing-arduino-ide" class="wp-block-heading">Installing Arduino IDE</h2>



<p class="wp-block-paragraph">Next is software. I’ll be using the Arduino IDE for software development.</p>



<p class="wp-block-paragraph"><strong>Although Modbus was developed in 1979, it has stood the test of time and is proven to still a reliable industrial communication protocol.</strong> You may choose to write your own C++ library implementation of Modbus, but I opted to use a prewritten library.</p>



<p class="wp-block-paragraph">A quick Google search yields several options. I chose <a href="https://github.com/smarmengol/Modbus-Master-Slave-for-Arduino/blob/master/ModbusRtu.h" rel="no follow">this one</a>.</p>



<h2 id="h-include-modbusrtu-library" class="wp-block-heading">Include ModbusRtu Library</h2>



<p class="wp-block-paragraph">The first step is to include the ModbusRtu library in your sketch.</p>



<p class="wp-block-paragraph"><em>The Modbus constructor takes three parameters:</em></p>



<ol class="wp-block-list">
<li><strong>Slave Address</strong> – this is the Modbus address that the master device will use to make specific queries. In other words, it’s how other devices in the network know who is talking. The address can be any number between 1 and 247; address 0 is reserved for the master. It’s important to ensure there are no duplicate addresses on the network.</li>
</ol>



<ol class="wp-block-list" start="2">
 

<li><strong>Serial Port</strong> &ndash; this tells the library which Serial port to use. Here is the list of options, depending on the number of ports available on your Arduino model:

 

<p class="wp-block-paragraph">0 = Serial</p>



 

<p class="wp-block-paragraph">1 = Serial1</p>



 

<p class="wp-block-paragraph">2 = Serial2</p>



 

<p class="wp-block-paragraph">3 = Serial3</p>



 

<p class="wp-block-paragraph">4 = SoftwareSerial</p>


 </li>


 

<li><strong>TX Enable Pin</strong> &ndash; because this is a 2-wire RS485 network, it is only half duplex. <strong>Half duplex means that a device can only receive or transmit at a time. </strong>By default, the Arduino is in receive mode. Setting the TX enable pin high allows the Arduino to transmit data.</li>


</ol>



<h2 id="h-modbus-array-example" class="wp-block-heading">Modbus_array Example</h2>



<p class="wp-block-paragraph">Below is example code that shows<strong> how we can use our newly created RS485 network and Modbus library</strong><em> </em>using a&nbsp;very simple (and probably unrealistic) scenario.</p>



<p class="wp-block-paragraph">In this scenario, our Arduino is hooked<strong> </strong>up to a toggle switch and an LED. We&nbsp;have an operator who wants to know the state of the switch (on or off) and the ability to remotely toggle the LED. What should we do?</p>



<p class="wp-block-paragraph">First, look at how the Modbus library function that allows for reading and writing:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">C++</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>Modbus::poll(uint16_t *regs, uint8_t u8size)</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #4EC9B0">Modbus</span><span style="color: #D4D4D4">::</span><span style="color: #DCDCAA">poll</span><span style="color: #D4D4D4">(</span><span style="color: #569CD6">uint16_t</span><span style="color: #D4D4D4"> *regs, </span><span style="color: #569CD6">uint8_t</span><span style="color: #D4D4D4"> u8size)</span></span></code></pre></div>



<p class="wp-block-paragraph"><em>*regs is a register table used for communication exchange, and u8size is the size of the register table.</em></p>



<p class="wp-block-paragraph">This method checks if there is an incoming query in its serial buffer. If there is, the library will validate the message (check the device address, data length, and CRC) and subsequently perform the correct function.</p>



<p class="wp-block-paragraph">In other words, to use this function, we must pass it an unsigned 16-bit integer array and its length. The array will contain the data that the master device is reading or writing over.</p>



<p class="wp-block-paragraph">For this simple example, I created an array called <strong>modbus_array</strong> that contains two elements.</p>



<ol class="wp-block-list">
<li>The first is a status bit for the LED. If the value of the first element is 0, the LED will turn off; otherwise, it will be on.</li>



<li>The second element will be the state of the switch.</li>
</ol>



<p class="wp-block-paragraph">Now, let’s look at this from the perspective of the master device.</p>



<p class="wp-block-paragraph"><strong>Toggling the LED</strong></p>



<ol class="wp-block-list">
<li>To toggle the LED, it must send a Modbus message to slave address 1 that writes a single holding register (Modbus function #6) to the first element with a value of 0 or 1.</li>



<li>To read the toggle switch, it must send a message that reads holding registers (Modbus function #3) starting at the second element with a length of 1. The Arduino will respond with the second element.</li>
</ol>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e"><span style="background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e">C++</span></span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>// Includes
#include &lt;ModbusRtu.h>

// Defines
#define LED_PIN 5
#define SWITCH_PIN 6

// Global variables
Modbus modbus_port;

// Modbus registers:
// &#91;0&#93; = LED control
// &#91;1&#93; = Switch state
uint16_t modbus_array[] = {0, 0};

void setup()
{
    // Create Modbus slave:
    // (slave ID = 1, serial port = 1, TX enable pin = 7)
    modbus_port = Modbus(1, 1, 7);

    // Start Modbus communication
    modbus_port.begin(9600);

    pinMode(LED_PIN, OUTPUT);
    pinMode(SWITCH_PIN, INPUT);
}

void loop()
{
    // Process Modbus requests
    modbus_port.poll(
        modbus_array,
        sizeof(modbus_array) / sizeof(modbus_array&#91;0&#93;)
    );

    // Register 0 controls the LED
    if (modbus_array&#91;0&#93; == 0)
    {
        digitalWrite(LED_PIN, LOW);
    }
    else
    {
        digitalWrite(LED_PIN, HIGH);
    }

    // Register 1 reports the switch state
    modbus_array&#91;1&#93; = digitalRead(SWITCH_PIN);
}</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #6A9955">// Includes</span></span>
<span class="line"><span style="color: #C586C0">#include</span><span style="color: #569CD6"> </span><span style="color: #CE9178">&lt;ModbusRtu.h&gt;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955">// Defines</span></span>
<span class="line"><span style="color: #C586C0">#define</span><span style="color: #569CD6"> LED_PIN 5</span></span>
<span class="line"><span style="color: #C586C0">#define</span><span style="color: #569CD6"> SWITCH_PIN 6</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955">// Global variables</span></span>
<span class="line"><span style="color: #D4D4D4">Modbus modbus_port;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955">// Modbus registers:</span></span>
<span class="line"><span style="color: #6A9955">// &#91;0&#93; = LED control</span></span>
<span class="line"><span style="color: #6A9955">// &#91;1&#93; = Switch state</span></span>
<span class="line"><span style="color: #569CD6">uint16_t</span><span style="color: #D4D4D4"> modbus_array[] = {</span><span style="color: #B5CEA8">0</span><span style="color: #D4D4D4">, </span><span style="color: #B5CEA8">0</span><span style="color: #D4D4D4">};</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">void</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">setup</span><span style="color: #D4D4D4">()</span></span>
<span class="line"><span style="color: #D4D4D4">{</span></span>
<span class="line"><span style="color: #6A9955">    // Create Modbus slave:</span></span>
<span class="line"><span style="color: #6A9955">    // (slave ID = 1, serial port = 1, TX enable pin = 7)</span></span>
<span class="line"><span style="color: #D4D4D4">    modbus_port = </span><span style="color: #DCDCAA">Modbus</span><span style="color: #D4D4D4">(</span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4">, </span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4">, </span><span style="color: #B5CEA8">7</span><span style="color: #D4D4D4">);</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955">    // Start Modbus communication</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #9CDCFE">modbus_port</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">begin</span><span style="color: #D4D4D4">(</span><span style="color: #B5CEA8">9600</span><span style="color: #D4D4D4">);</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">pinMode</span><span style="color: #D4D4D4">(LED_PIN, OUTPUT);</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #DCDCAA">pinMode</span><span style="color: #D4D4D4">(SWITCH_PIN, INPUT);</span></span>
<span class="line"><span style="color: #D4D4D4">}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">void</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">loop</span><span style="color: #D4D4D4">()</span></span>
<span class="line"><span style="color: #D4D4D4">{</span></span>
<span class="line"><span style="color: #6A9955">    // Process Modbus requests</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #9CDCFE">modbus_port</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">poll</span><span style="color: #D4D4D4">(</span></span>
<span class="line"><span style="color: #D4D4D4">        modbus_array,</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #569CD6">sizeof</span><span style="color: #D4D4D4">(modbus_array) / </span><span style="color: #569CD6">sizeof</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">modbus_array</span><span style="color: #D4D4D4">&#91;</span><span style="color: #B5CEA8">0</span><span style="color: #D4D4D4">&#93;)</span></span>
<span class="line"><span style="color: #D4D4D4">    );</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955">    // Register 0 controls the LED</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #C586C0">if</span><span style="color: #D4D4D4"> (</span><span style="color: #9CDCFE">modbus_array</span><span style="color: #D4D4D4">&#91;</span><span style="color: #B5CEA8">0</span><span style="color: #D4D4D4">&#93; == </span><span style="color: #B5CEA8">0</span><span style="color: #D4D4D4">)</span></span>
<span class="line"><span style="color: #D4D4D4">    {</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #DCDCAA">digitalWrite</span><span style="color: #D4D4D4">(LED_PIN, LOW);</span></span>
<span class="line"><span style="color: #D4D4D4">    }</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #C586C0">else</span></span>
<span class="line"><span style="color: #D4D4D4">    {</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #DCDCAA">digitalWrite</span><span style="color: #D4D4D4">(LED_PIN, HIGH);</span></span>
<span class="line"><span style="color: #D4D4D4">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color: #6A9955">    // Register 1 reports the switch state</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #9CDCFE">modbus_array</span><span style="color: #D4D4D4">&#91;</span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4">&#93; = </span><span style="color: #DCDCAA">digitalRead</span><span style="color: #D4D4D4">(SWITCH_PIN);</span></span>
<span class="line"><span style="color: #D4D4D4">}</span></span></code></pre></div>



<h2 id="h-conclusion" class="wp-block-heading">Conclusion</h2>



<p class="wp-block-paragraph">Arduinos are usually confined to hobbyist and educational markets because of their low barrier to entry, but this also makes them ideal candidates for prototyping, even in the industrial automation space.</p>



<p class="wp-block-paragraph">Do you have a low fidelity project that could use some low-cost microcontrollers? Check out our <a href="https://static.dmcinfo.com/services/embedded-development-and-embedded-programming">Embedded Development and Programming</a> service area to find out if DMC could be good fit for you! Or, <a href="https://static.dmcinfo.com/contact">Contact Us</a> to get started on a solution today.</p>



<p class="wp-block-paragraph"></p>
<p>The post <a href="https://static.dmcinfo.com/blog/24183/turning-an-arduino-into-a-modbus-device/">Turning an Arduino into a Modbus Device</a> appeared first on <a href="https://static.dmcinfo.com/">DMC, Inc.</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
