Stepper Motor with Loadcell

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
berke
Posts: 17
Joined: Wed Sep 01, 2021 7:45 am

Stepper Motor with Loadcell

Post by berke » Thu Nov 11, 2021 1:31 pm

Hello everyone, I am trying to use stepper motor with load cell. I want to run stepper motor until loadcell>= 200 gr . I am trying to write but when I write like this:

Code: Select all

def calcWeight(import,isbigger=True):
    isWeightOk = True
    while isWeightOk:
        enaPin.low()
        pulPin.low()
        time.sleep_us(1)
        pulPin.high()
        time.sleep_us(1)
        val = (scales.stable_value()*(0.00589)/10000
        print(val) 
        if(isbigger):
            if(val>=(import*0.001)):
                isWeightOk=False
        else:
            if(val<=(import*0.001)):
                isWeightOk=False
    return True
This code is working but motor is too slow. Normally Motor should wait with time.sleep_us(500) for my project. When I write time.sleep_us(1) it is really really slow, I can't work like this.I think the problem is loadcell. Without loadcell motor is working good and I can arrange speed easily. What can I do? I am using this hx711 library : https://github.com/SergeyPiskunov/micro ... r/hx711.py

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Stepper Motor with Loadcell

Post by scruss » Thu Nov 11, 2021 3:20 pm

berke wrote:
Thu Nov 11, 2021 1:31 pm
... When I write time.sleep_us(1) it is really really slow, I can't work like this.I think the problem is loadcell.
Are you sure that you want to be using sleep_us(), which uses microseconds? Real physical sensors take a while to settle down, and it takes some time for an HX711 to read and send its numbers. A typical 200 steps/rev motor at 50 rpm is 6000 us / step.

Maybe use sleep_ms()?

berke
Posts: 17
Joined: Wed Sep 01, 2021 7:45 am

Re: Stepper Motor with Loadcell

Post by berke » Thu Nov 11, 2021 5:59 pm

When I add
oadcell>= 200 gr . I am trying to write but when I write like this:
CODE: SELECT ALL

def calcWeight(import,isbigger=True):
isWeightOk = True
while isWeightOk:

Code: Select all

 for i in range(250): 
                 enaPin.low()
                 pulPin.low()
                 time.sleep_us(1)
                 pulPin.high()
                 time.sleep_us(1)
        val = (scales.stable_value()*(0.00589)/10000
        print(val) 
        if(isbigger):
            if(val>=(import*0.001)):
                isWeightOk=False
        else:
            if(val<=(import*0.001)):
                isWeightOk=False
    return True

I added for i in range(250) , İt is working like this. When I increase the 250 , hx711 is going slow, When I decrease the 250 , hx711 going fast but this is not good
Stepper motor is going 250 then stop, I am taking one line hx711 value and motor is again start. Motor is not working good like this.
Last edited by berke on Thu Nov 11, 2021 6:02 pm, edited 2 times in total.

berke
Posts: 17
Joined: Wed Sep 01, 2021 7:45 am

Re: Stepper Motor with Loadcell

Post by berke » Thu Nov 11, 2021 6:00 pm

scruss wrote:
Thu Nov 11, 2021 3:20 pm
berke wrote:
Thu Nov 11, 2021 1:31 pm
... When I write time.sleep_us(1) it is really really slow, I can't work like this.I think the problem is loadcell.
Are you sure that you want to be using sleep_us(), which uses microseconds? Real physical sensors take a while to settle down, and it takes some time for an HX711 to read and send its numbers. A typical 200 steps/rev motor at 50 rpm is 6000 us / step.

Maybe use sleep_ms()?
I will try it, thank you.

berke
Posts: 17
Joined: Wed Sep 01, 2021 7:45 am

Re: Stepper Motor with Loadcell

Post by berke » Fri Nov 12, 2021 5:43 am

scruss wrote:
Thu Nov 11, 2021 3:20 pm
berke wrote:
Thu Nov 11, 2021 1:31 pm
... When I write time.sleep_us(1) it is really really slow, I can't work like this.I think the problem is loadcell.
Are you sure that you want to be using sleep_us(), which uses microseconds? Real physical sensors take a while to settle down, and it takes some time for an HX711 to read and send its numbers. A typical 200 steps/rev motor at 50 rpm is 6000 us / step.

Maybe use sleep_ms()?
It didn't worked.

berke
Posts: 17
Joined: Wed Sep 01, 2021 7:45 am

Re: Stepper Motor with Loadcell

Post by berke » Fri Nov 12, 2021 10:52 am

I tried to use 2 core for better solution but I couldn't do that with this code hx711 is working but stepper is not working. Is there any Idea for me? I am not expert I started 2-3 months ago.

Code: Select all

import utime
import _thread
from machine import Pin
from hx711 import HX711
from utime import sleep_us
import time
from scales import Scales
if __name__ == "__main__":
    scales = Scales(d_out=22, pd_sck=21)
    scales.tare()
enaPin = Pin(16, Pin.OUT)
dirPin = Pin(17, Pin.OUT)
pulPin = Pin(18, Pin.OUT)
baton = _thread.allocate_lock()
def second_thread():
    while True:
        baton.acquire()
        pulPin.low()
        time.sleep_ms(500)
        pulPin.high()
        time.sleep_ms(500)
        baton.release()
_thread.start_new_thread(second_thread, ())
while True:
    baton.acquire()
    time.sleep(1)
    val = (scales.stable_value()*(0.00589)/10000
    print(val)
    baton.release()

berke
Posts: 17
Joined: Wed Sep 01, 2021 7:45 am

Re: Stepper Motor with Loadcell

Post by berke » Fri Nov 12, 2021 11:41 am

I used https://github.com/robert-hh/hx711/blob ... 711_pio.py this library, now I can use two cores. Stepper and loadcell working together without problem. Now I need to comunicate 2 cores :D . Thank you everyone.

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Stepper Motor with Loadcell

Post by scruss » Sat Nov 13, 2021 9:15 pm

What do you need two cores for? Even the oldest 8-bit microcontrollers were fast enough to manage motor drive and metering
berke wrote:
Fri Nov 12, 2021 5:43 am
It didn't worked.
What errors did you get? What did you expect and what did you get instead?

berke
Posts: 17
Joined: Wed Sep 01, 2021 7:45 am

Re: Stepper Motor with Loadcell

Post by berke » Sun Nov 14, 2021 6:47 pm

scruss wrote:
Sat Nov 13, 2021 9:15 pm
What do you need two cores for? Even the oldest 8-bit microcontrollers were fast enough to manage motor drive and metering
berke wrote:
Fri Nov 12, 2021 5:43 am
It didn't worked.
What errors did you get? What did you expect and what did you get instead?
I am not having error. I want to run stepper for one direction until hx711 measure right value. Then stepper has to stop and stepper need to wait for another task. When I run stepper, I can't measure weight. I couldn't pass this line.This line needs to work until hx711 measure 0.200 gr.

Code: Select all

 enaPin.low()
        pulPin.low()
        time.sleep_ms(1)
        pulPin.high()
        time.sleep_ms(1)
Here is full code:

Code: Select all

def calcWeight(import,isbigger=True):
    isWeightOk = True
    while isWeightOk:
        enaPin.low()
        pulPin.low()
        time.sleep_ms(1)
        pulPin.high()
        time.sleep_ms(1)
        val = (scales.stable_value()*(0.00589)/10000
        print(val) 
        if(isbigger):
            if(val>=(import*0.001)):
                isWeightOk=False
        else:
            if(val<=(import*0.001)):
                isWeightOk=False
    return True
When I use sleep_ms like you said, stepper is working too slow again. Do you know why? Thank you.

Post Reply