Help with Sensor and Motor

Hi,I am having problem with the motor and pressure sensor
I want that when I press the sensor, according to the pressure, he turns the engine, but it only works occasionally,sometimes yes and sometimes no

I'm using this code :

int motorPin = 3;
 int Senval=0;
int Senpin=A0;
void setup()
{
  Serial.begin(9600);
      
pinMode(13,OUTPUT);
pinMode(2,OUTPUT);
  pinMode(motorPin, OUTPUT);
}
 
void loop()
{
  
  Senval=analogRead(Senpin);
  
      Serial.println(Senval); 
    
      analogWrite(motorPin,Senval);
    
 //analogWrite(motorPin,500);
        
  delay(100);

}

I'm able to read the value of the applied pressure, which ranges from 0 to 500
if i put digitalWrite(motorpin,HIGH); the motor work.
why sometimes the motor work,and sometimes the motor dont work.
Sorry for my bad english.
Thanks for the attention

analogueWrite should be between 1 - 255
So try the following

int motorPin=3;
int Senval=0;
int Senpin=A0;
void setup()
{
  Serial.begin(9600);
      
pinMode(13,OUTPUT);
pinMode(2,OUTPUT);
  pinMode(motorPin, OUTPUT);
}
 
void loop()
{
  
  Senval=analogRead(Senpin);
  
      Serial.print(Senval,DEC); 
      Senval=constrain(Senval,1,500);
      Senval=map(Senval,1,500,1,255);
      Serial.print (" BUT NOW CHANGED TO ");
      Serial.println(Senval,DEC);   
      analogWrite(motorPin,Senval);      
      delay(100);

}

KenF:
analogueWrite should be between 1 - 255
So try the following

int motorPin=3;

int Senval=0;
int Senpin=A0;
void setup()
{
  Serial.begin(9600);
     
pinMode(13,OUTPUT);
pinMode(2,OUTPUT);
  pinMode(motorPin, OUTPUT);
}

void loop()
{
 
  Senval=analogRead(Senpin);
 
      Serial.print(Senval,DEC);
      constrain(Senval,1,500);
      Senval=map(Senval,1,500,1,255);
      Serial.print (" BUT NOW CHANGED TO ");
      Serial.println(Senval,DEC);   
      analogWrite(motorPin,Senval);     
      delay(100);

}

dont work,but if i put analogWrite(motorPin,500);.the motor turn on.

Senval=constrain(Senval,1,500);
      Senval=map(Senval,1,500,1,255);

Why not just

Senval = map(Senval, 1, 1023, 1, 255);

Or, even simpler
Senval = Senval / 4; // 255 = 1023 / 4 for all practical purposes

Or
Senval = Senval >> 2; // same as / 4

...R

Robin2:

Senval=constrain(Senval,1,500);

Senval=map(Senval,1,500,1,255);




Why not just


Senval = map(Senval, 1, 1023, 1, 255);

Because he only mentioned that his values get up to 500. therefore this would only give him half speed.

Robin2:
Or, even simpler
Senval = Senval / 4; // 255 = 1023 / 4 for all practical purposes

For the same reason as just mentioned

Robin2:
Or
Senval = Senval >> 2; // same as / 4

and again.

In fact I was assuming that his sensor values are only getting up to 500. (but wasn't quite sure I believed him so added the constraint as an after thought)

KenF:
In fact I was assuming that his sensor values are only getting up to 500. (but wasn't quite sure I believed him so added the constraint as an after thought)

OK, I hadn't spotted the 500 limit.

Just divide by 2 instead of 4.

...R

Robin2:
Just divide by 2 instead of 4.

Now THIS could work :slight_smile:

Dont work =s.
1 video :

But later i made this :

int motorPin=3;
int Senval=0;
int Senpin=A0;
void setup()
{
  Serial.begin(9600);
      
pinMode(13,OUTPUT);
pinMode(2,OUTPUT);
  pinMode(motorPin, OUTPUT);
}
 
void loop()
{
  
  Senval=analogRead(Senpin);
  
     


      Serial.println(Senval);
      
     if(Senval > 0 && Senval< 50)
     {
      analogWrite(motorPin,3000);  
     }else{
       analogWrite(motorPin,0);
     }
     if(Senval > 50 && Senval < 100)
     {
     analogWrite(motorPin,2000);
     }
       if(Senval > 100 && Senval < 150)
     {
     analogWrite(motorPin,1500);
     }
       if(Senval > 150 && Senval < 250)
     {
     analogWrite(motorPin,1000);
     }
       
       if(Senval > 250 )
     {
     digitalWrite(motorPin,HIGH);
     }
     
      delay(100);

}

But the motor dont varies according to the pressure gradually

Could you post a schematic of exactly how you have the motor connected.

the circuit for the sensor :

The motor connected to pin 3 and ground

saidmrn:
The motor connected to pin 3 and ground

Never connect a motor to one of the I/O pins - it will probably damage your Arduino by drawing far too much current. It may already have damaged it. The I/O pins can only provide a max of 40mA and I suggest 20mA would be a sensible upper limit. Even small electric motors need 10 or 20 times that amount.

At the very least you need a transistor between the Arduino and the motor. Or get a h-bridge such as an L298 f you want forward and reverse control.

...R

saidmrn:
The motor connected to pin 3 and ground

This is what I was afraid of. Robin has said it all. Ideally you should have a separate power supply for the motor and at least a transistor to drive it. Even then you’d want a current limiting resistor to the base of the transistor, and possibly a diode to prevent back EMF causing damage as you release the power.

I have transistor,diode,resistor,Do you can send me 1 circuit to mount it?

saidmrn:
I have transistor,diode,resistor,Do you can send me 1 circuit to mount it?

http://www.ecs.umass.edu/ece/m5/tutorials/tip122_transistor_tutorial.html

i maked this circuit

the motor get connected and the speed does not change when I put the value in serialprint

int motorPin = 3;
 
void setup() 
{ 
  pinMode(motorPin, OUTPUT);
  Serial.begin(9600);
  while (! Serial);
  Serial.println("Speed 0 to 255");
} 
 
 
void loop() 
{ 
  if (Serial.available())
  {
    int speed = Serial.parseInt();
    if (speed >= 0 && speed <= 255)
    {
      analogWrite(motorPin, speed);
    }
  }
}

I'm not sure whether your transistor circuit is correct - I'm not good at that stuff.

But a couple of other things....

There is a real possibility that Pin 3 on your Uno is damaged already. Try using a different pin.

You really should NOT power the motor from the 5v pin. If it draws a lot of current (which it probably will) the voltage will drop and the Arduino will reset or otherwise misbehave. Give the motor a separate power supply. Just make sure GND for the motor power supply is connected to the Arduino GND - which probably means that your transistor circuit needs to be changed so it is controlling the positive side.

...R