0

I am using software driven PWM which is capable of outputting a PWM signal on any GPIO pins. Following Gordon's page.

I am trying to run 2 DC motors

I am using GPIO pin 18 also knowing that it is used for Hardware Driven PWM.I am able to compile program with no errors but after executing the program i dint recieve any output(Motors are not working)

pi@raspberrypi:~ $ g++ wipi.cpp -lwiringPi -lpthread -o wipi pi@raspberrypi:~ $ sudo ./wipi

Apologies My understanding in implementing PWM (using C++) is limited. Any hints and suggestion would be great help. Thank you !

#include<iostream>
#include<wiringPi.h>
#include <softPwm.h>

using namespace std;

#define  MotorPin1 17
#define  MotorPin2 18
#define  MotorEnableDC1 4

#define MotorPin3 27
#define MotorPin4 22
#define MotorEnableDC2 23


 int main(void)
 {

 if(wiringPiSetupGpio()==-1)
{
    cout<<"Setup wiring pi failed";
    return 1;
}


pinMode(MotorPin1,OUTPUT);
pinMode(MotorPin2,OUTPUT);
pinMode(MotorEnableDC1,OUTPUT);

pinMode(MotorPin3,OUTPUT);
pinMode(MotorPin4,OUTPUT);
pinMode(MotorEnableDC2,OUTPUT);

softPwmCreate(MotorEnableDC1,0,50);
softPwmCreate(MotorEnableDC2,0,50);

while(1)
{

    softPwmWrite(MotorEnableDC1,HIGH);
    digitalWrite(MotorPin1,HIGH);
    digitalWrite(MotorPin2,LOW);
    delay(5000);
    softPwmWrite(MotorEnableDC1,LOW);

    softPwmWrite(MotorEnableDC2,HIGH);
    digitalWrite(MotorPin3,LOW);
    digitalWrite(MotorPin4,HIGH);
    delay(5000);
    softPwmWrite(MotorEnableDC2,LOW);
}
return 0;
}

3 Answers 3

2

Assuming that you have wired up correctly the most obvious error is the softPwmWrite() parameter of HIGH which I assume will be 1.

You need to give a larger value, try something like 100000 (I don't know what the legal range is but I believe it is clipped to the maximum value).

Given that you use softPwmCreate(MotorEnableDC1,0,50) I would try softPwmWrite(MotorEnableDC1, 50).

The relevant prototypes are

extern int  softPwmCreate (int pin, int value, int range) ;
extern void softPwmWrite  (int pin, int value) ;
extern void softPwmStop   (int pin) ;
2
  • Thanks for the revert Joan .i have connected all devices and wiring correctly because it was working fine when i was trying simple forward reverse program. yes you are correct parameter HIGH is 1 because To switch on /Off the power to run motors . But " softPwmWrite() " is used either to enable on or off(i am sorry if i misunderstood). Software PWM takes values from 0 (Off) to 100(full speed on in case of Motor) that i have mention 50 that is half the speed in " softPwmCreate(MotorEnableDC1,0,50); ". I dont know where i am doing wrong . :(. Hardware PWM takes maximum value upto 1024
    – Atila Hun
    Jan 26, 2017 at 11:22
  • Try 50. See edited answer.
    – joan
    Jan 26, 2017 at 11:30
1

Try this for a half-duty cycle (half speed)

softPwmWrite(MotorEnableDC1,25);
softPwmWrite(MotorEnableDC2,25);
1

enter image description here You have to use the wiring pi / gpio pin numbers not the physical ones it should be 0 1 2 for dc1 and 3 4 5 but use the wiring pi pins (look at the pic you will understand)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.