Skip to content

Instantly share code, notes, and snippets.

@ypelletier
Last active August 7, 2021 11:43
Show Gist options
  • Save ypelletier/67ea06d002fd7d0bc042 to your computer and use it in GitHub Desktop.
Save ypelletier/67ea06d002fd7d0bc042 to your computer and use it in GitHub Desktop.
Contrôle d'un moteur DC par le Raspberry Pi
#!/usr/bin/python
# -*- coding:utf-8 -*-
'''
Controle d'un moteur à courant continu par le Raspberry Pi
Plus de details:
http://electroniqueamateur.blogspot.com/2014/09/controler-un-moteur-dc-en-python-avec.html
'''
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BOARD) ##je prefere la numerotation BOARD plutot que BCM
Moteur1A = 16 ## premiere entrée du premier moteur, pin 16
Moteur1B = 18 ## deuxieme entrée du premier moteur, pin 18
Moteur1E = 22 ## enable du premier moteur, pin 22
GPIO.setup(Moteur1A,GPIO.OUT) ## ces trois broches du Raspberry Pi sont des sorties
GPIO.setup(Moteur1B,GPIO.OUT)
GPIO.setup(Moteur1E,GPIO.OUT)
pwm = GPIO.PWM(Moteur1E,50) ## pwm de la broche 22 a une frequence de 50 Hz
pwm.start(100) ## on commemnce avec un rapport cyclique de 100%
print ("Rotation sens direct, vitesse maximale (rapport cyclique 100%)")
GPIO.output(Moteur1A,GPIO.HIGH)
GPIO.output(Moteur1B,GPIO.LOW)
GPIO.output(Moteur1E,GPIO.HIGH)
sleep(5) ## on laisse tourner le moteur 5 secondes avec des parametres
pwm.ChangeDutyCycle(40) ## modification du rapport cyclique a 40%
print ("Rotation sens direct, au ralenti (rapport cyclique 40%)")
sleep(5)
print ("Rotation sens inverse, au ralenti (rapport cyclique 40%)")
GPIO.output(Moteur1A,GPIO.LOW)
GPIO.output(Moteur1B,GPIO.HIGH)
sleep(5)
pwm.ChangeDutyCycle(100)
print ("Rotation sens inverse, vitesse maximale (rapport cyclique 100%)")
sleep(5)
print ("Arret du moteur")
GPIO.output(Moteur1E,GPIO.LOW)
pwm.stop() ## interruption du pwm
GPIO.cleanup()
Copy link

ghost commented May 4, 2020

print "Rotation sens direct, vitesse maximale (rapport cyclique 100%)"

need to be :

print ("Rotation sens direct, vitesse maximale (rapport cyclique 100%)")

@fned-ship
Copy link

hi. i use esp32 istead of Raspberry . the question is may i use this coding . and i noticed that there GPIO in esp32 but there ain't RPI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment