Arduino Forum

Using Arduino => Programming Questions => Topic started by: TruePatrick on Jul 22, 2017, 03:03 pm

Title: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Jul 22, 2017, 03:03 pm
That's the seccond time i visit this forum. Now i am smarter then before. This is my first arduino project - selfriding robot that avoides obstacles.
SETUP:
-4WD platform with 4 DC motors.
-Romeo V2.2 board.
-Servo SG90
-distance sensor - ultrasonic HC-SR04
I downloaded the code from similiar project and modified this on my purpose, especialy for my sensor.
This is quite strange, when i connect my romeo board via USB and upload same code with 5V, everything starts quickly and another time when i connect board also via USB ---- robot starts moving slowly back (servo doesnt work) and stops. The White diode on the board with "L" name lights. And the same thing is when i connect 8 x 1,5V AA battery which are connected in series.
Here's the photo of my robot:
http://imgur.com/a/6Y6w4
and code below
https://github.com/TruePatrick/ROMEO-V2

:EDIT: - video here how's it looking - https://www.youtube.com/watch?v=SoNNOrAiGtc   -  check out my last answer, i have almost fixed this.
Title: Re: Program react differently regards of POWER source - USB/BATTERY
Post by: johnwasser on Jul 22, 2017, 04:12 pm
I don't see anything connected to "ServoPWR".  

The Romeo board uses a 32U4 processor like the Leonardo.  You should put in the:
Code: [Select]
     Serial.begin(9600);
     while (!Serial) {}

That will wait for the USB connection to complete.  It may be that your sketch can't use Serial when there is no USB connection.  In that case you may need to put  "if(Serial)" before any Serial calls like Serial.read() or Serial.print().  You might also need to use:
Code: [Select]
     Serial.begin(9600);
     while (!Serial && millis() < 5000) {}

 That way the waiting for a USB connection will time out after 5 seconds.
Title: Re: Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Jul 22, 2017, 04:39 pm
Let's see:
Code: [Select]
Serial.begin(9600);
     while (!Serial) {}


Code: [Select]

Arduino:1.8.3 (Windows 10), Płytka:"Arduino Leonardo"

RoverCommander:24: error: no match for call to '(HardwareSerial) ()'

  while(!Serial1()){

                 ^

exit status 1
no match for call to '(HardwareSerial) ()'

Ten raport powinien zawierać więcej informacji jeśli w
File -> Preferencje zostanie włączona opcja "Pokaż
szczegółowe informacje podczas kompilacji"

Title: Re: Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Jul 22, 2017, 04:50 pm
Here's the thing. When i upload code on the board everything looks fine. Servo moving, sensor sees obstacles and robot try to avoid it. BUT right after i plug it out and THEN plug it in again via USB robot behave differently. Starts moving slowly back and servo and sensor don't move. Until i plug it again via usb and upload code from the beggining
Title: Re: Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Jul 22, 2017, 05:18 pm
Is it becamuse once for like 0.2 sec i switched on the switch for external batery when usb cable was connected by mistake. I've heard that you mustn't do it!
Title: Re: Program react differently regards of POWER source - USB/BATTERY
Post by: johnwasser on Jul 22, 2017, 06:24 pm
Let's see:
Code: [Select]
RoverCommander:24: error: no match for call to '(HardwareSerial) ()'

  while(!Serial1()){ 

Where did that line come from?!?

Whoever wrote that did not understand why the Leonardo needs: 
Code: [Select]
     Serial.begin(9600);
     while (!Serial) {}
(Even though I explained why when I recommended it.)

Title: Re: Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Jul 22, 2017, 06:43 pm
This doesn't work, even when i delete ALL this lines which are responsible for serial kontrol with pc. When i upload the code for the first time motorst starts move forward, then i normally plug it out from the pc and switch it on on external batery. Robot starts slowly move backward - seems like this line is responsible for that reaction
Code: [Select]

if(distance<=10){
      back_off (100,100);
    }


Yep it is this line about, when i change "back_off (200,200)", and repeat all the stuff that i do - motors starts going backward faster.
It is all about the sensor i think.
Here's the code that i'm working on currently.
https://github.com/TruePatrick/Here-it-is - let me know if this site is working.
Title: Re: Program react differently regards of POWER source - USB/BATTERY
Post by: johnwasser on Jul 22, 2017, 11:27 pm
Code: [Select]
  distance = (duration/2) / 29.1;


   // if distance outside 5 - 20 cm range
   if(distance>15){
     obstacle = false;
     advance (200,200);
   }
   else {
     if(distance<=10){
       back_off (200,200);
     }
     else{


A: If the sensor isn't working or no echo is detected pulseIn() returns 0 which would cause the repeated back_off().  You might get better results if you treat 0 the same as >15.

B: Your comment about 5-20 cm range doesn't match the code
Title: Re: Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Jul 22, 2017, 11:40 pm
Don't look at the commends, they are from the other code.
now i have this code and the same problem - soon i'll upload the video on youtube and show you.
Code: [Select]

void Explore(){

  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;

 
  distance = (duration/2) / 29.1;

 
 if(distance>12 || distance<0){
    obstacle = false;
    advance (120,120);
  }
  else{
   
    // too close, distance < 15 cm
    if(distance<=1){
      back_off (150,150);
    }
    else{
     
      obstacle = true;
     
      if(GetPosition() > GetMiddlePosition()){
        turn_R (200,200);
       
       
        SetServoDirection(0);
      }
      else{
        turn_L (200,200);
     
       
        SetServoDirection(1);
      }
  }
}
}


I'm working on this like whole day and now night. I refresh this site every 10 minutes with hope that you adwise me.
VIDEO - https://www.youtube.com/watch?v=SoNNOrAiGtc
THE code works only when i will upload it via usb. After i plug out and then again plug in, robot behave like on the beginning of the video
Title: Re: Program react differently regards of POWER source - USB/BATTERY
Post by: sterretje on Jul 23, 2017, 12:01 am
@johnwasser

You should never use while(!Serial) in a 32U4 based Arduino if it needs to run from external power; it will hang forever.
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Jul 23, 2017, 12:09 am
Even if i delete this whole lines. The problem still appears. Here's the robot of this guy form who i have this code.
He have different sensor so i modify my code that it can fit to my sensor - https://www.youtube.com/watch?v=87TKCV07cjM&index=15&list=PL4Hx9Al-UrglylyfTldES9apzLg4fAZO7
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Jul 23, 2017, 01:26 am
Seems like I repaired the code for exploration. Now it works everytime i plug it into usb but not on external baterry!
Code: [Select]

/*
  # This code has been inspired by an article from Dominique Meurisse : http://arduino103.blogspot.fr/2011/06/detecteur-de-proximite-infrarouge-sharp.html
  #
  # this script is distributed under Creative Commons Attribution-ShareAlike 3.0 License
*/


void Explore(){

  #define trigPin 13
  #define echoPin 12
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

 
  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;

 
  distance = (duration/2) / 29.1;

   Serial.print(distance);
   Serial.println(" cm");

   if(distance>15){
     obstacle = false;
     advance (200,200);
   }
   else {
     if(distance<10 && distance>0){
       back_off (200,200);
     }
    else{
      if(distance>0){
      obstacle = true;
     
      if(GetPosition() > GetMiddlePosition()){
        turn_R (200,200);
        SetServoDirection(0);
      }
      else{
        turn_L (200,200);
        SetServoDirection(1);
      }
      }
  }
}
}


I add my previous project to find out if sensor works. It measures the distance in cm!
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Jul 23, 2017, 04:38 pm
CAN anyone help me? I'm feeling done now...
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: johnwasser on Jul 23, 2017, 05:40 pm
Maybe you should add some LEDs to light up based on the distance reading.  That way you can see if the problem is your sensor or your motor driver.
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Jul 23, 2017, 06:07 pm
I will tomorrow. I need a break from all this. The thing is, i've discover that when i will plug board via usb IT ALSO gather the power from the output baterry even when switch is off. I measured this by multimetr. That's strange. I tried adding more baterry, i had 18,5V but still was the same. It's not about voltage. I tried to charge this vai USB from POWERBANK 5V. Worked fine ... but i had to have connected external battery to work, even if it was on OFF switch.
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: Gabriel_swe on Jul 23, 2017, 09:06 pm
Have you measured voltage to ultrasonic sensor when on USB power and on battery power?

Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Jul 23, 2017, 09:25 pm
How am i suppose to measure it? Sorry for asking, i'm a beginner. I measured the voltage from the external battery when usb cable was pluged into board. In theory usb has 5V (distance sensor is connected to the red 5V on the board) and when i measured the voltage which was in the external battery which was connected to the board (but the switch was OFF) it dropped like 2V!!! From 10V to 8V. Program worked perfectly. How is this possible that besides the switch on the board from the external battery was off (http://imgur.com/a/6Y6w4 - first left Green on the top, lower is the switch), board still collect voltage from the external battery. ---------
3 days ago i accidentaly switched that switch ON when USB was connected, and program schuted down that day. I've read that you musnt do that. 10V from batteries + 5V USB = 15. Max voltage that board can handle is 20V so i didn't worry about it much, i switched it off after 0.5 sec.
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: Gabriel_swe on Jul 23, 2017, 10:13 pm
Motors are always powered from external source. Switch only separate system power from motor power.

To measure voltage, measure on the red pins between GND and 5V, GND and 3,3V and finally between GND and Vin. Do it for both USB power and battery power. I would start with 6 volt battery power as power regulator might be broken.
 
https://www.dfrobot.com/wiki/index.php/Romeo_V2-All_in_one_Controller_(R3)_(SKU:DFR0225) (https://www.dfrobot.com/wiki/index.php/Romeo_V2-All_in_one_Controller_(R3)_(SKU:DFR0225))
Big warning from DF Robot.
"Please Turn OFF the Motor Power Switch when debugging Romeo through USB cable. Or the external power supply(>12V) will destroy your Romeo."

It is possible power regulator on your board is toast from having both USB and battery power source connected. Board can't give you 5V any longer and no power to ultrasonic sensor.

If your servo doesn't move you need to connect 5V to servo power terminal
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Jul 23, 2017, 10:28 pm
Do Debugging mean - reuploading code that has a little change in it?
I hope i didn't toast any circuit on the board.
I will try to connect SERVO to
Motors are always powered from external source. Switch only separate system power from motor power.
That's helpfull answer. When i connect board via usb, and deconnect external power, servo and distance sensor works(i'm sure) BUT not motors.

I would start with 6 volt battery power as power regulator might be broken.
In the house i've found little battery from rc car - 7,2V 500mAh. Tried it but i chave to charge it.
Here's one thing, Seems like board doesn't like external battery at all. When i only on EX battery -code works weird(like i sad before), but when i connect powerbank 5V to the usb port - WORKS perfectly like from PC. I can't find any problem in the code. Ehhh

I will check the voltage later - tomorrow cuz it's 10:30 p.m. and i'm tired.
UPdate: Voltage
USB (powerbank 5V) + 8x 1,5V AA battery (their actuall voltage is ~10V) and switch OFF:
5V - GND: ~5V
3V3 - GND: ~3,3V
Vin - GND: 0  (nothing shows)
8x 1,5V AA battery and switch ON:
5V - GND: 5V
3V - GND: 3,3V
Vin - GND: 9,8V - program starts runnig in circle - starts stoping - the L diode flashing - and the runing for like 1 sec and voltage drops to ~6,3V. -  do you want a video that shows how program works on external battery?

Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: Gabriel_swe on Jul 24, 2017, 12:20 am
Better than expected, even if I thought Vin would be 5V running from USB power.

Voltage drop when battery powered is too much. From 9.8 down to 6.3 is 35% drop and will most likely reboot the unit. Fresh alkaline will be closer to 12 V with low voltage drop. Even fully charged NiMH I would expect to be a little more than 10V and less than 1V drop.

Voltage drop most likely comes when motors start to rotate for first time. To get a visual indication of a reboot you can add a for loop to blink a led in setup() for 10 or 20 seconds.
If you after the blinking just get a tiny movement and it start to blink again - you had a reboot.
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Jul 24, 2017, 12:43 am
Yep, you were right.
When the motors starts, program starts rebooting. What can i do for it? Reduce the external battery voltage?
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: Gabriel_swe on Jul 24, 2017, 08:34 pm
What types of batteries do you have now?
Voltage should match your motors. Most ebay ads for those says 3-6V.
Even if you run motors on low speed they will get full voltage, as power is applied in pulses (PWM)
2 cells LiIon 18650 in series is 7-8 volt and should have no problem deliver current needed.

Another solution is to have separate power to motors and system.
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Jul 24, 2017, 09:29 pm
So i have to use only that much voltage that motors need?
https://botland.com.pl/podwozia-robotow/7289-podwozie-robota-4wd-czterokolowe-z-silnikami-prostokatne.html
Here's the platform with motors - in the description below they say that they need maximum 6V. I used 8x 1,5V alkaline power battery AA from Philips. I though that i can use higher voltage and board will take only that much voltage that needs.
I searched in my desk some batteries, and have found 2 batteries from the head lamp - each Li-ion 18650 8800mAh 3,7V.
How can i separate power? Using separate 6V to the engines port on the board, and for example powerbank 5V to the usb?
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Jul 25, 2017, 03:24 pm
Anyone? Please?
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: aarg on Jul 25, 2017, 03:32 pm
Cease and desist with the videos if you want help. Instead, please provide proper schematics and clear photographs. Attach them to your post, don't use offsite links.
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Jul 25, 2017, 04:32 pm
Yes but since i have posted this post we have already solved some of the problems, and another just appeard. Do i have to modify the whole? Read last answers cuz there is completly description for all of the problems.
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: Gabriel_swe on Jul 25, 2017, 06:51 pm
USB power bank to the system and 4x1,5V battery to motors are good. In that way system gets a stable power source. You could also get a battery holder for 2 pcs of 18650 to get ~7,5V. I don´t think it will damage the 6V motors. Battery holders can be bought cheap on ebay, or that polish site.
Those batteries you have, they are not 8800 mAh. Roughly 3500 mAh (http://lygte-info.dk/info/BatteriesRidiculousRatings%20UK.html) is the most a 18650 can hold.
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Jul 25, 2017, 08:52 pm
I have 4 battery holders for 8x AA batteries. So Either i use 4x AA 1,5V or 2x 18650? Is there a way to don't use powerbank to usb port? Is this Green Port on the board accountable for ONLY motors power or motors power and the board system power? There's not much room for a powerbank so i would not use it
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: Gabriel_swe on Jul 25, 2017, 10:18 pm
I have successfully used 2x18650 but my robot only had 2 motors. (Most) Li-ion batteries can supply plenty of current without voltage drop. In that case you can have switch to ´on´ and only need a single power source.

You have a few (https://www.dfrobot.com/wiki/index.php/Romeo_V2-All_in_one_Controller_(R3)_(SKU:DFR0225)#Power_solution_design) power options listed at DFRobot.
Switch ´off´, separate power source.
5V to either USB port or 5V pin, or 5~23v power source to VIN.
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Jul 29, 2017, 11:00 pm
I'm back with project.
5V from usb when "OFF" is set and 6V from batteries is an option. But why when i connect 10V from batteries to this port and set "ON" program keeps reseting? And what's going on with this "Vin" thing. I can see "Vin" port on my board, but i hadn't connected to this single wire.
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: Gabriel_swe on Jul 30, 2017, 12:00 am
Board reset because of unstable power.
Vin is parallel to power jack (on ordinary boards). Input power that goes to a voltage regulator. Your board is missing the power jack as you have a screw terminal instead (when switch is on).
If you separate power with the switch, you can still feed 6 to 23 volt to Vin, to power the system.
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Jul 30, 2017, 12:47 am
Sorry, but i'm not completely sure if i have understand you. http://imgur.com/a/6Y6w4 - here's photo of my board. First green port from the right top is Motors and System Power. If i switch it on "OFF" it feed only motors, when "ON" motors and system. I tried to connect 10V battery and i have the same problem when i switched it "ON". I don't know how to us Vin port which is first pin from the RED PIN TOP.
But how can i seperate power (Motors and board)?
*that's where and what to connect on the board** - not exactly mine
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Jul 30, 2017, 01:43 pm
I connected 2x 18650 3,7V batteries (around 6V together when i checked this) and i still have this problem. Board keeps reseting
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: TomGeorge on Jul 30, 2017, 02:28 pm
Hi,
Can you post a circuit diagram of your project please, hand draw and post a picture.
Not a Fritzy picture you have supplied.

You seem to be doing all sorts of different things to fix your problems without any logical thought.
Where do you connect the battery you use to power the control board when you remove the USB?

Thanks.. Tom.. :)
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Jul 30, 2017, 02:56 pm
I'll send you picture in 10-15 minutes. Thanks
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Jul 30, 2017, 03:24 pm
It's not the preatiest schemat but there it is.
VCC from the sensor is connected to a 5V n board not VCC. Becamue i watched some tutorials, and that's how it was connected. Motors are connected to a green motors pin on the right.
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Jul 30, 2017, 08:55 pm
Did it help you solve the problem?
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: Gabriel_swe on Jul 31, 2017, 12:31 am
I connected 2x 18650 3,7V batteries (around 6V together when i checked this) and i still have this problem. Board keeps reseting
Sounds like they need a charge.  3V from a Li-ion is almost empty.
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Jul 31, 2017, 12:33 am
When i measure it on multimeter it shows 5,91V. On the batteries writes: "The voltage of full charge and empty condition is 4,2V and 2,75V"
I will charge it.What do you think how much time it would to charge it fully?
And how types of batteries can have influence on the board?
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Jul 31, 2017, 12:26 pm
Hey UPDATE here. I charged my 2x 18650 batteries. Not full but voltage reaches 7,36V. I connected this to a board and switched the switch "ON" that board and motors use voltage from batteries. Seems that all is working correctly, BUT sometimes board reset but on very short time. I will chage it fully and keep your raporting, if it's good now
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Aug 01, 2017, 12:54 pm
What if i ran over the 18650 battery with a car and it is Slightly dented but when i measure it with multimeter it shows 3,65V. Can i use it?
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: johnwasser on Aug 01, 2017, 02:24 pm
What if i ran over the 18650 battery with a car and it is Slightly dented but when i measure it with multimeter it shows 3,65V. Can i use it?
Sure, as long as you use it in an area that won't be damaged if it bursts or catches fire.
https://www.youtube.com/watch?v=ZTzEHsJVZhA (http://"https://www.youtube.com/watch?v=ZTzEHsJVZhA")
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Aug 01, 2017, 02:57 pm
I charged the batteries that it gives 8,1V and almost everything looks fine. I will update you in 3-4 hours if it changed anything. I also added 4 leds to it. I will upload some photos later, and try charge it fully and then tell you if it changed anything.
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Aug 03, 2017, 02:35 pm
Here's update. This 1 battery amlost expload so i threw it away. MY friend will borrow my 2x 18650 don't (know voltage yet) from laptop battery. I will charge them and try one more time.
Title: Re: *VIDEO* Program react differently regards of POWER source - USB/BATTERY
Post by: TruePatrick on Aug 08, 2017, 12:23 pm
Okay that's the end. My friend gave me 2x 18650 and theri voltage is 8,3V. IT all worked, so the topic is done. Thanks you all.