<?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[Connect units to PbHUB]]></title><description><![CDATA[<p dir="auto">I am trying to connect a <code>LIGHT</code> and an <code>EARTH</code> unit to the <code>PbHUB</code>. I connected the <code>PbHUB</code> to <code>PortA</code>, the <code>EARTH</code> sensor to <code>Hub 0</code> and the <code>LIGHT</code> sensor to <code>Hub 1</code>.</p>
<p dir="auto"><img src="/assets/uploads/files/1568885497208-img_3717-resized.jpg" alt="0_1568885495578_IMG_3717.JPG" class=" img-fluid img-markdown" /></p>
<p dir="auto">My code looks like this:</p>
<pre><code>pbhub = unit.get(unit.PBHUB, unit.PORTA)
earth = pbhub.analogRead(0x00)
light = pbhub.analogRead(0x01)
</code></pre>
<p dir="auto">I am able to read values from both sensors, however with strange behaviours. E.g. the lux value increases when it gets darker and vice versa. I guess the problem is, that I am lacking the logic of the <code>analogRead</code> function in the <code>LIGHT</code> and <code>EARTH</code> units:<br />
<a href="https://github.com/m5stack/UIFlow-Code/blob/a0dbef98fe29323d6b1837b479764fdab50c3836/units/_light.py" target="_blank" rel="noopener noreferrer nofollow ugc">https://github.com/m5stack/UIFlow-Code/blob/a0dbef98fe29323d6b1837b479764fdab50c3836/units/_light.py</a><br />
<a href="https://github.com/m5stack/UIFlow-Code/blob/a0dbef98fe29323d6b1837b479764fdab50c3836/units/_earth.py" target="_blank" rel="noopener noreferrer nofollow ugc">https://github.com/m5stack/UIFlow-Code/blob/a0dbef98fe29323d6b1837b479764fdab50c3836/units/_earth.py</a></p>
<p dir="auto">However I do not see how I would have to connect this unit correctly to the <code>PbHUB</code>. Do I have to copy paste the <code>analogRead</code> logic or is there another way to connect these units?<br />
<a class="plugin-mentions-user plugin-mentions-a" href="/user/ajb2k3" aria-label="Profile: ajb2k3">@<bdi>ajb2k3</bdi></a> Do you know how this should be handled?</p>
]]></description><link>https://community.m5stack.com/topic/1330/connect-units-to-pbhub</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Apr 2026 04:47:55 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/1330.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 19 Sep 2019 08:55:36 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Connect units to PbHUB on Fri, 04 Oct 2019 21:04:16 GMT]]></title><description><![CDATA[<p dir="auto">I ended up copy pasting the method from the <code>Light</code> unit with some slight adjustments:</p>
<pre><code>def pbhubAnalogRead(pbhub, pbhub_address):
        data = 0
        max_val = 30
        min_val = 750
        for i in range(0, 10):
            newdata = 750 - pbhub.analogRead(pbhub_address)
            data += newdata
            if newdata &gt; max_val:
                max_val = newdata
            if newdata &lt; min_val:
                min_val = newdata
        data -= (max_val + min_val)
        data &gt;&gt;= 3
        return round(max(1024 * data / 750, 0), 2)

pbhub = unit.get(unit.PBHUB, unit.PORTA)
light = pbhubAnalogRead(pbhub, 0x01)
</code></pre>
<p dir="auto">The magic number <code>min_val</code> I got by pressing my finger on the light sensor (blocking all the light from entering) and reading the analog value. The value <code>750</code> seems to be the minimum value the light sensor would emit. The number <code>max_val</code> I got by pointing a strong light at the sensor, it seems to be the maximum value the sensor emits. The min max values are mixed up because it measures the resistance and decreasing resistance means increasing light intensity. Using this method I get values that are somewhat similar to what I get by connecting the <code>Light</code> unit to <code>PortB</code> directly.</p>
<p dir="auto">If anyone knows how this should be done properly, please let me know!</p>
]]></description><link>https://community.m5stack.com/post/5787</link><guid isPermaLink="true">https://community.m5stack.com/post/5787</guid><dc:creator><![CDATA[maechler]]></dc:creator><pubDate>Fri, 04 Oct 2019 21:04:16 GMT</pubDate></item><item><title><![CDATA[Reply to Connect units to PbHUB on Thu, 26 Sep 2019 08:51:55 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ws1088" aria-label="Profile: ws1088">@<bdi>ws1088</bdi></a> Thanks for the clarification!<br />
Do you say that the value I get with <code>pbhub.analogRead(0x00)</code> should be plugged in for <code>newdata </code> in <code>Light.analogValue</code>?<br />
I would have placed it where the Light unit calls <code>self.adc.readraw()</code>.<br />
Do I also have to call  <code>pbhub.analogRead(0x00)</code> 10 times in a loop, as the Light unit does?</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ajb2k3" aria-label="Profile: ajb2k3">@<bdi>ajb2k3</bdi></a> Yes, kind of. I have tested what the Light unit does for increasing values of <code>self.adc.readraw()</code> it will output decreasing values. This inversion is missing when reading directly from <code>PbHUB </code>.</p>
<pre><code>class Light:
    @staticmethod
    def analogValue(value):
        data = 0
        max = 0
        min = 4096
        for i in range(0, 10):
            newdata = 4095 - value
            data += newdata
            if newdata &gt; max:
                max = newdata
            if newdata &lt; min:
                min = newdata
        data -= (max + min)
        data &gt;&gt;= 3

        return round(1024 * data / 4095, 2)


print(400, Light.analogValue(400))  # 400 923.98
print(500, Light.analogValue(500))  # 500 898.97
print(600, Light.analogValue(600))  # 600 873.96
print(700, Light.analogValue(700))  # 700 848.96
print(800, Light.analogValue(800))  # 800 823.95
print(900, Light.analogValue(900))  # 900 798.95
</code></pre>
]]></description><link>https://community.m5stack.com/post/5738</link><guid isPermaLink="true">https://community.m5stack.com/post/5738</guid><dc:creator><![CDATA[maechler]]></dc:creator><pubDate>Thu, 26 Sep 2019 08:51:55 GMT</pubDate></item><item><title><![CDATA[Reply to Connect units to PbHUB on Thu, 26 Sep 2019 07:29:00 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/maechler" aria-label="Profile: maechler">@<bdi>maechler</bdi></a> are you saying the values are inverted?</p>
]]></description><link>https://community.m5stack.com/post/5736</link><guid isPermaLink="true">https://community.m5stack.com/post/5736</guid><dc:creator><![CDATA[ajb2k3]]></dc:creator><pubDate>Thu, 26 Sep 2019 07:29:00 GMT</pubDate></item><item><title><![CDATA[Reply to Connect units to PbHUB on Wed, 25 Sep 2019 20:17:34 GMT]]></title><description><![CDATA[<p dir="auto">I think the Python code in these units was not implemented with PbHUB in mind at all. I agree with your suggested interface will be very useful. You can probably do this by deriving a <code>HubAwareLight</code> class from <code>Light</code>. The job of <code>HubAwareLight</code> is to just set <code>newdata</code> value in <code>Light</code></p>
]]></description><link>https://community.m5stack.com/post/5734</link><guid isPermaLink="true">https://community.m5stack.com/post/5734</guid><dc:creator><![CDATA[ws1088]]></dc:creator><pubDate>Wed, 25 Sep 2019 20:17:34 GMT</pubDate></item><item><title><![CDATA[Reply to Connect units to PbHUB on Tue, 24 Sep 2019 20:59:45 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/lukasmaximus" aria-label="Profile: lukasmaximus">@<bdi>lukasmaximus</bdi></a>  <a class="plugin-mentions-user plugin-mentions-a" href="/user/ajb2k3" aria-label="Profile: ajb2k3">@<bdi>ajb2k3</bdi></a>  Thanks for your support! Although the video does not really answer my question.</p>
<p dir="auto">I am able to read values from the PbHUB like this:</p>
<pre><code>pbhub = unit.get(unit.PBHUB, unit.PORTA)
earth = pbhub.analogRead(0x00)
light = pbhub.analogRead(0x01)
</code></pre>
<p dir="auto">The problem is, that these values seem to be wrong. The lux value increases when it gets darker, similarly the earth unit says the earth gets dryer when it actually gets wetter. I think the problem is, that reading directly from <code>PbHUB</code>, the logic from the <code>LIGHT.analogValue</code> function is missing:</p>
<pre><code>class Light:
    @property
    def analogValue(self):
        data = 0
        max = 0
        min = 4096
        for i in range(0, 10):
            newdata = 4095 - self.adc.readraw()
            data += newdata
            if newdata &gt; max:
                max = newdata
            if newdata &lt; min:
                min = newdata
        data -= (max + min)
        data &gt;&gt;= 3
        return round(1024 * data / 4095, 2)
</code></pre>
<p dir="auto">This function seems to transform the data in a way that is missing when reading the value directly from <code>PbHUB</code>. My question is whether I do have to copy paste this logic or whether there is some way to reuse the <code>LIGHT</code> unit with <code>PbHUB</code>?</p>
<p dir="auto">I would  like to be able to do something like this:</p>
<pre><code>pbhub = unit.get(unit.PBHUB, unit.PORTA)
earth = unit.get(unit.EARTH, pbhub.HUB0) # Not M5Stack API yet
light = unit.get(unit.LIGHT, pbhub.HUB1) # Not M5Stack API yet

# This should read the value correctly, using the `LIGHT.analogValue` logic
print(earth.analogValue())
print(light.analogValue()) 
</code></pre>
]]></description><link>https://community.m5stack.com/post/5725</link><guid isPermaLink="true">https://community.m5stack.com/post/5725</guid><dc:creator><![CDATA[maechler]]></dc:creator><pubDate>Tue, 24 Sep 2019 20:59:45 GMT</pubDate></item><item><title><![CDATA[Reply to Connect units to PbHUB on Tue, 24 Sep 2019 18:58:53 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/maechler" aria-label="Profile: maechler">@<bdi>maechler</bdi></a> You caught me here at a loss. Check out <a class="plugin-mentions-user plugin-mentions-a" href="/user/lukasmaximus" aria-label="Profile: lukasmaximus">@<bdi>lukasmaximus</bdi></a> video on youtube.</p>
]]></description><link>https://community.m5stack.com/post/5724</link><guid isPermaLink="true">https://community.m5stack.com/post/5724</guid><dc:creator><![CDATA[ajb2k3]]></dc:creator><pubDate>Tue, 24 Sep 2019 18:58:53 GMT</pubDate></item><item><title><![CDATA[Reply to Connect units to PbHUB on Tue, 24 Sep 2019 18:39:53 GMT]]></title><description><![CDATA[<p dir="auto">Hey I made a video on the PBHUB <a href="https://www.youtube.com/watch?v=NG9SBKfxX5w" target="_blank" rel="noopener noreferrer nofollow ugc">https://www.youtube.com/watch?v=NG9SBKfxX5w</a> maybe it can help you. Often when I want to write some micropython code I make it in Uiflow first. Since the micropython documentation is lacking this is a good way to find out what might be missing from your code and what the micropython commands you need are.</p>
]]></description><link>https://community.m5stack.com/post/5723</link><guid isPermaLink="true">https://community.m5stack.com/post/5723</guid><dc:creator><![CDATA[lukasmaximus]]></dc:creator><pubDate>Tue, 24 Sep 2019 18:39:53 GMT</pubDate></item></channel></rss>