Go Down

Topic: [solved]DC motor Arduino-Processing interface (simple code not working,help) (Read 746 times) previous topic - next topic

kaning

Hi,
i want to control the speed of dc motor in arduino with a processing sketch.
i already tried every i can think of but something is still missing and i ask your help :) must be something pretty simple..maybe im not using the right aproach but i think like this its possible to work.
both codes are here below with some coments,hope someone can detect where the problem is..thank you very much!!

arduino code: (where i think problem is)
notes:
-when in processing value is 0 it doesnt move and with multimeter reading is 0 (so serial comunication is working)
-than with any value i send in processing i only can measure 0.50v in pin 9 and ear noise from motor if i force spin it starts and goes up to 1.8 v but no more.

so aparently serial comunication is working but basically i cant understand why im not being able to get analogWrite from 0 to 5v.. :(

Code: [Select]
int leitura;
int vel;
const int motor=9;

void setup()
{
 Serial.begin(9600);
 pinMode(motor,OUTPUT);  
}

void loop()
{
 if(Serial.available())
 {
   leitura=Serial.read();
   vel=map(leitura,0,255,0,1023);
   if(leitura>0)
   {
     analogWrite(motor,vel);
     delay(1);
   }
   else
   {
     analogWrite(motor,LOW);
   }
 }
}


processing code:
notes:
-code is a mess!! im just trying every step to have a fully rc car interface with processing,next step is puting everything working in the same code.
-just pay atention to motor part please :)
Code: [Select]
import processing.serial.*;
Serial myPort;
int a=0;
int b=1;
int l1=240;
int l2=250;

int x=105;
int x1;

int y=355;
int y1;
int vel;

void setup()
{
 size(400, 400);
 println(Serial.list());
 myPort = new Serial(this, Serial.list()[0], 9600);
}

void draw()
{
 //led test------------------------
/*
a=a-b;
  a=constrain(a,0,1);
  if (a==1)
  {
  rectMode(CORNER);
  background(255);
  fill(50);
  rect(50, 50, 100, 100);
  myPort.write(l1);
  println(l1);
  }
  else if (a==0)
  {
  background(255);
  fill(125);
  rect(50, 50, 100, 100);
  myPort.write(l2);
  println(l2);
  }
 
  //servo---------------------
 
  rectMode(CORNER);
  stroke(0);
  fill(0);
  rect(100,190,190,20);
  stroke(0);
  fill(255);
  rect(x,190,10,20);
  if(mouseX>97 && mouseX<292 && mouseY > 189 && mouseY< 211 && mousePressed)
  {
  x=mouseX;
  x=constrain(x,100,280);
  x1=x-100;
  println(x1);
  myPort.write(x1);
  }
  */
 //motor--------------------------

 rectMode(CORNER);
 stroke(0);
 fill(0);
 rect(300, 100, 20, 265);
 stroke(0);
 fill(255);
 rect(300, y, 20, 10);
 if (mouseX>299 && mouseX<321 && mouseY > 97 && mouseY< 368 && mousePressed)
 {
   y=mouseY;
   y=constrain(y, 100, 355);
   y1=abs(y-355);
 }
 println(y1);
 myPort.write(y1);  
}
/*
 void mousePressed()
 {
   if (mouseX>49 && mouseX<151 && mouseY>49 && mouseY<151)
   {
     b=b*-1;
   }
 }
*/

both y and y1 are int variables. below you can see how the sketch works
http://imgur.com/CH64hmm

thank you!!cumps

Grumpy_Mike

Quote
i only can measure 0.50v in pin 9 and ear noise from motor if i force spin it starts and goes up to 1.8 v but no more.

How have you got things wired up? I am assuming you have a transistor driving the motor but is the motor in the emitter of the collector. It needs to be in the collector with a base resistor.
How are you measuring things? A meter will not do a good job of displaying the PWM value. You do not get a variable DC voltage out put a pulse train.
http://www.thebox.myzen.co.uk/Tutorial/PWM.html

kaning

yes, im using mosfet just to back voltage dont damage anything else (like in arduino starter kit project09 motorized pinwheel). schematic is exactly like in picture below with exception that mosfet gate is conected to 5v (it means other two pins are always conected) and red wire of dc motor is conected to pin9 (PWM). I dont have anything else to measure..anyway i think meter is enough at least to check if voltage is changing.at least to measure 0 or 5v :p

http://imgur.com/ITtKfVa

Grumpy_Mike

It sounds like from your description you have it wired up wrong, the motor should not be wired to the PWM pin. It should be wired EXACTLY as that photograph.

kaning

if i do that when i move the processing slide bar and (Serial.available>0) the motor will get exactly and only 5v and i want to control speed. :/ anyway i can try, its worth the shot..cant find any possible change in code? ty a lot

"connect digital pin 9 to the left pin on the transistor.this pin is called gate.a change in voltage in the gate makes a connection between the other two pins. connect one end of the motor to the middle pin of the transistor, this pin is called the drain. when arduino activates transistor by suplying voltage to the gate this pin will be connected to the third pin,called the source.connect the source to the ground."

i think the first pin(gate) only works as a switch..

jack wp

What motor are you using (URL to web page please)? Is it rated at 4Volts, or 12Volts, or what.
You may have some
Quote
code not working
problem, but you do have some hardware/wiring problem.
Good luck, Jack

kaning

i solved the problem grumpy mike was right. the quote i gave in my last reply confused me a bit. so it seems the gate pin in mosfet works like enable pin of H bridge and you must pwm from there. you open the door wide open (5v) or dont (0v) or just a litle and like that you are controling the speed, i think. thanks a lot guys

Go Up