Wiring L293D with Raspberry Pi

Now we will interface L293D with Raspberry Pi as shown in the following diagram:

Figure 8.8

In the previous diagram:

  • GPIO 12 of Raspberry Pi is connected to enable 1 (pin 1) of L293D
  • GPIO 14 of Raspberry Pi is connected to input 1 (pin 2) of L293D
  • GPIO 15 of Raspberry Pi is connected to input 2 (pin 7) of L293D
  • GPIO 23 of Raspberry Pi is connected to enable 2 (pin 9) of L293D
  • GPIO 24 of Raspberry Pi is connected to input 3 (pin 10) of L293D
  • GPIO 25 of Raspberry Pi is connected to input 4 (pin 15) of L293D
  • VSS of L293D is connected to 5V output from Raspberry Pi
  • GND of L293D is connected to ground terminal of Raspberry Pi

Here enable 1 and enable 2 pin of L293D are responsible for driving the motor and using these pins we will control the speed of motor by applying PWM. PWM control the speed by providing a series of on-off pulses with varying duty cycle.

To increase the speed of motor we increase the duty cycle which in turn increases the high signal duration in single pulse compared to the duration of a low signal which results in the motor being in on state for a longer duration then off during one single pulse. Here a single pulse is a combination of on and off combined.

Let's understand our case with the following diagram:

Figure 8.9

In the preceding diagram:

  • DC = 0%: Enable voltage signal will be zero all the time
  • DC = 25%: Enable voltage will remain high for only ¼ of the duration of a single pulse
  • DC = 50%: Enable voltage will remain high for only ½ of duration of single pulse
  • DC = 75%: Enable voltage will remain high for only ¾ of duration of single pulse
  • DC = 100%: Enable voltage will remain high for full duration of single pulse

From the previous diagram, we can understand how PWM controls the speed of motor.

Now that we have completed the circuit and connections, let's write the code. Here we will implement MQTT protocol for communication between car and remote-control client.

We need Node.js runtime to be set up in Raspberry Pi and MQTT broker running on the cloud/server. Since we have already done these setups in the previous chapter so we will not repeat them here again.

Create a file with the name car_control.js and include pigpio module. This module gives access to GPIO of Raspberry Pi:

var GPIO = require('pigpio').Gpio;

Set up the GPIO in code as per connections made earlier. Here we initialize the GPIO of Raspberry Pi as per the requirement:

var red_LED = new GPIO(17,{mode: GPIO.OUTPUT}), 
green_LED = new GPIO(5,{mode: GPIO.OUTPUT}), front_left_LED = new GPIO(19,{mode: GPIO.OUTPUT}), front_right_LED = new GPIO(26,{mode: GPIO.OUTPUT}), enable_1 = new GPIO(12,{mode: GPIO.OUTPUT}),
enable_2 = new GPIO(23,{mode: GPIO.OUTPUT}),
input_1 = new GPIO(14,{mode: GPIO.OUTPUT}),
input_2 = new GPIO(15,{mode: GPIO.OUTPUT}),
input_3 = new GPIO(24,{mode: GPIO.OUTPUT}),
input_4 = new GPIO(25,{mode: GPIO.OUTPUT});

var rightIndicatorHandler='', leftIndicatorHandler= '';

Now we will write functions for all the different movements of a car that is forward, reverse, right turn, and left turn. These functions will return a callback to the calling function as acknowledgment.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.253.221