<?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[Reading analog voltage with ADC and attenuation]]></title><description><![CDATA[<p dir="auto">Hi all,</p>
<p dir="auto">I am playing with a rangefinder ultrasonic sensor (<a href="http://www.maxbotix.com/Ultrasonic_Sensors/MB7360.htm" target="_blank" rel="noopener noreferrer nofollow">MB7360 from Maxbotix</a>) and I have some question regarding the way to correctly read the sensor values.</p>
<p dir="auto">The sensor operates at voltage between 2.7V and 5.5V, so I hooked it up on the 3.3V pin of the expansion board.</p>
<p dir="auto">I am using the sensor's analog voltage output to read the distance measured by the sensor. In the spec-sheet it is mentioned that</p>
<ul>
<li>the output is referenced to the sensor ground and Vcc (which is 3.3V, right?)</li>
<li>the scale factor is Vcc / 5120 per 1 mm</li>
<li>using a 10 bits ADC, we just need to multiply the voltage read by 5 to obtain the distance</li>
</ul>
<p dir="auto">Since the maximum voltage output is 3.3V, I used an attenuation of 11 dB and I am using this piece of code to get the distance:</p>
<pre><code>from machine import ADC

# setting up the Analog/Digital Converter with 10 bits
adc = ADC(bits=10)

# create an analog pin on P20 for the ultrasonic sensor
apin = adc.channel(pin='P20', attn=ADC.ATTN_11DB)

# get distance
dist = apin() * 5
</code></pre>
<p dir="auto">With that code, the readings are not accurate, so I was wondering if I need to scale the distance to take into account the attenuation? (sorry if it's a stupid question, this is new to me)</p>
<p dir="auto">Many thanks for your help/comments/suggestions!</p>
<p dir="auto">Cheers,</p>
<p dir="auto">Johan</p>
<p dir="auto">PS: the sensor has also other outputs such as a Pulse Width output and a serial output (RS232 format), but the former seems to be difficult to measure using a LoPy (correct me if I am wrong, maybe I missed something while searching in the forum) and I don't have a RS232 &lt;-&gt; TTL converter with me for the later(plus I'd like to avoid using additional boards).</p>
]]></description><link>https://forum.pycom.io/topic/1611/reading-analog-voltage-with-adc-and-attenuation</link><generator>RSS for Node</generator><lastBuildDate>Fri, 17 Apr 2026 16:12:56 GMT</lastBuildDate><atom:link href="https://forum.pycom.io/topic/1611.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 03 Aug 2017 03:20:58 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Reading analog voltage with ADC and attenuation on Thu, 03 Aug 2017 05:21:32 GMT]]></title><description><![CDATA[<p dir="auto">Hi all,</p>
<p dir="auto">I am playing with a rangefinder ultrasonic sensor (<a href="http://www.maxbotix.com/Ultrasonic_Sensors/MB7360.htm" target="_blank" rel="noopener noreferrer nofollow">MB7360 from Maxbotix</a>) and I have some question regarding the way to correctly read the sensor values.</p>
<p dir="auto">The sensor operates at voltage between 2.7V and 5.5V, so I hooked it up on the 3.3V pin of the expansion board.</p>
<p dir="auto">I am using the sensor's analog voltage output to read the distance measured by the sensor. In the spec-sheet it is mentioned that</p>
<ul>
<li>the output is referenced to the sensor ground and Vcc (which is 3.3V, right?)</li>
<li>the scale factor is Vcc / 5120 per 1 mm</li>
<li>using a 10 bits ADC, we just need to multiply the voltage read by 5 to obtain the distance</li>
</ul>
<p dir="auto">Since the maximum voltage output is 3.3V, I used an attenuation of 11 dB and I am using this piece of code to get the distance:</p>
<pre><code>from machine import ADC

# setting up the Analog/Digital Converter with 10 bits
adc = ADC(bits=10)

# create an analog pin on P20 for the ultrasonic sensor
apin = adc.channel(pin='P20', attn=ADC.ATTN_11DB)

# get distance
dist = apin() * 5
</code></pre>
<p dir="auto">With that code, the readings are not accurate, so I was wondering if I need to scale the distance to take into account the attenuation? (sorry if it's a stupid question, this is new to me)</p>
<p dir="auto">Many thanks for your help/comments/suggestions!</p>
<p dir="auto">Cheers,</p>
<p dir="auto">Johan</p>
<p dir="auto">PS: the sensor has also other outputs such as a Pulse Width output and a serial output (RS232 format), but the former seems to be difficult to measure using a LoPy (correct me if I am wrong, maybe I missed something while searching in the forum) and I don't have a RS232 &lt;-&gt; TTL converter with me for the later(plus I'd like to avoid using additional boards).</p>
]]></description><link>https://forum.pycom.io/post/10005</link><guid isPermaLink="true">https://forum.pycom.io/post/10005</guid><dc:creator><![CDATA[jojo]]></dc:creator><pubDate>Thu, 03 Aug 2017 05:21:32 GMT</pubDate></item><item><title><![CDATA[Reply to Reading analog voltage with ADC and attenuation on Thu, 03 Aug 2017 05:31:50 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/1388">@jojo</a> With an attn of 11db, the range of the ADC is 0-3.3V. You used 10 bits, so 1024 is equivalent to about 5 meters. I would assume then that the distance in meters you should get is:</p>
<pre><code>ADC_RANGE = const(1024)
sensor_range = 5.0
dist = apin * sensor_range/ ADC_RANGE
</code></pre>
<p dir="auto">If you use bits=12, then ADC_RANGE has to be set to 4096. You might need to adapt sensor_range for calibration. And be warned, the ADC of ESP is not very good. It is nonlinear and noisy. It might be advisable instead of a single value to take the average of several values.<br />
Anyhow, from that data sheet it looks as anyhow using the serial output is the most precise option.</p>
]]></description><link>https://forum.pycom.io/post/10011</link><guid isPermaLink="true">https://forum.pycom.io/post/10011</guid><dc:creator><![CDATA[robert-hh]]></dc:creator><pubDate>Thu, 03 Aug 2017 05:31:50 GMT</pubDate></item><item><title><![CDATA[Reply to Reading analog voltage with ADC and attenuation on Thu, 03 Aug 2017 07:03:34 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><br />
Thanks for the quick reply! :)</p>
<p dir="auto">I should have said that when I use:</p>
<pre><code>dist = apin() * 5
</code></pre>
<p dir="auto">it is already a shortcut for</p>
<pre><code>dist = apin() * 5120 / 1024
</code></pre>
<p dir="auto">where 5120 is the sensor range and 1024 is the range of the ADC using 10 bits. I was wondering if because there is an attenuation, I also have to scale the value of <code>apin()</code> by a factor <code>x</code>:</p>
<pre><code>dist = apin() * 5 * x
</code></pre>
<p dir="auto">Also, thanks for the suggestions. I should have mentioned that the piece of code is just a simplified exemple. In my code I also take multiple readings and then use the median (I'm not too found of the mean due to the huge variability). I agree that using a RS232 &lt;-&gt; TTL converter is probably the best option.</p>
]]></description><link>https://forum.pycom.io/post/10015</link><guid isPermaLink="true">https://forum.pycom.io/post/10015</guid><dc:creator><![CDATA[jojo]]></dc:creator><pubDate>Thu, 03 Aug 2017 07:03:34 GMT</pubDate></item><item><title><![CDATA[Reply to Reading analog voltage with ADC and attenuation on Thu, 03 Aug 2017 07:31:01 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/1388">@jojo</a> The internal range of the ADC is 0-1V. The attentiation of 11dB gives the range of 0-3.3V, which is what you need for our sensor, running at an Vcc of 3.3V. So you do not need a factor of x of range scaling, but you might need it for calibration.<br />
I never used the ADC in a range other than 12 bits. It might be that in lower than 12 bits mode you have to shift right the returned value by 1 or 2 bits. I have to try. So if the value you get is off by a factor of 4, that this may be the reason.</p>
]]></description><link>https://forum.pycom.io/post/10020</link><guid isPermaLink="true">https://forum.pycom.io/post/10020</guid><dc:creator><![CDATA[robert-hh]]></dc:creator><pubDate>Thu, 03 Aug 2017 07:31:01 GMT</pubDate></item><item><title><![CDATA[Reply to Reading analog voltage with ADC and attenuation on Thu, 03 Aug 2017 08:24:55 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><br />
Thanks for all the clarifications! I'll switch to an ADC of 12 bits and give it a try.</p>
]]></description><link>https://forum.pycom.io/post/10025</link><guid isPermaLink="true">https://forum.pycom.io/post/10025</guid><dc:creator><![CDATA[jojo]]></dc:creator><pubDate>Thu, 03 Aug 2017 08:24:55 GMT</pubDate></item><item><title><![CDATA[Reply to Reading analog voltage with ADC and attenuation on Fri, 29 Dec 2017 21:50:09 GMT]]></title><description><![CDATA[<p dir="auto">Hi...i am a new user here. As per my knowledge you should run this sensor at Vcc of 3.3V. So you do not need a factor of x of range scaling, but you might need it for calibration. You can try ADC in a range other than 12 bits. It might be that in lower than 12 bits mode you have to shift right the returned value by 1 or 2 bits.</p>
<p dir="auto"><a href="https://www.youtube.com/watch?v=-7AI18ugw1I" target="_blank" rel="noopener noreferrer nofollow">pcb assemblies</a></p>
]]></description><link>https://forum.pycom.io/post/12761</link><guid isPermaLink="true">https://forum.pycom.io/post/12761</guid><dc:creator><![CDATA[JardCrocker]]></dc:creator><pubDate>Fri, 29 Dec 2017 21:50:09 GMT</pubDate></item><item><title><![CDATA[Reply to Reading analog voltage with ADC and attenuation on Wed, 15 Nov 2017 16:13:37 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/1388">@jojo</a> said in <a href="/post/10005">Reading analog voltage with ADC and attenuation</a>:</p>
<blockquote>
<p dir="auto">serial output (RS232 format),</p>
</blockquote>
<p dir="auto">As far as I understand the data sheet, the voltage levels of the serial output are 0 and Vcc. If you run the sensor at Vcc=3.3v, then these ouput levels are ocmpatible with the xxPy devices. You can try to connect the output to the input of UART 1, 9600 baud. If you receive gibberish, then an inverter may be needed, which can be a single 2n7000 Transistor (S-&gt;GND, D-&gt;Rx input, G-&gt;Sensor-ouput). You might need a pullup-resitor at the Rx-input.<br />
To test, whether that works, connect it to a USB/UART bridge and open a serial terminal emulator.</p>
]]></description><link>https://forum.pycom.io/post/12762</link><guid isPermaLink="true">https://forum.pycom.io/post/12762</guid><dc:creator><![CDATA[robert-hh]]></dc:creator><pubDate>Wed, 15 Nov 2017 16:13:37 GMT</pubDate></item><item><title><![CDATA[Reply to Reading analog voltage with ADC and attenuation on Tue, 26 Jun 2018 08:40:14 GMT]]></title><description><![CDATA[<p dir="auto">the scale factor is Vcc / <a href="http://mm-to-inches.net" target="_blank" rel="noopener noreferrer nofollow">5120 per 1 mm</a></p>
]]></description><link>https://forum.pycom.io/post/20482</link><guid isPermaLink="true">https://forum.pycom.io/post/20482</guid><dc:creator><![CDATA[holim]]></dc:creator><pubDate>Tue, 26 Jun 2018 08:40:14 GMT</pubDate></item></channel></rss>