GPIO PWM for a step motor

Hello,

I’m using a raspberry 3 with openhabian for automation.
I want to control a step motor for which I need a pulsed signal.
I think this should be possible with gpio and exec binding.
But I’m complete new in this binding.

I just want to have an item, like a dimmer, to change the speed of motor.
Can anybody help me to create a script to execute a pwm on a gpio port?

Thanks a lot
Chris

I don’t think that the GPIO pins carry nearly enough power to drive a stepper motor. You will want a dedicated motor controller with its own power supply to drive the motor.
Adafruit is one good supplier but there are many others. They also have good tutorials on driving the steppers.

Thanks for your reply, I’ve got a
HY-DIV268N-5A two phase hybrid stepper motor driver.
So I just need a pulsed signal created by openhab.

openHAB is not a real time system, it is not capable of this. It can of course issue speed and control commands to “something else” - hardware, or a script.
I think you’ll need to find some independent script or utility to run on your pi that can accept control parameters.

That’s exactly what I want to have, a script running on my pi which manages the pwm on gpio.
Openhab shall only give a trigger to start or to stop, including a parameter to change the pwm signal.
I thought exec should be the right way, but don’t know how.

Exec will indeed call the script or program that you use to control the motor. See https://www.openhab.org/addons/bindings/exec/ for the docs on that binding.

But to actually write the script you will have to look elsewhere. We are home automation enthusiasts. We don’t have that large of a DIY electronics population here who can help you with such a script. It’s also outside the scope of OH.

MQTT could send the commands to a script running on the Pi. I just built a barometer using Adafruit stepper motors and driver for the dials. Its not controlled by MQTT but sends pressure data to openhab via MQTT. It could be done in reverse… aka publish . You would need the Openhab MQTT binding, Python Paho for a python script and a mqtt broker . The broker could be on the same Pi as your openhab install. Thats how I have it and use another Pi to connect to the driver and pressure sensor. Installing the MQTT broker (mosquitto) is easlliy done from the pi-config menu, or if using v2 MQTT binding there is an embedded one available.

Worth a look. I am not an expert!!! The barometer was my first ever foray into python, mqtt and electronics type stuff. It worked :slight_smile:

Hi,

@m4rk, thanks a lot for your note to try it using mqtt.
I’ve got the first step working, I can transmit a switch command via mqtt to change a gpio pin.
I can also transmit a number to my python script.

My actual problem is, that I don’t know how to convert it using it anyway in the script.

I want to use 'global_pwm ', which i’m changing in openHab to be used anyway.

My code:

import paho.mqtt.client as mqtt
import RPi.GPIO as GPIO
import time
from time import sleep

MQTT_HOST = "192.168.4.1"
MQTT_PORT = 1883
MQTT_KEEPALIVE_INTERVAL = 45
MQTT_TOPIC = "raspi3/pin18PWM"
MQTT_TOPIC1 = "raspi3/pin6"
MQTT_TOPIC2 = "raspi3/pin23"

LED1 = 6
LED2 = 23
servoPIN = 18

GPIO.setmode(GPIO.BCM)

GPIO.setup(servoPIN, GPIO.OUT)
GPIO.setup(LED1, GPIO.OUT)
GPIO.setup(LED2, GPIO.OUT)

global_pwm = 2

p = GPIO.PWM(servoPIN, 50) # GPIO 18 als PWM mit 50Hz, 20ms
p.start(50) #

try:
  def on_connect(self,mosq, obj, rc):
     mqttc.subscribe(MQTT_TOPIC, 0)
     mqttc.subscribe(MQTT_TOPIC1, 0)
     print("Connect on "+MQTT_HOST)
  
  def on_message(mosq, obj, msg):
    if (msg.topic == 'raspi3/pin18PWM'):
      global_pwm = float(msg.payload)
      print "Topic: " + str(msg.topic)
      print "message: " + str(msg.payload)
  def on_subscribe(mosq, obj, mid, granted_qos):
          print("Subscribed to Topic: " + 
          MQTT_TOPIC + " with QoS: " + str(granted_qos))
    # Initiate MQTT Client
  mqttc = mqtt.Client()

    # Assign event callbacks
  mqttc.on_message = on_message
  mqttc.on_connect = on_connect
  mqttc.on_subscribe = on_subscribe

    # Connect with MQTT Broker
  mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)

    # Continue monitoring the incoming messages for subscribed topic
  #mqttc.loop_forever(): 
  while True:
    mqttc.loop(0.01)
    GPIO.output(LED1,True)
    sleep(global_pwm) 
    mqttc.loop(0.01)
    GPIO.output(LED1,False)
    sleep(global_pwm)
  
except KeyboardInterrupt:  
  # here you put any code you want to run before the program   
  # exits when you press CTRL+C
  p.ChangeDutyCycle(2.5)
  p.stop()
  GPIO.cleanup()  

Can anybody help me with this.

Thanks a lot
Chris

Christian - did you get anywhere with this?

I want to accomplish something similar. I am currently building up a project whereby I am using excess solar energy for battery charging and once the battery is full water heating. I have a proportional solid state relay that will take a 0-5V DC signal. Ideally I can use one of the GPIO pins and an RC filter to control the hot water load.

Any help would be appreciated before I start from scratch.

Cheers, Shane

Seems a bit over the top for water heating. The time constants are so long you could do PWM directly from openHAB with just on/off commands as portions of a minute long timeslice, But generally a simple thermostatic on/off would do for water.

The time constants are long when you are referring to the rate at which water changes temperature but really short when you talking the power in a house which fluctuates fairly rapidly and frequently. There are several off the shelf products that perform this function (limiting solar exports by proportionally controlling a hot water heating element) and some of them adjust their power output at sub 1 second intervals.

example - http://www.paladin.nz/ or https://www.marlec.co.uk/product/solar-iboost/

I just prefer to do this all from one system as opposed to having several discrete systems. I definitely don’t need a sub 1 second refresh rate but would prefer a sub 10 second refresh rate. I can poll the house power at a 1 second rate which I am currently doing. All I need now is a PID loop and an analog output I can adjust from Openhab ideally hard wired directly from the PI via the PWM output.