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