<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[[SOLVED] UART and MH-Z16 CO2 sensor]]></title><description><![CDATA[<p dir="auto">Hello there! :)</p>
<p dir="auto">I am having some trouble communicating with this CO2 sensor.<br />
<a href="https://wiki.dfrobot.com/Infrared_CO2_Sensor_0-50000ppm_SKU__SEN0220#More" target="_blank" rel="noopener noreferrer nofollow">This</a> it's wiki with sample code, and this is the <a href="https://github.com/Arduinolibrary/DFRobot_Gravity_UART_Infrared_CO2_Sensor/raw/master/MH-Z16%20CO2%20Datasheet.pdf" target="_blank" rel="noopener noreferrer nofollow">datasheet</a>.</p>
<p dir="auto">Okay so I think my issue is related to byte conversion but not sure.</p>
<pre><code>from machine import UART
uart = UART(1, 9600) 
uart.write(b'\xFF\x01\x86\x00\x00\x00\x00\x00\x79') # This is what should trigger the measure 
uart.read(9) #b'\xff\x86\x01\xecF\x00\x00\x00G' #This is the answer
</code></pre>
<p dir="auto">I am not sure how to process the read value.. The first 2 number is always ff and 86 the next is always small, then the 4th is something weird.. then always 2 zero, and zero with a letter in the and.<br />
So do you have any idea how can I get proper measurment out of it? Thank you!</p>
]]></description><link>https://forum.pycom.io/topic/4821/solved-uart-and-mh-z16-co2-sensor</link><generator>RSS for Node</generator><lastBuildDate>Fri, 16 Oct 2026 11:32:12 GMT</lastBuildDate><atom:link href="https://forum.pycom.io/topic/4821.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 12 May 2019 18:45:13 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [SOLVED] UART and MH-Z16 CO2 sensor on Sun, 12 May 2019 20:05:38 GMT]]></title><description><![CDATA[<p dir="auto">Hello there! :)</p>
<p dir="auto">I am having some trouble communicating with this CO2 sensor.<br />
<a href="https://wiki.dfrobot.com/Infrared_CO2_Sensor_0-50000ppm_SKU__SEN0220#More" target="_blank" rel="noopener noreferrer nofollow">This</a> it's wiki with sample code, and this is the <a href="https://github.com/Arduinolibrary/DFRobot_Gravity_UART_Infrared_CO2_Sensor/raw/master/MH-Z16%20CO2%20Datasheet.pdf" target="_blank" rel="noopener noreferrer nofollow">datasheet</a>.</p>
<p dir="auto">Okay so I think my issue is related to byte conversion but not sure.</p>
<pre><code>from machine import UART
uart = UART(1, 9600) 
uart.write(b'\xFF\x01\x86\x00\x00\x00\x00\x00\x79') # This is what should trigger the measure 
uart.read(9) #b'\xff\x86\x01\xecF\x00\x00\x00G' #This is the answer
</code></pre>
<p dir="auto">I am not sure how to process the read value.. The first 2 number is always ff and 86 the next is always small, then the 4th is something weird.. then always 2 zero, and zero with a letter in the and.<br />
So do you have any idea how can I get proper measurment out of it? Thank you!</p>
]]></description><link>https://forum.pycom.io/post/27828</link><guid isPermaLink="true">https://forum.pycom.io/post/27828</guid><dc:creator><![CDATA[tttadam]]></dc:creator><pubDate>Sun, 12 May 2019 20:05:38 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] UART and MH-Z16 CO2 sensor on Sun, 12 May 2019 19:47:50 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/3340">@tttadam</a> Look at the struct module. It allows to unpack a bytearray into individual data elements. You can also make it simpler,. just following the manual:</p>
<pre><code>from machine import UART
uart = UART(1, 9600) 
uart.write(b'\xFF\x01\x86\x00\x00\x00\x00\x00\x79') # This is what should trigger the measure 
response = bytearray(uart.read(9)) #
concentration = response[2] * 256 + response[3]
checksum = (256 - sum(response[1:-1])) % 256
print(concentration)
</code></pre>
<p dir="auto">The last byte is the checksum. It looks as if the sample C code in the data sheet is wrong. The variable checksum is not initialized to 0.<br />
Edit: Checksum simplified</p>
]]></description><link>https://forum.pycom.io/post/27829</link><guid isPermaLink="true">https://forum.pycom.io/post/27829</guid><dc:creator><![CDATA[robert-hh]]></dc:creator><pubDate>Sun, 12 May 2019 19:47:50 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] UART and MH-Z16 CO2 sensor on Sun, 12 May 2019 19:51:51 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/98">@robert-hh</a> Thank you. The code is working, but the values seems to be off, and I am not sure how should I understood the checksum. Should I calibrate the sensor somehow?<br />
response: 586<br />
checksum: -29</p>
<p dir="auto">response: 590<br />
checksum: -33</p>
<p dir="auto">response: 667<br />
checksum: -112</p>
]]></description><link>https://forum.pycom.io/post/27830</link><guid isPermaLink="true">https://forum.pycom.io/post/27830</guid><dc:creator><![CDATA[tttadam]]></dc:creator><pubDate>Sun, 12 May 2019 19:51:51 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] UART and MH-Z16 CO2 sensor on Sun, 12 May 2019 19:55:17 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/3340">@tttadam</a> The checksum is just to be used to verify, that the data was received correctly. The calculated value should be identical to response[8]. Note that I changed the code, such that the checksum is always positive.</p>
]]></description><link>https://forum.pycom.io/post/27831</link><guid isPermaLink="true">https://forum.pycom.io/post/27831</guid><dc:creator><![CDATA[robert-hh]]></dc:creator><pubDate>Sun, 12 May 2019 19:55:17 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] UART and MH-Z16 CO2 sensor on Sun, 12 May 2019 20:02:35 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/3340">@tttadam</a> Using struct.unpack, you can also use:</p>
<pre><code>from machine import UART
from struct import unpack
uart = UART(1, 9600) 
uart.write(b'\xFF\x01\x86\x00\x00\x00\x00\x00\x79') # This is what should trigger the measure 
response = uart.read(9)
cmd, concentration, check = unpack(&quot;&gt;xBh4xB&quot;, response)
checksum = (256 - sum(response[1:-1])) % 256
print(concentration, checksum == check)
</code></pre>
]]></description><link>https://forum.pycom.io/post/27832</link><guid isPermaLink="true">https://forum.pycom.io/post/27832</guid><dc:creator><![CDATA[robert-hh]]></dc:creator><pubDate>Sun, 12 May 2019 20:02:35 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] UART and MH-Z16 CO2 sensor on Sun, 12 May 2019 20:03:43 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/98">@robert-hh</a> Thank you! :)<br />
I got it now. It's work like it's should.<br />
Maybe I should to open the window :D</p>
]]></description><link>https://forum.pycom.io/post/27833</link><guid isPermaLink="true">https://forum.pycom.io/post/27833</guid><dc:creator><![CDATA[tttadam]]></dc:creator><pubDate>Sun, 12 May 2019 20:03:43 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] UART and MH-Z16 CO2 sensor on Mon, 09 Sep 2019 20:06:27 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/98">@robert-hh</a> said in <a href="/post/27832">[SOLVED] UART and MH-Z16 CO2 sensor</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/3340">@tttadam</a> Using struct.unpack, you can also use:</p>
<pre><code>from machine import UART
from struct import unpack
uart = UART(1, 9600) 
uart.write(b'\xFF\x01\x86\x00\x00\x00\x00\x00\x79') # This is what should trigger the measure 
response = uart.read(9)
cmd, concentration, check = unpack(&quot;&gt;xBh4xB&quot;, response)
checksum = (256 - sum(response[1:-1])) % 256
print(concentration, checksum == check)
</code></pre>
</blockquote>
<p dir="auto">FYI, nowadays when I try this code I got ValueError: bad typecode error. I am not sure if this was the case back in May.</p>
]]></description><link>https://forum.pycom.io/post/29425</link><guid isPermaLink="true">https://forum.pycom.io/post/29425</guid><dc:creator><![CDATA[tttadam]]></dc:creator><pubDate>Mon, 09 Sep 2019 20:06:27 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] UART and MH-Z16 CO2 sensor on Tue, 10 Sep 2019 07:32:40 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/3340">@tttadam</a> It looks like the x type code is not supported by MicroPython. Use:</p>
<pre><code>d1, cmd, concentration, d2, check = unpack(&quot;&gt;bBhiB&quot;, s)
</code></pre>
<p dir="auto">d1 and d2 are dummy values, which can be ignored.</p>
]]></description><link>https://forum.pycom.io/post/29427</link><guid isPermaLink="true">https://forum.pycom.io/post/29427</guid><dc:creator><![CDATA[robert-hh]]></dc:creator><pubDate>Tue, 10 Sep 2019 07:32:40 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] UART and MH-Z16 CO2 sensor on Tue, 10 Sep 2019 17:58:18 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/98">@robert-hh</a> Yep, Thank You! :)</p>
]]></description><link>https://forum.pycom.io/post/29432</link><guid isPermaLink="true">https://forum.pycom.io/post/29432</guid><dc:creator><![CDATA[tttadam]]></dc:creator><pubDate>Tue, 10 Sep 2019 17:58:18 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] UART and MH-Z16 CO2 sensor on Wed, 18 Sep 2019 06:30:59 GMT]]></title><description><![CDATA[<p dir="auto">I found an interesting bug/glitch today. The sensor only works one time after I upload the code.<br />
When I plug it out, and hook it to a power brick, or back to the computer it does not work. sends back false data. every other uart and I2c sensor is OK, but not this.</p>
<p dir="auto">Any tipp how this is possible? It is so weird I don't even know how to start the investigation :D</p>
]]></description><link>https://forum.pycom.io/post/29484</link><guid isPermaLink="true">https://forum.pycom.io/post/29484</guid><dc:creator><![CDATA[tttadam]]></dc:creator><pubDate>Wed, 18 Sep 2019 06:30:59 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] UART and MH-Z16 CO2 sensor on Wed, 18 Sep 2019 06:43:54 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/3340">@tttadam</a> Is there a difference in how you start the code?<br />
a) After uploading. How do you upload? And how do you start the code? Do you for instance push reset?<br />
b) After reset: The code should be executed normally. Can you verify after reset, that the code is really loaded permanently to the device. For verification,  do not use the tools you used for uploading. Use something different. If you uploaded for instance  with pymakr, use ftp/Filezilla for verification.</p>
]]></description><link>https://forum.pycom.io/post/29485</link><guid isPermaLink="true">https://forum.pycom.io/post/29485</guid><dc:creator><![CDATA[robert-hh]]></dc:creator><pubDate>Wed, 18 Sep 2019 06:43:54 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] UART and MH-Z16 CO2 sensor on Thu, 19 Sep 2019 05:44:37 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/98">@robert-hh</a> Well, I hit upload in pymakr, and then the code start by itself. Or when I plug in to the usb, or to a powerbrick then it's also start by itself.<br />
The device sends data over mqtt, so even when it's not connected to the pc I have some sense what's going on. (That how I realised that the data what goes to the server is corrupted)</p>
]]></description><link>https://forum.pycom.io/post/29492</link><guid isPermaLink="true">https://forum.pycom.io/post/29492</guid><dc:creator><![CDATA[tttadam]]></dc:creator><pubDate>Thu, 19 Sep 2019 05:44:37 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] UART and MH-Z16 CO2 sensor on Mon, 14 Oct 2019 05:51:05 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/3340">@tttadam</a> I think I find the issue.<br />
If I shut the pycom down by pluging it out (like the 99,9% of the cases) the sensor wont work.<br />
But if I reboot it after I pluged-in then it does work.<br />
So my &quot;solution&quot;/workaround is that I check in the <a href="http://boot.py" target="_blank" rel="noopener noreferrer nofollow">boot.py</a> if the checksum is false, if it is, then I do a machine.reset() . Any more elegant solution is welcomed :D</p>
]]></description><link>https://forum.pycom.io/post/29830</link><guid isPermaLink="true">https://forum.pycom.io/post/29830</guid><dc:creator><![CDATA[tttadam]]></dc:creator><pubDate>Mon, 14 Oct 2019 05:51:05 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] UART and MH-Z16 CO2 sensor on Wed, 15 Jun 2022 20:59:19 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/98">@robert-hh</a> I have run your code in rpi 3B but it causes an error as the machine is not found what should we write instead?</p>
]]></description><link>https://forum.pycom.io/post/40474</link><guid isPermaLink="true">https://forum.pycom.io/post/40474</guid><dc:creator><![CDATA[saman naz]]></dc:creator><pubDate>Wed, 15 Jun 2022 20:59:19 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] UART and MH-Z16 CO2 sensor on Thu, 16 Jun 2022 07:05:57 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/10297">@saman-naz</a> This code is not made for a linux based RPI.</p>
]]></description><link>https://forum.pycom.io/post/40476</link><guid isPermaLink="true">https://forum.pycom.io/post/40476</guid><dc:creator><![CDATA[robert-hh]]></dc:creator><pubDate>Thu, 16 Jun 2022 07:05:57 GMT</pubDate></item></channel></rss>