Monday, 31 August 2020

Motors, Servos, Capacitive Touch

Servo: 

1x Arduino Uno 
3x Jumpers
1x Servo Motor
How to Connect Them

Code:


#include <Servo.h> 


int servoPin = 3; 

Servo Servo1; 

void setup() { 

   Servo1.attach(servoPin); 

}

void loop(){ 

   Servo1.write(0); 

   delay(1000); 

   Servo1.write(90); 

   delay(1000); 

   Servo1.write(180); 

   delay(1000); 

}


MPR121 12-Key Capacitive Touch Sensor:

1x Arduino 
2x Jumper 
1x Touch Sensor
Touch aszd5vxmjb
Code: 
void setup() {
 pinMode(2, INPUT);
 Serial.begin(9600);
}
void loop() {
 if (digitalRead(2) == HIGH)Serial.println("Sensor is touched");
 delay(500);
}
Stepper Motor:

1x Arduino Uno board

1x Stepper Motor

1x Stepper Driver

2x Resistor

9x Jumpers


Code:

#include <Stepper.h> //including stepper motor library

int stepIN1Pin = 8;         

int stepIN2Pin = 9;

int stepIN3Pin = 10;

int stepIN4Pin = 11;

int stepsPerRevolution = 2048; // amount of steps per revolution


const int button1Pin = 2;  // pushbutton 1 pin for clockwise rotation

const int button2Pin = 3;  // pushbutton 2 pin for counter clockwise rotation


Stepper myStepper(stepsPerRevolution, stepIN1Pin, stepIN3Pin, stepIN2Pin, stepIN4Pin);


void setup() {


  pinMode(button1Pin, INPUT);

  pinMode(button2Pin, INPUT);

myStepper.setSpeed(15);

}


void loop() {

   

int button1State, button2State;

  button1State = digitalRead(button1Pin);

  button2State = digitalRead(button2Pin);

 if (((button1State == LOW) && !(button2State == LOW)))  // if we're pushing button 1 OR button 2

  myStepper.step(stepsPerRevolution/8);

  if (((button2State == LOW) && !(button1State == LOW)))  // if we're pushing button 1 OR button 2

  myStepper.step(-stepsPerRevolution/8);

  }


No comments:

Post a Comment