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;
}
1

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) ;
  • 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 '17 at 11:22
  • Try 50. See edited answer. – joan Jan 26 '17 at 11:30
0

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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