0

I'm building a robot car as a beginner project with a Raspberry Pi 3 Model B. It was easily possible to control the motors with help of the wiringPi library in C. To adjust the speed of my car I want to use Software PWM, including the softPwm.h file. Last week it worked without a problem, now I have loaded the battery pack (7,4V 2200 mAh) and it won´t work anymore. It is possible to make the car start, it drives about 3 seconds, then the Pi freezes and I'm losing the connection to it (it works as a wifi hotspot). It is not possible to control the Pi anymore, I have to restart it. Here you can see my code:

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

#define ENABLEA 23 
#define ENABLEB 28
#define INPUT1 24 
#define INPUT2 27 
#define INPUT3 29
#define INPUT4 25


int main(int argc, char **argv)
{

    if(wiringPiSetup()==-1){
        printf("Error\n");
        return 1;
    }
    pinMode(ENABLEA, PWM_OUTPUT);
    pinMode(ENABLEB, PWM_OUTPUT);
    pinMode(INPUT1, OUTPUT);
    pinMode(INPUT2, OUTPUT);
    pinMode(INPUT3, OUTPUT);
    pinMode(INPUT4, OUTPUT);

    softPwmCreate(ENABLEA,1,100);
    softPwmCreate(ENABLEB, 1, 100);

    softPwmWrite(ENABLEA, 25);
    softPwmWrite(ENABLEB, 25);

    digitalWrite(INPUT1,1);
    digitalWrite(INPUT3, 0);
    digitalWrite(INPUT2, 0);
    digitalWrite(INPUT4, 1);
    delay(5000);
    digitalWrite(ENABLEA, 0);
    digitalWrite(ENABLEB,0);
    digitalWrite(INPUT1, 0);
    digitalWrite(INPUT4,0);


    return 0;
}

For comparison, here is the working code without Software Pwm:

#include <stdio.h>
#include <wiringPi.h>


#define ENABLEA 23 
#define ENABLEB 28
#define INPUT1 24 
#define INPUT2 27 
#define INPUT3 29
#define INPUT4 25


int main(int argc, char **argv)
{

    if(wiringPiSetup()==-1){
        printf("Error\n");
        return 1;
    }

    pinMode(ENABLEA, OUTPUT);
    pinMode(ENABLEB, OUTPUT);
    pinMode(INPUT1, OUTPUT);
    pinMode(INPUT2, OUTPUT);
    pinMode(INPUT3, OUTPUT);
    pinMode(INPUT4, OUTPUT);

    digitalWrite(ENABLEA, 1);
    digitalWrite(ENABLEB, 1);
    digitalWrite(INPUT1, 1);
    digitalWrite(INPUT3, 0);
    digitalWrite(INPUT2, 0);
    digitalWrite(INPUT4, 1);
    delay(5000);
    digitalWrite(ENABLEA, 0);
    digitalWrite(ENABLEB,0);
    digitalWrite(INPUT1, 0);
    digitalWrite(INPUT4,0);
    printf("fertig\n");

    return 0;
}

Do you guys have an idea, what´s the matter of the problem?

What I have tried so far:

  • connected the Pi to a Monitor to check if the Pi is still alive after the crash --> Got the message the SD card was ejected, then the monitor freezes.

  • connected the battery pack to the power supply --> still freeze

  • adjusted the parameters of the softPwmWrite functions --> motors are slower, but still freeze

  • read the /var/log/syslog and /var/log/message files --> no entries at the time around the freeze

  • You say it worked last week. Was that working with or without PWM? The question suggests the only actual change is in using the battery. – joan May 24 '18 at 15:43
  • 3
    Sounds more like a "brown out" situation because of the battery pack. If your program was crashing due to SoftPWM, it would not likely cause you to loose connection to the pi. But an issue with the pi's power could – Chad G May 24 '18 at 16:28
1

I have a strong hunch that @ChadG and @joan have identified the issue: power. RPi's are not known for their robust power supplies, and (we're guessing) you're using a battery for Pi power. That could easily compound the problem unless you've taken steps to isolate the RPi from the car and its motors. The solution for you is two steps:

  1. Check & verify the RPi input voltage is not suffering transients caused by your motors and peripeheral circuitry. If you happen to have a decent storage oscilloscope handy, you should use that. If not, you may be able to monitor your supply voltage using "piPower" software. Please try that with your setup, and let us know the results.

  2. Isolate the RPi input voltage from your car and its motors. You've not provided a schematic showing how this is wired, so I'm not going to make guesses at how you've done this. However, "isolation" can be easily accomplished with a storage capacitor and a diode. Connect the capacitor across the input power rails, and insert a diode between the battery and the input to the RPi power rail. Connect the cathode to the RPi, the anode to the battery/regulator.

Again, if you'll use the "schematic tool" to draw a simple sketch of your power circuitry, we can get into more details if necessary.

1

One simple way to test for issues is to disconnect the motors (you could connect low-power lamps / LEDs instead to visualize what the RPi is trying to do). If you still get resets, it may be a software issue. If not, it's almost certainly power glitches.

Another test is to run a motor constantly from a separate battery in the vicinity of the RPi and see if the wireless communication is not disturbed. This is most likely not the immediate problem, but something worth testing before you invest your time into that particular motor model.

As a side note, consider using an Arduino or a similar microcontroller-based platform for this project. One advantage of MCU over CPU is power stability: e.g. the ATmega chip used in Arduino can work with anything between 1.8 and 5V with appropriate clock settings. An RPi may not be happy even with 4.5V.

-2

Thank you for your answers.

I tried to use the piPower software, but it caused a freeze as well. Finally the solution is quite simple, I forgot about the sudo when starting my program.

  • Glad you're making progress, but not using sudo shouldn't have caused your Pi to "freeze". – Seamus Jun 7 '18 at 15:12

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.