Running two simple DC motors (5 Amp) Forward and Backward in Jetson Nano

I am trying to run two DC motors forward and backward. I used it on Raspberry Pi and it worked. I got to know that the pin configuration of Jetson Nano and Raspberry Pi is same. So, I thought that the Python code which worked on Raspberry Pi would also work for Jetson Nano. Here is the code I am trying to run for simple two DC motors.

import RPi.GPIO as GPIO			# using Rpi.GPIO module
from time import sleep			# import function sleep for delay
GPIO.setmode(GPIO.BCM)			# GPIO numbering
GPIO.setwarnings(False)			# enable warning from GPIO
AN2 = 13				# set pwm2 pin on MD10-Hat
AN1 = 12				# set pwm1 pin on MD10-hat
DIG2 = 24				# set dir2 pin on MD10-Hat
DIG1 = 26				# set dir1 pin on MD10-Hat
GPIO.setup(AN2, GPIO.OUT)		# set pin as output
GPIO.setup(AN1, GPIO.OUT)		# set pin as output
GPIO.setup(DIG2, GPIO.OUT)		# set pin as output
GPIO.setup(DIG1, GPIO.OUT)		# set pin as output
sleep(1)				# delay for 1 seconds
p1 = GPIO.PWM(AN1, 100)			# set pwm for M1
p2 = GPIO.PWM(AN2, 100)			# set pwm for M2

try:					
  while True:

   print "Left"				# display "Forward" when programe run
   GPIO.output(DIG1, GPIO.HIGH)		# set DIG1 as HIGH, M1B will turn ON
   GPIO.output(DIG2, GPIO.LOW)		# set DIG2 as HIGH, M2B will turn ON
   p1.start(100)			# set speed for M1 at 100%
   p2.start(100)			# set speed for M2 at 100%
   sleep(2)				#delay for 2 second
   print "Forward"
   GPIO.output(DIG1, GPIO.LOW)          # set DIG1 as LOW, to control direction
   GPIO.output(DIG2, GPIO.LOW)          # set DIG2 as LOW, to control direction
   p1.start(100)                        # set speed for M1 at 100%
   p2.start(100)                        # set speed for M2 at 100%
   sleep(2)                             #delay for 2 second

   print "Backward"
   GPIO.output(DIG1, GPIO.HIGH)         # set DIG1 as HIGH, to control direction
   GPIO.output(DIG2, GPIO.HIGH)         # set DIG2 as HIGH, to control direction
   p1.start(100)                        # set speed for M1 at 100%
   p2.start(100)                        # set speed for M2 at 100%
   sleep(2)                             #delay for 2 second

   print "Right"
   GPIO.output(DIG1, GPIO.LOW)       
   GPIO.output(DIG2, GPIO.HIGH)    
   p1.start(100)                     
   p2.start(100)                   
   sleep(2)                        

   print "STOP"
   GPIO.output(DIG1, GPIO.LOW)          # Direction can ignore
   GPIO.output(DIG2, GPIO.LOW)          # Direction can ignore
   p1.start(0)                          # set speed for M1 at 0%
   p2.start(0)                          # set speed for M2 at 0%
   sleep(3)                             #delay for 3 second


except:					# exit programe when keyboard interupt
   p1.start(0)				# set speed to 0
   p2.start(0)				# set speed to 0
					# Control+x to save file and exit

But it did not work. I need help to run this Python code on Jetson Nano. The main difficulty is that Jetson Nano is not letting me open these pins as PWM.

I googled about it found some references here. I tried to find the pins but it turned out to be that those pins need to be enabled by some dts method as described here. Later, I found some detailed article about how to enable PWM pins for jetson nano here. Later, I found a discussion in which a method was described to open PWM using jetson-io.py tool. I have also gone through an other discussion in which it was described that Jetson Nano has only two pins for PWM. I am not sure about it.

I am facing difficulty in using Jetson Nano like Raspberry Pi. I am coming from Arduino and Raspberry Pi background. I use different sensors and motors very easily in Arduino and Raspberry Pi. However, as being new in this amazing Jetson Nano world, I am learning.

In short, I need help to run above code for to move two simple DC motors forward and backward. Is it possible in Jetson Nano?

Any comment or suggestion is appreciated. Thank you so much.

please check pinmux for pin available settings,
there’re Pin-206/GPIO07, Pin-228/GPIO13, and Pin-230/GPIO14 available for PWM configurations.

please refer to the pinmux spreadsheets for the default pin configurations,
please also check Configuring the 40-Pin Expansion Header with Jetson-IO tool to simplify the configuration of the I/Os exposed by the 40‑pin expansion header.
thanks

Thank you very much for your help. I opened Pin 32 and 33 of jetson nano as PWM0 and PWM3 and changed the board in the above code from BCM to BOARD and put the name of pins. The working code is as follows:

import RPi.GPIO as GPIO			# using Rpi.GPIO module
from time import sleep			# import function sleep for delay
#GPIO.setmode(GPIO.BCM)			# GPIO numbering
GPIO.setwarnings(False)			# enable warning from GPIO
GPIO.setmode(GPIO.BOARD)
AN2 = 33				# set pwm2 pin on MD10-Hat
AN1 = 32				# set pwm1 pin on MD10-hat
DIG2 = 18				# set dir2 pin on MD10-Hat
DIG1 = 37				# set dir1 pin on MD10-Hat
GPIO.setup(AN2, GPIO.OUT)		# set pin as output
GPIO.setup(AN1, GPIO.OUT)		# set pin as output
GPIO.setup(DIG2, GPIO.OUT)		# set pin as output
GPIO.setup(DIG1, GPIO.OUT)		# set pin as output
sleep(1)				# delay for 1 seconds
p1 = GPIO.PWM(AN1, 100)			# set pwm for M1
p2 = GPIO.PWM(AN2, 100)			# set pwm for M2

try:					
  while True:

   print ("Left")				# display "Forward" when programe run
   GPIO.output(DIG1, GPIO.HIGH)		# set DIG1 as HIGH, M1B will turn ON
   GPIO.output(DIG2, GPIO.LOW)		# set DIG2 as HIGH, M2B will turn ON
   p1.start(100)			# set speed for M1 at 100%
   p2.start(100)			# set speed for M2 at 100%
   sleep(2)				#delay for 2 second
   print ("Forward")
   GPIO.output(DIG1, GPIO.LOW)          # set DIG1 as LOW, to control direction
   GPIO.output(DIG2, GPIO.LOW)          # set DIG2 as LOW, to control direction
   p1.start(100)                        # set speed for M1 at 100%
   p2.start(100)                        # set speed for M2 at 100%
   sleep(2)                             #delay for 2 second

   print ("Backward")
   GPIO.output(DIG1, GPIO.HIGH)         # set DIG1 as HIGH, to control direction
   GPIO.output(DIG2, GPIO.HIGH)         # set DIG2 as HIGH, to control direction
   p1.start(100)                        # set speed for M1 at 100%
   p2.start(100)                        # set speed for M2 at 100%
   sleep(2)                             #delay for 2 second

   print ("Right")
   GPIO.output(DIG1, GPIO.LOW)       
   GPIO.output(DIG2, GPIO.HIGH)    
   p1.start(100)                     
   p2.start(100)                   
   sleep(2)                        

   print ("STOP")
   GPIO.output(DIG1, GPIO.LOW)          # Direction can ignore
   GPIO.output(DIG2, GPIO.LOW)          # Direction can ignore
   p1.start(0)                          # set speed for M1 at 0%
   p2.start(0)                          # set speed for M2 at 0%
   sleep(3)                             #delay for 3 second


except:					# exit programe when keyboard interupt
   p1.start(0)				# set speed to 0
   p2.start(0)				# set speed to 0
					# Control+x to save file and exit

Now, I can run two motors in forward and backward direction. Thank you.

2 Likes

Hello I have this same problem but I tried your code and it didn’t work. When you say you opened pins 32 and 33 as PWM0 and PWM3 what does this mean? I am new to the jetson nano so I haven’t learned the basics yet.

1 Like

Hi carlos144,

Please help to open a new topic. Thanks