B4J Question HX711 - Raspberry - B4J

micro

Well-Known Member
Licensed User
Longtime User
Hi to all
someone has interfaced the HX711 module with the raspberry and B4J?
if possible could you share the code?
Thanks
 

thetahsk

Active Member
Licensed User
Longtime User
A very simple and clever method is to use the Sparkfun OpenScale device. It uses serial communication and you can use the jserial lib.

https://www.b4x.com/android/forum/threads/jserial-library.34762/

The SparkFun OpenScale is a simple-to-use, open source solution for measuring weight and temperature.
It has the ability to read multiple types of load cells and offers a simple-to-use serial menu to configure
calibration value, sample rate, time stamp and units of precision.

On board the SparkFun OpenScale is the ATmega328P microcontroller, for addressing your communications
needs and transferring your data to a serial terminal
or to a data logger such as the OpenLog, an FT231 with mini USB,
for USB to serial connection; the HX711, a 24-bit ADC for weigh scales; and the TMP102, for recording the ambient temperature of your system


https://www.sparkfun.com/products/13261

https://github.com/sparkfun/OpenScale
 
Last edited:
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Thanks thetahsk and derez for your answer
I have already developed my custom board with hx711 and arduino mini, connected serially with the rapberry.
I meant the only HX711 module connected directly to the rapsberry gpio and managed with B4J
Good day everyone
 
Upvote 0

thetahsk

Active Member
Licensed User
Longtime User
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
I'm running same test
this is the sub used for arduino for read a value fron HX711
B4X:
long getValue()
{
 
 byte data[3];

    while (digitalRead(DOUT))
        ;

    for (byte j = 3; j--;)
    {
        for (char i = 8; i--;)
        {
            digitalWrite(SCK, HIGH);
            bitWrite(data[j], i, digitalRead(DOUT));
            digitalWrite(SCK, LOW);
        }
    }

    digitalWrite(SCK, HIGH);
    digitalWrite(SCK, LOW);

    data[2] ^= 0x80;

    return ((uint32_t) data[2] << 16) | ((uint32_t) data[1] << 8)
            | (uint32_t) data[0] << 1;
}

this is the code I tried to run
with B4j and raspberry GPIO but surely is wrong because I don't get the desired results
and perhaps the timing (clock pulse) are not correct.

B4X:
Sub Capture
    datoconv.Initialize
    Dim i, y As Int
    Dim b(3) As Byte
    For y = 2 To 0 Step -1
        For i = 1 To 8
            Ck.State = True
                If Dout.State = False Then
                    datoconv.Append("0")
                Else
                    datoconv.Append("1")
                End If
            Ck.State = False
        Next
        b(y) = Bit.ParseInt(datoconv, 2)
        datoconv.Initialize
    Next
    Ck.State = True
    Ck.State = False
    b(2) = Bit.Xor(b(2), 0x80)
    Dim result As Int = Bit.Or(Bit.ShiftLeft(b(2),16), Bit.ShiftLeft(b(1), 8))
    result = Bit.Or(result, Bit.ShiftLeft(b(0), 1))
    CallSubDelayed2(mTarget, "VisDato", result)
    Log(b(2) & " " & b(1) & " " & b(0))
    Sleep(10)
End Sub

dataconv is a Stringbuilder
Ck is a output GPIO pin used as clock
Dout is a input GPIO pin for read a data.

I also used this

B4X:
datoconv.Initialize
    Dim i As Int
    For i = 1 To 24
            Ck.State = True
             If Dout.State = False Then
                    datoconv.Append("0")
             Else
                    datoconv.Append("1")
             End If
            Ck.State = False
     Next
    Ck.State = True
    Ck.State = False
    CallSubDelayed2(mTarget, "VisDato", Bit.Parseint(datoconv.ToString, 2))
    Sleep(10)
maybe a little better but even here the data is incorrect.
We can perfect the reading routine?

Thanks
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Hi
Then
this code is correct
B4X:
Sub Capture As Int
    Dim count, i As Int
    For i = 1 To 24
        Ck.State = True
        count = Bit.ShiftLeft(count, 1)
        Ck.State = False
        If Dout.State Then count = count + 1
    Next
    count = Bit.Xor(count, 0x800000)
    Ck.State = True
    Ck.State = False
        Ck.State = True
    Ck.State = False
    Return count
End Sub
or
B4X:
Sub Capture As Int
    Dim i As Int
    datoconv.Initialize
    For i = 1 To 24
        Ck.State = True
        If Dout.State = False Then
            datoconv.Append(0)
        Else
            datoconv.Append(1)
        End If
        Ck.State = False
    Next
    Dim result As Int = Bit.ParseInt(datoconv, 2)
    result = Bit.Xor(result, 0x800000)
    Ck.State = True
    Ck.State = False
    Ck.State = True
    Ck.State = False
    Return result
End Sub
but i think that the direct use of B4J is not appropriate,
execution times are too high and this instruction (Ck.State = ...) on a GPIO pin to carry a pin from
high to low lasts more than 60 us and the HX711 sometimes goes into powerdown status and some bit
is badly read
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Why not use an Arduino for this? You can develop with B4R
Hi Erel
if you read the post #4
I say that I have already designed my board with an arduino mini
I have already developed my custom board with hx711 and arduino mini, connected serially with the rapberry.

I wanted to reduce the hardware
Thanks
Best regards
 
Upvote 0
Top