element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Raspberry Pi Forum Python to Raspberry Pi to Arduino to Motors...
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
Raspberry Pi Wishlist
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 5 replies
  • Subscribers 666 subscribers
  • Views 719 views
  • Users 0 members are here
  • raspberry
  • raspi
  • pi
  • gpio
  • pwm
  • motors
  • raspberry_pi
  • rpi
  • arduino
Related

Python to Raspberry Pi to Arduino to Motors...

wallarug
wallarug over 12 years ago

I have a software question based robotics question.

 

 

I know very little about Ardunio and the coding for it.  I have written some of the code that I require but I am now stuck at the Arduino Sketch.  There is two motors, a left and right motor which their speeds are defined as "a" and "b" respectivly.  All I know is that I have to use the analogWrite() command to get this to work with values between 0 and 255.  I have converted all values to work with this (ie. values between 0 - 255).

 

This is what I have so far:

 

 

[code]

/*

* Robot Motor Controller script with PWM speed control

*/

//---------------------- Motors

int motorL[] = {6, 7}

int motorR[] = {8, 9}

int ledPin = 13;

 

 

 

 

if y > 0:

  digitalWrite(motorL[0], HIGH);

  digitalWrite(motorL[1], LOW);

  digitalWrite(motorR[0], HIGH);

  digitalWrite(motorR[1], LOW);

  analogWrite(9, motor_a);

  analogWrite(6, motor_b);

 

 

if y < 0:

  digitalWrite(motorL[0], LOW);

  digitalWrite(motorL[1], HIGH);

  digitalWrite(motorR[0], LOW);

  digitalWrite(motorR[1], HIGH);

  analogWrite(9, motor_a);

  analogWrite(6, motor_b);

 

 

[/code]

 

 

Can someone please fix any Syntex errors and other errors that may be present in my code please.  I would really like this project to work as it is the most complicated I have tried with the Raspberry Pi.

 

 

What I need this to do is:

     - decide whether the motor is going forwards or backwards

     - (Based on the above) set the pins for that direction

     - Interpit the values that are given to it from the TCP server.

     - loop back continuly until the program is terminaled.

 

 

 

 

Any help will be appreciated.

 

 

For anyone who wants to know how the motors are controlled read on...

I will be controlling the robot by using a joystick on a remote computer using the pygame module.  The code for the below is below:

import pygame

[code]Removed By Request[/code]

 

 

So far, I have a TCP server and client working but I have not incorporated it in this script yet. 

 

 

I only want help on the Arduino Sketch.  The Joystick control and TCP are working nicely but the Arduino code is not working (as seen above).

 

 

Any help would be appreciated.

  • Sign in to reply
  • Cancel
  • GreenYamo
    GreenYamo over 12 years ago

    Hello Fergus,

    Will probably be worth asking this in the Arduino forum ? Although if Simon Monk pops in here (I have seen him in this forum a few times) he would probably be able to help, he is the authour of quite a few Arduino books.

    Steve

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • wallarug
    wallarug over 12 years ago in reply to GreenYamo

    This is the updated sketch:

    [code]

     

    /*

    * Robot Motor Controller script with PWM speed control

    */

    //---------------------- Motors

    int motorL[] = {6, 7};

    int motorR[] = {8, 9};

    int ledPin = 13;

     

     

    void setup() {

    Serial.begin(9600);

    int i;

    for(i =0; i > 0; i < 0){

    pinMode(motorL[i], OUTPUT);

    pinMode(motorR[i], OUTPUT);

    pinMode(ledPin, OUTPUT);

    }

     

     

    }

     

     

    void loop() {

     

     

    drive_forward();

    if y > 0:

              digitalWrite(motorL[0], HIGH);

              digitalWrite(motorL[1], LOW);

              digitalWrite(motorR[0], HIGH);

              digitalWrite(motorR[1], LOW);

              analogWrite(9, motor_a);

              analogWrite(6, motor_b);

     

     

    drive_backwards();

    if y < 0:

              digitalWrite(motorL[0], LOW);

              digitalWrite(motorL[1], HIGH);

              digitalWrite(motorR[0], LOW);

              digitalWrite(motorR[1], HIGH);

              analogWrite(9, motor_a);

              analogWrite(6, motor_b);

     

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • GeorgeIoak
    GeorgeIoak over 12 years ago in reply to wallarug

    I'm not sure what type of motors you're using, perhaps servos, looking at your code? You might want to look at Adafruit's page for some helpful tips http://www.ladyada.net/make/mshield/use.html

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • wallarug
    wallarug over 12 years ago in reply to GeorgeIoak

    They are DC motors.  I CANNOT use AF library becasue I don't have their shield.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • iagorubio
    iagorubio over 11 years ago in reply to wallarug

    A couple of things:

     

    What kind of controller you are using ?

     

    You should not try to drive DC motors directly from a Arduino - or any inductive load.

     

     

    Seems like you are mixing somewhat things.

     

    The first one is like if you are using C and Python at the same time.

     

    The second one is like you are mixing direction pins on a H Bridge - HIGH and LOW for one direction LOW and HIG for the reverse - with PWM speeds in the same pins.

     

    I am going to address the "syntax" mix. I am sure that code you posted can't compile on the arduino IDE.

     

    The pseudo-code you posted - that is not right - can be writen in C as:

     

    int motorL[] = {6, 7};
    int motorR[] = {8, 9};
    int ledPin = 13;
    
    
    void setup() {
      Serial.begin(9600);
      int i;
      for(i =0; i > 2; i++ ){
        pinMode(motorL[i], OUTPUT);
        pinMode(motorR[i], OUTPUT);
      }
      pinMode(ledPin, OUTPUT);
    
    }
    
    
    void loop() {
      if( y > 0 ){
        digitalWrite(motorL[0], HIGH);
        digitalWrite(motorL[1], LOW);
        digitalWrite(motorR[0], HIGH);
        digitalWrite(motorR[1], LOW);
        analogWrite(9, motor_a);
        analogWrite(6, motor_b);
      }else if(  y < 0 ){
        digitalWrite(motorL[0], LOW);
        digitalWrite(motorL[1], HIGH);
        digitalWrite(motorR[0], LOW);
        digitalWrite(motorR[1], HIGH);
        analogWrite(9, motor_a);
        analogWrite(6, motor_b);
      }
    }

     

     

    The main problem here is "y" is not declared nor defined so that's an error.

     

    If you want to try PWM speed in the Arduino, connect one pin on the motor to ground the other to 6 and use that sketch:

     

    void setup(){
      pinMode(6, OUTPUT);
    }
    void loop(){
      int i;
    
      for(i =0; i<255;i++){
        analogWrite(6,i);
        delay(500);
      }
    }

     

    Now going a bit further. The code you posted seem taken from an H-Bridge type DC motor controller like this one L298 2A 6V-50V Compact Dual Motor Driver Kit - RobotShop the Arduino motor shield is based in the same chip ST L298 Arduino - ArduinoMotorShieldR3

     

    Those shield - and pretty much any H-Bridge - use to have 2 pins for direction and one for speed called the "enable" pin.

     

    The direction pins for one motor use this truth table:

     

    DirectionPIN 1PIN 2
    ForwardHIGHLOW
    BackwardLOWHIGH
    FreeLOWLOW
    Brake/StallHIGHIGH

     

    For the speed you use PWM on the enable PIN.

     

    So with this in mind, the code you posted makes more sense.

     

    The motor_R and motor_L pins are the direction pins for each motor and you would need another two pins for PWM.

     

    The pseudo code for this kind of controller - without any comunication just the movement function - can be:

     

    int direction_pin_motorL[] = {10, 11};
    int direction_pin_motorR[] = {8, 9};
    int speed_pin_motorL = 5;
    int speed_pin_motorL = 6;
    
    void setup(){
      int i;
      for(i =0; i > 2; i++ ){
         pinMode(motorL[i], OUTPUT);
         pinMode(motorR[i], OUTPUT);
      }
      pinMode(5, OUTPUT);
      pinMode(6, OUTPUT);
    }
    
    void loop(){
      // PUT HERE YOUR CONTROL CODE
    }
    
    void motor_L_speed(int speed){ // from 0 (stop) to 255
      analogWrite(speed_pin_motorL, speed); // PWM
    }
    
    void motor_R_speed(int speed){ // from 0 (stop) to 255
      analogWrite(speed_pin_motorR, speed); // PWM
    }
     void motor_L_forward(){
      digitalWrite(direction_pin_motorL[1], HIGH);
      digitalWrite(direction_pin_motorL[0], LOW);  
     }
    
      void motor_R_forward(){
      digitalWrite(direction_pin_motorR[1], HIGH);
      digitalWrite(direction_pin_motorR[0], LOW);  
     }
    
      void motor_L_backward(){
      digitalWrite(direction_pin_motorL[1], LOW);
      digitalWrite(direction_pin_motorL[0], HIGH);  
     }
    
      void motor_R_backward(){
      digitalWrite(direction_pin_motorR[1], LOW);
      digitalWrite(direction_pin_motorR[0], HIGH);  
     }
    
    
    void drive_forward(){ // both motors forward
      motor_L_forward();
      motor_R_forward();
     }
    
     void drive_backwards(){ // both motors forward
      motor_L_backward();
      motor_R_backward();
     }

     

    Not sure if this is what you was looking for.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube