Using two xbee's - series 1 , gy-521 and two DC motors.

Hi, I am totally vexed with trying to set up two xbee's, two dc motors and gyroscope - gy-521.

I am using two xbee series 1
Two xbee pro shields
One Arduino Uno, that is going to control two dc motors in a toy rc car with L298n dual motor module
One Arduino Mega 2560, with gy-521 gyroscope
Also using MacBook Pro El Capitan.

I have set up the xbee's so that they are communicating and have used them to control a micro servo, via a potentiometer - one of Jeremy Blum's tutorials.

I have searched online, but unless I'm missing something, I cannot find a suitable program.

What I am trying to achieve, is to be able to control the rc car's two DC motors - one for steering left, right, straight. And the other for forward, reverse, stop - with the gy-521, so I can tilt my hand left and right for steering and up and down for forward, reverse etc.

Some of the codes that I have found (mainly hand gesture rc cars etc.) haven't worked for me, mainly because I cannot work out the parts of code to use for DC motors. I have tried so hard before asking for help.

I have only been using Arduino since July this year, and can usually work things out, but this has done me!

Most of the codes on line, have connections for x & y, and x,y, z. The gy-521 doesn't state these, so have been connecting SCL and SDA to the Mega 2560 respectively, and also tried Analog pins 4&5. I don't know exactly what's x & y, or z pins are on the gy-521.

I hope I have made things clear in this post and put it on the correct forum, as this is my first time!

Any help will be most appreciated and I thank you in advance.

(MOD. removed empty lines)

Two xbee pro shields

Which ones? There are half a dozen different XBee shields available, ranging from not bad to junk.

I have searched online, but unless I'm missing something, I cannot find a suitable program.

So, write one.

Some of the codes that I have found (mainly hand gesture rc cars etc.) haven't worked for me, mainly because I cannot work out the parts of code to use for DC motors. I have tried so hard before asking for help.

Most such code would use a library that contains a digitalWrite() statement to control direction and an analogWrite() statement to control speed, Post SOMETHING!

I don't know exactly what's x & y, or z pins are on the gy-521.

and I'm n ot going to post a link. I want you guys to do the research for me. Good luck with that.

I hope I have made things clear in this post

Nope.

@Brett72
PaulS can be quite direct sometimes. But if you separate his message from his style you see he is asking for more information as he is eager to help.

He is asking for the exact XBEE's you are using.

For specific projects it is hard to find the exact code, snippets is often the best you get. Posting the code you currently have, what you expect and what you see helps us to help you. In general splitting up a project in separate parts - e.g. how to control a motor until you really understand the parts - is a good practice to get bigger projects working.

For the electronic part, a schema (handwritten) is always helpful, and datasheets of the components used.

It might be good to state you knowledge level (SW/HW/yrs experience). This increases the chance that answers are on the right level.

With respect to your question I see these potential problem area's

  1. The xbee communication.
    Q: does it work as expected or not?

  2. Controlling the motors of the RC cars.
    Q: seems to be under control

  3. Getting the gy-521 to work
    Q: this seems to me your biggest problem, can you confirm?
    Q: can you give all information you have (links to datasheets) as I am unfamiliar with this device.
    Q: can you post the code you work with and explain your expectations, versus what the code does.

  4. Integrating different parts into one project
    Q: this seems to me a lesser problem, right?

Thank you Rob, I was put off by PaulS but now you have explained what he was trying to do, i wil post as much detail as I can and get back to it.

I have two xbee's that are:

DIGI INTERNATIONAL XB24-API-001 MODULE, XBEE, 802.15.4, PCB ANTENNA
Data Rate: 250Kbps
MCU Supported Families: -
Module Applications: RF Communications, Wireless Connectivity-ZigBee
Module Interface: UART
Processor Type: -
SVHC: To Be Advised
Supply Voltage Max: 3.6V
Supply Voltage Min: 2.8V
Supply Voltage Range: 2.8V to 3.6V
Wireless Protocol: IEEE 802.15.4

I used SainSmart XBee USB Adapter Explore to set up the xbee's on XCTU

The xbee pro shield is also SainSmart - high quality shield RF module for Arduino xbee mega nano zig bee
Platform UK

I can use the GY-521 in a sketch with Arduino Mega 2560 to control a servo, and since my last post, I have read a lot more on gyroscopes with accelerometers. What I cannot get my head round, is that on some sketches I've looked at on the Internet, it says to connect X, Y and Z to certain pins on the Arduino board, but the GY-521 doesn't state these pins on the board.

I think my head is so full at trying to understand it all, that I'm overlooking something that is probably totally obvious.

Example - from Jeremy Blum's SudoGlove:

const int right_gyro_4Y = 9;
const int right_gyro_4X = 10;

The schematics from Jeremy Blum's SudoGlove show that they are Analog pins

I have read the data sheet on GY-521 and it states that SCL and SDA pins are used to connect to 4 & 5 on Uno or on Mega 2560, on the SCL & SDA pins. ADO can be connected to ground or pin 2 and INT to pin 2 of Arduino. (if I have read it correctly).

So my problem is, that what 4Y and 4X relate to on the GY-521?

As for the Xbee's, I have set them up to communicate with each other with XCTU and used them to control a servo with a potentiometer. The baud rate I used was 34800.

I can control two DC motors with L298n dual motor control, using infrared to control on an rc car that has one DC motor for steering (left, right centre) one DC motor for direction (forward, reverse, stop). I will post the sketch from my Mac, as I'm writing this on my iPad. Please bare with me.

The problem I have, is writing a sketch to control the DC motors with the GY-521 via the xbee's.

Basically, I want to create a wrist controlled gyroscope using Mega 2560 and xbee, to control the DC motors on Arduino Uno and xbee.

I hope this post is better this time.

Thank you.

The xbee pro shield is also SainSmart - high quality shield RF module for Arduino xbee mega nano zig bee
Platform UK

A link would be far better. Of course, SainSmart is going to claim that the shield is the best thing since sliced bread. That doesn't make it so.

I think my head is so full at trying to understand it all, that I'm overlooking something that is probably totally obvious.

Lnks. You are assuming that we are familiar with Jeremy Blum's work. While I've heard on him, it's only because his projects seem to cause a lot of people grief when they try to implement them.

The problem I have, is writing a sketch to control the DC motors with the GY-521 via the xbee's.

The project needs to be broken down into parts that can be implemented. Controlling the DC motors is one part that needs to be handled. Getting data from the GY-521 is another. Getting the data from one Arduino to another via the XBees is another. Getting the data from the GY-521 to control the motors is the final part.

What exactly is one DC motor supposed to do when the raw x data from the GY-521 is -30? When it is 127?

Thanks PaulS. This is what I have so far breaking it down as recommend, and starting with one DC motor with gyroscope/accelerometer and no xbee. Photo attached also.

I know what your saying about SainSmart and also, the Mega 2560 is funduino!

I have only been making robotics for about 6 months after watching Jeremy Blum's on line tutorials. The one for potentiometer is here - Tutorial 09 for Arduino: XBee Wireless Communication - YouTube

I found him on YouTube just searching around, and had no experience what so ever.

I have a problem with integrating xbee's into the project to send codes for the gyroscope/accelerometer, one xbee to another.

This is a sketch to control direction of 1 DC motor via gy-521 and no xbee.

#include <Wire.h>
#include <I2Cdev.h>
#include <MPU6050.h>
MPU6050 mpu;

int16_t ax, ay, az;
int16_t gx, gy, gz;

#define pin1 3
#define pin2 5

void setup(){
Wire.begin();
Serial.begin(38400);
Serial.println("Initialize MPU");
mpu.initialize();
Serial.println(mpu.testConnection() ? "Connected" : "Connection failed");
pinMode(pin1,OUTPUT);
pinMode(pin2,OUTPUT);
}

void loop()
{
mpu.getMotion6 (&ax, &ay, &az, &gx, &gy, &gz);

ax = map(ax, -17000, 17000, -1500, 1500);

Serial.println(ax);
if(ax > 0){
if(ax<255){
Serial.println(ax);
analogWrite(pin2,ax);
}
else{
Serial.println("+255");
analogWrite(pin2,255);
}
}
if (ax<0){
if (ax>-255){
Serial.println(ax);
analogWrite (pin1, ax-ax-ax);
}
else{
Serial.println("-255");
analogWrite(pin1, 255);
}
}
delay(50);
}

This sketch above works ok but a bit sparadic sometimes, making me wonder if it need stability somewhere.

I have used lc293 ic and attached pins from top left of the ic

pin 1 to pin 9 (all on Mega 2560 pwm)
pin 2 to pin 3 (pwm)
pin 3 & 6 to the motor
pin 4, 5, 12 & 13 to ground
pin 7 to pin 9 (pwm)
pin 8 & 9 to 5v

I don't know for sure, but I think pin 9 on mega is providing a live feed and 3 & 5 are for the signal to the ic.

The gyro is connected to 5v, Ground, SDA & SCL

All the grounds are connected.

So, when I can stabilise this sketch, I want to add another motor and use it on L298n dual motor control.
Then add the xbee's to Arduino Uno and Mega 2560, using the Mega for the controller and gyro.

The steering motor should be tilt left, right and centre on X axis (I believe)
I want to control the other motor for direction forward, stop, back etc. also with gyro and y axis.

The sketch below is for a gyro control of a servo (may use it in another project) but I want to get the motors up and running first. Just showing as an example because it has better stability.

This sketch seems more stable for whatever reason, maybe because of the simplicity of the values? I don't know.

#include <Servo.h>
#include <Wire.h>
#include <I2Cdev.h>
#include <MPU6050.h>
MPU6050 mpu;

int16_t ax, ay, az;
int16_t gx, gy, gz;

Servo myservo;
int val;
int prevVal;
void setup()
{
Wire.begin();
Serial.begin(38400);
Serial.println("initialize MPU");
mpu.initialize();
Serial.println(mpu.testConnection());
myservo.attach(9);
}
void loop()
{
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
val = map(ay, -17000, 17000, 0, 179);
if (val != prevVal)
{
myservo.write(val);
prevVal = val;
}
delay(50);
}

This should be the photo - couldn't upload it before.

I want the motors to turn clockwise stop, then go counter clockwise.

I have only have what's in the sketch - +255, -255, 0

I have tried that sketch again, and it doesn't always work for some reason, but the servo sketch seems to be ok.

Arduino .pdf (998 KB)

GY-521 MPU-650 Data Sheet

Your post is about using XBees to send data. Still no XBee shield link...

Hi PaulS
Sorry, all i can find are these.

xbee data sheet

http://www.cooking-hacks.com/skin/frontend/default/cooking/pdf/90000982_A.pdf

i cannot get onto sainsmart website for some strange reason, but it says that it is based on maxstream, which Arduino seem to cover.

This is all i can get on the sainsmart

The SainSmart XBee board for Arduino mates directly with the Arduino USB board and allows to wireless communicate over a modified ZigBee protocol using the popular XBee module from Maxstream.

This shield (an Expansion Board without XBee module) is a compliant solution designed to meet low-cost, low-power wireless sensor networks with special needs. The module is easy to use, low power consumption, and the provision of critical data between devices reliable transmission. As the innovative design, XBee-PRO can be in the range 2-3 times beyond the standard ZigBee modules. XBee-PRO modules work in the ISM 2.4 GHz frequency band. The MaxStream's XBee (1 mW) Zigbee module is pin-compatible.You can use a simple AT command to advanced configuration. An OEM developer is now XBee code development package. It is self-developed in collaboration with the MaxStream ZigBee/802.15.4 RF module code.

Features

  • Shield for the platform.
  • Shield does not include an XBee module.
  • Any XBee module will work with the shield.
  • Board mates directly with the Arduino USB board and allows to wireless communicate over a modified ZigBee protocol using the popular XBee module from Maxstream.

Hi just a off-topic question (sort-of):

Have you been able to connect your USB Shield with your el capitan os? I use the USB Xbee explorer from sparkfun and since el capitan i cannot use them anymore. At the sparkfun forum they said the FTDI support in el capitan is broken.

That's about what I figured. You got the worst possible shield to connect the XBees to. You can choose to use Serial to talk to the XBees or you can choose not to talk to them. Not much choice there. You can't (effectively) then use Serial to talk to the PC for debugging purposes.

So, what code are you trying to run, and what issues are you having? Can you summarize, please?

Solik - I can use it with El Capitan, the one I use is in the link below.

Features
Used for the XBee module configuration parameters
Easily connected to a PC via mini USB cable
XBee-setting support software X-CTU
Also used as a USB-TTL adapter
Voltage: +5V(USB Power)

I was able to upload firmware easy, using X-CTU. Perhaps try using X-CTU with the shield you have?

Hope this helps.

PaulS. I will get back to you asap. Thanks for your help so far. I totally understand about the shield, they will say they're high quality - Not!

Interesting, seems they don´t use ftdi drivers for connecting. I use the sparkfun usb adapter which needs ftdi support which seems to be broken with El capitan

I am so nearly there with the code I have been writing, but just need a little help getting it finalised.

I have started with putting together code for using a gyroscope/accelormeter GY-521, series one xbee, xbee shield, on one Arduino Genuino/Uno. And another Arduino Uno, xbee and shield, with a servo. All as detailed in previous posts.

I have got the two xbee's talking, and have a little problem getting the servo to move left, back to centre, right, back to centre.

I have only been able to get the servo twitching about using the values below.

I don't know if the values should be the same in each part of the codes. I have tried many different ways, and stopped before I mucked all my hard work up!

Man I would be grateful for any assistance - thank you.

Controller code: Arduino Genuino/Uno xbee & gyro

#include <Wire.h>
#include <I2Cdev.h>
#include <MPU6050.h>
MPU6050 mpu;

int16_t ax, ay, az;
int16_t gx, gy, gz;

#define pin1 4
#define pin2 5
int val;
int prevVal;
void setup(){

Wire.begin();
Serial.begin(38400);
Serial.println("Initialize MPU");
mpu.initialize();
Serial.println(mpu.testConnection() ? "Connected" : "Connection failed");
pinMode(pin1,OUTPUT);
pinMode(pin2,OUTPUT);
}
void loop()
{
mpu.getMotion6 (&ax, &ay, &az, &gx, &gy, &gz);
val = map(ax, -1700, 1700, 0, 180);
if (val != prevVal)
Serial.println(val);
delay(50);
}


Receiver code: Arduino Uno, xbee and servo

#include <Servo.h>

int servoPin = 9;
Servo myservo;

void setup()
{
Serial.begin(38400);
myservo.attach(servoPin);
}

void loop()
{
while(Serial.available()==0);
int data = Serial.read() -'0';
int pos = map(ax, -1700, 1700, 0, 180);
Serial.println(pos);
myservo.write(pos);
Serial.read();
}