Code needed , for motor and reed switch

HI guys ,

basically i need to create mechanism where when a button is pressed it runs the motor , and when a certain point of the motor hits a reed switch it cuts off the motor? I am new to arduino and any assistance in writing a code will be helpful :slight_smile:

Kieran

What type of motor (DC brushed, Brushless DC, servo, stepper)? Will the motor run in only one direction or must it be able to run in forward and reverse? Will the motor speed be variable or just on/off? What type of motor driver?

Details are important and must be known before code can be written.

I am new to arduino and any assistance in writing a code will be helpful :slight_smile:

The IDE comes with a ton of examples with code to get you started, forum searches can point you to solutions, the Playground on this site is a source of information, the Reference in the Learning tab explains much of the Arduino C++ language, Google can find tutorials on most parts of your project.

Read the "how to use the forum" stickies. There is information on how to ask an effective question (what we need to know to be of help). And when you have some code to post, the stickies tell how to format and post code properly.

Just a standard DC motor , and it only has to run in one direction. The functionality is that the motor goes round and stops when the reed sensor detects the magnet that is on it

1st
Press button once
//Go round once and stop when reed sensor detects the magnet

2nd

Push button twice
// Go round twice , ignores first magnet detection but stops on second magnet detection

It seems that you want someone to write the code for you. That is not how it works here. You do some research, write some code. If the code does not work, you post the code. Tell us what the code is supposed to do and what it actually does. Then we help. I have spent hours helping people that try, I won't lift a finger for those who don't try.

What is a standard DC motor? Which standard? You will need a motor driver as no Arduino can directly drive a motor. In order to pick a driver the motor rated voltage and stall current must be known. For single direction rotation a MOSFET, solid state relay or mechanical relay can be used as a driver. Google "Arduino DC motor" to see how to control a motor.

oh no no , i wasnt expecting you to write the code , its just im interested in the arduino software and im trying out reed sensors and collaborating them with motors , is there any tips you can give me to implement it in the code ? i have been looking at tutorials but i cant seem to get myself started.

  1. Can you write a basic sketch that just reads the push button switch and outputs its state to the serial monitor?

  2. Can you write a basic sketch that just reads the reed switch and outputs its state to the serial monitor?

  3. Can you write a basic sketch that turns the motor on and delays a few seconds and turns the motor off?

Start small. In the IDE digital examples (File, Examples, Digital) are several examples about reading switches. Use the examples to do #1 and #2 above. If you have trouble we are here to help.
You need to cover the basic parts individually and when you get each working and you under stand how each works then it is easy to put it all together.

/*1st Program
*Motor controlled with a button and a reed switch.
*Objective is the motor is pressed via the button therefore motor starts moving
*when magnet on motor hits read switch it stops the motor 180 degree turn(2 magnets on the motor opposite side)
*2nd program
*button is pressed twice

  • 1st magnet passes the reed switch without stopping on the second and stops on the first magnet with a 360 degree turn
    */
    //pin assignments
    const int buttonPin = A1; //Push button
    const int switchPin = A2; //reedswitch
    const int motorPin = 2; //dc motor

//variables

int buttonPinVal = 0;
int switchPinVal = 0;
int motorPinVal = 0;

void setup() {
// push button
pinMode(buttonPin, INPUT); // button pin set as input
digitalWrite(buttonPin, LOW) // button has to be pressed to start

// switchPin
pinMode(switchPin,INPUT); // reed sensor set as input
digitalWrite(switchPin,HIGH); // when motor moves reed sensor state turns to low

// motorPin
pinMode(motorPin,OUTPUT); // DC motor set to an output
digitalWrite(switchPin,LOW); //When button is pressed then motor will run

Serial.begin(9600);
}

This what i have done so far

Did you miss the part in the sticky posts that asks for code to be posted in code tags?

pinMode(buttonPin, INPUT);  // button pin set as input
  digitalWrite(buttonPin, LOW) // button has to be pressed to start

You cannot set the state of an input like that. All writing low to an input does is turn off the internal pullup resistor. I know some of the examples have the switch wired to Vcc with a pull down resistor. This is done so that the switch is HIGH when pressed so makes it easier for new users to grasp. The accepted method in the real world is for the switch to be wired from ground to an input and the internal resistor enabled with pinMode(pin, INPUT_PULLUP). So the input will be LOW when the switch closed (pressed).

Your code modified with comments

// THESE COULD BE byte VARIABLES TO SAVE 3 BYTES
const int  buttonPin = A1;                   //Push button
const int  switchPin = A2;                  //reedswitch
const int  motorPin = 2;                     //dc motor

  //variables
// THESE VARIABLES COULD BE byte, TOO. Or boolean for the switches states (low/high)
int buttonPinVal = 0;
int switchPinVal = 0;
int motorPinVal = 0;

void setup()
{
  // push button
  pinMode(buttonPin, INPUT_PULLUP);  // button pin set as input

  // switchPin
  pinMode(switchPin,INPUT_PULLUP); // reed sensor set as input

  // motorPin
  pinMode(motorPin,OUTPUT); // DC motor set to an output
// YOU MIGHT WANT TO SET THE OUTPUT HIGH OR LOW TO MAKE SURE THE MOTOR IS OFF TO START
  Serial.begin(9600);
}

// THESE COULD BE byte VARIABLES TO SAVE 3 BYTES
const int buttonPin = A1; //Push button
const int switchPin = A2; //reedswitch
const int motorPin = 2; //dc motor

//variables
// THESE VARIABLES COULD BE byte, TOO. Or boolean for the switches states (low/high)
int buttonPinVal = 0;
int switchPinVal = 0;
int motorPinVal = 0;

void setup()
{
// push button
pinMode(buttonPin, INPUT_PULLUP); // button pin set as input

// switchPin
pinMode(switchPin, INPUT_PULLUP); // reed sensor set as input

// motorPin
pinMode(motorPin, OUTPUT); // DC motor set to an output
// YOU MIGHT WANT TO SET THE OUTPUT HIGH OR LOW TO MAKE SURE THE MOTOR IS OFF TO START
Serial.begin(9600);
}
void loop() {
buttonPinVal = digitalRead(buttonPin);
if (buttonPinVal == HIGH) {
// if the button is pushed
digitalWrite(motorPinVal, HIGH);
} else {
digitalWrite(motorPinVal, LOW);
}
{
switchPinVal = digitalRead(switchPin); //senses what the REED switch is doing
//check if the input is HIGH (switch on)
if (switchPinVal == HIGH) { //if button is pressed
digitalWrite(motorPinVal, LOW); //turns motor off
} else {
//Motor is running until stopped by reed sensor
digitalWrite(motorPinVal, HIGH);
} } }

This is what i have done so far , the next thing is
Push button twice
// Go round twice , ignores first magnet detection // first reed sensor detection and detects the next reed sensor / magnet

DC MOTOR 3V 100 RPM , WITH MOSFET

Hi,
Welcome to the Forum

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html
then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

This is what i have done so far , the next thing is
Push button twice
// Go round twice , ignores first magnet detection // first reed sensor detection and detects the next reed sensor / magnet

NO NO NO, what you need to do is to get that first bit of your code to work, before trying the next stage.
Will your 100rpm be able to stop at the hall effect device or run through due to inertia.
You may need to use PWM and analogWrite to slow the motor.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

// THESE COULD BE byte VARIABLES TO SAVE 3 BYTES
const int  buttonPin = 2;                   //Push button
const int  switchPin = 4;                  //reedswitch
const int  motorPin = 3;                     //dc motor

//variables
// THESE VARIABLES COULD BE byte, TOO. Or boolean for the switches states (low/high)
int buttonPinVal = 0;
int switchPinVal = 0;
int motorPinVal = 0;
int buttonState = 0;         // current state of the button
int LastButtonState = 0;     // previous state of the button
int buttonPushCounter = 0;   // counter for the number of button presses
void setup()
{
  // push button
  pinMode(buttonPin, INPUT_PULLUP);  // button pin set as input

  // switchPin
  pinMode(switchPin, INPUT_PULLUP); // reed sensor set as input

  // motorPin
  pinMode(motorPin, OUTPUT); // DC motor set to an output
  // YOU MIGHT WANT TO SET THE OUTPUT HIGH OR LOW TO MAKE SURE THE MOTOR IS OFF TO START
  Serial.begin(9600);
}
void loop() {
  buttonPinVal = digitalRead(buttonPin);
  if (buttonPinVal == LOW) {
    // if the button is pushed
    digitalWrite(motorPinVal, HIGH);
  } else {
    digitalWrite(motorPinVal, LOW);
  }
  delay(50); //Avoid bouncing
  {
    switchPinVal = digitalRead(switchPin); //senses what the REED switch is doing
    //check if the input is HIGH (switch on)
    if (switchPinVal == LOW) { //if button is pressed
      digitalWrite(motorPinVal, LOW); //turns motor off
    } else {
      //Motor is running until stopped by reed sensor
      digitalWrite(motorPinVal, HIGH);
      delay(50); //Avoid bouncing
    }    }    }

Here is the code i have got so far
also here is the schematic i have drawn up
it does not seem to be working and i do not know where i have gone wrong , help would be much appreciated :slight_smile:

Here is an idea of what i want to happen with the mechanism , hopefully this helps :slight_smile:

it does not seem to be working

Do your realize how insufficient this statement is in order for people to help you?

The sketch does something. What does it do? Is the motor not running? Is the motor not stopping? What does the sketch not do that you want it to do? Have you ever gotten the motor to run in a simple sketch without trying to make it stop with the reed switch?

The sketch verifies but when i upload onto the arduino board it does not work. The wiring on the circuit is in the right places and i have tested the motors using the test motors code and it works , but the code which i have uploaded does not seem to work , i really cannot pinpoint where the problem in the code is

this is the test code i used to test the motors

int motorPin = 3;
const int  buttonPin = 2;
boolean buttonState = LOW; //Push button
const int  buttonPin = 4;                 
boolean buttonState = LOW;
int motorEnabled = 0;
boolean previousButtonState = LOW;
void setup() {
 pinMode(buttonPin, INPUT_PULLUP);
 pinMode(buttonPin, INPUT_PULLUP);
 pinMode(motorPin, OUTPUT);
}
void loop() {
 buttonState = digitalRead(buttonPin);
 
if(previousButtonState != buttonState && buttonState == LOW){
    motorEnabled = !motorEnabled;
    }
  if(motorEnabled == 1){
    digitalWrite (motorPin,HIGH);
    }
  else{
    digitalWrite (motorPin,LOW);
    }
  previousButtonState = buttonState;

  
}

You have many lines like this

digitalWrite(motorPinVal, HIGH);
  
    digitalWrite(motorPinVal, LOW)

You need to be writing to the motorPin (digital pin 3) not the motorPinVal which is set to 0. You never appear to use motorPinVal in any manner which makes sense. What is it in the program for?

Do you understand that your motor will only run as long as the switch is held down?

You may want to study up on the state change example in the 02 Digital examples of the IDE to make the motor run when the moment the switch becomes pressed (not is pressed) and the motor stop when the reed switch becomes closed.

Hi,
I think

 pinMode(buttonPin, INPUT_PULLUP);
 pinMode(buttonPin, INPUT_PULLUP);

Should be;

 pinMode(buttonPin, INPUT);
 pinMode(buttonPin, INPUT_PULLUP);

Try that.

Tom... :slight_smile:

digitalWrite(motorPinVal, HIGH);
  
    digitalWrite(motorPinVal, LOW)

you were right about this , as soon as i deleted the Val the motor started to run which is a great sign , but the reed sensor still does not pick up the magnet, the motor continues to progress , the motor is running as soon as the code is uploaded, it is not supposed to move until user pushes the button

the reed sensor still does not pick up the magnet, the motor continues to progress , the motor is running as soon as the code is uploaded, it is not supposed to move until user pushes the button

It's possible that the button and reed switch are not actually wired correctly as your diagram indicates. It's also possible that the reed switch is normally closed and opens when the magnet is present.

You need to do some hardware debugging to see if button presses and magnet presence leads to the HIGH or LOW values your logic expects.

The code you used to test the motor looks correct for picking up the state change when a button is pressed.

Hi,
Have you done what has been suggested in post #16?

Tom... :slight_smile:
PS. Do you have a DMM?