Input and Output¶
Input¶
I take HW-495 witch is from analog hall family. It is a type of sensor that uses the Hall effect to measure magnetic fields. In sample word it is a magnetic field sensor.
HW-495¶
Connection¶
- The plus pin connect to 5V pin in Arduino
- The negative pin connect to GND pin in Arduino
- The S pin connect to A0 pin in Arduino
Ignore all this it is for the project just focus in HW-495 and Arduino Nano.
and for test the sensor I use magnetic.
How much they can handle voltage.¶
For Arduino NANO and HW-495 they can handle up to 5V.
The Code¶
// Define the analog input pin connected to the HW-495 sensor
const int hallSensorPin = A0;
void setup() {
// Initialize serial communication at 9600 bps
Serial.begin(9600);
}
void loop() {
// Read the analog voltage from the HW-495 sensor
int sensorValue = analogRead(hallSensorPin);
// Print the sensor value to the Serial Monitor
Serial.print("Hall Sensor Value: ");
Serial.println(sensorValue);
// Delay for a short period
delay(500);
}
The Result¶
If there is no magnetic fields around the sensor the output in serial monitor will be around 519 like in next images.
If there is magnetic fields around the sensor the output in serial monitor will be more or less then 519 by allot like in next images.
In this video you can see if the magnetic is close to the sensor the output in serial monitor will change from 519 to around 165.
Output¶
For output I take servo motor and I use the code from Arduino IDE Example and change it to control the speed of the motor rotation
Servo Motor¶
A servo motor is a type of rotary actuator that is widely used in various applications, particularly in robotics and automation. It is designed to provide precise control over angular position, velocity, and acceleration.
The Connection¶
From servo motor to Arduino NANO
- The brown wire to GND
- The red wire to 5V
- The orange wire to D9
How much they can handle voltage.¶
For Arduino NANO can handle up to 5V. For The servo motor can handle up to 5V.
The Code¶
You can fide the code in Example then Servo then Sweep.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
}
How to Change the Speed of Servo Motor¶
You can change the code to change the speed. There is tow Methods to change the speed.
First Method¶
You can just change the delay in original example code. For example change it to 1. When you change the delay it is meaning the motor will change every 1 ms 1 degree rotation like in next code. and in original code will be every 15 ms 1 degree rotation.
The code you need just to change the delay like this.
(delay(1);)
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(1); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(1); // waits 15 ms for the servo to reach the position
}
}
Second Method¶
In second method you can change the degrees of the step like in original code the step of degree is 1. By make it 10 the motor will move 1 degrees every step.
For example in this code I change the degree steps of the motor from 1 to 10.
(pos += 10)
void loop() {
for (pos = 0; pos <= 180; pos += 10) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 10) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
}
In this code I change the degree steps of the motor from 1 to 5.
void loop() {
for (pos = 0; pos <= 180; pos += 5) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 5) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
}
You can make the motor go fast in one direction on slow in another direction.
Like in this code I use 10 degrees for steps and for opposite direction I use 1 degree for steps.
void loop() {
for (pos = 0; pos <= 180; pos += 10) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
}
And in this code I use 10 degrees for steps and for opposite direction I use 5 degree for steps.
void loop() {
for (pos = 0; pos <= 180; pos += 10) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 5) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
}
Input and Output¶
For input I use variable resister and for output I use Stepper motor. The Stepper motor connected to motor driver (L298N) and the motor driver connected to Arduino NANO.
The Connection¶
From Stepper Motor to Drive Motor (L298N)¶
You can check the wire that copalite the circuit to make coil by three methods. One of the by multimeter choose in multimeter the option that check the Connection.
The second Method is by connect two of the wires and move the motor if it easy to move it change on of the wire and if it is hard to move it the this two wire copalite one of the coil. The third method is by searching in internet about the motor model Connection.
For MERCURY MOTOR model (SM-42BYG011-25 1.8’).
- OUT1 to Red Wire
- OUT2 to Green Wire
- OUT3 to Bulow Wire
- OUT4 to Yellow Wire
From Drive Motor (L298N) to Arduino NANO¶
- GND to GND
- +12V to 5V
- IN1 to D4
- IN2 to D5
- IN3 to D6
- IN4 to D7
Variable Resister to Arduino NANO¶
Make sure you face the variable resister and you can see the value of the resister in my case it is B2k so it is 2K ohm. 1. The left pin to GND 1. The middle pin to A0 1. The right pin to 5V
How much they can handle voltage.¶
For Arduino NANO can handle up to 5V. For The Stepper motor and motor driver can handle up to 12V.
The Code¶
The code is about how to make the Stepper motor in two direction by changing the variable resister.
#include <Stepper.h>
const int stepsPerRevolution = 200;
const int motorSpeed = 60; // Motor speed in RPM
const int duration = 5; // Duration in seconds
int potPin = A1; // potentiometer is connected to analog 0 pin
int potValue; // variable used to store the value coming from the sensor
int percent; // variable used to store the percentage value
Stepper myStepper(stepsPerRevolution, 4, 5, 6, 7);
int stepCount = 0; // number of steps the motor has taken
void setup() {
// initialize the serial port:
myStepper.setSpeed(motorSpeed);
Serial.begin(9600);
}
void loop() {
unsigned long totalSteps = (motorSpeed * stepsPerRevolution) * duration / 60;
potValue = analogRead(potPin); // read the value from the potentiometer and assign the name potValue
percent = map(potValue, 0, 1023, 0, 100); // convert potentiometer reading to a percentage
Serial.print(percent);
if(percent > 80 && percent <=100){
unsigned long startTime = millis();
while (millis() - startTime < 100) {
myStepper.step(-stepsPerRevolution);
}
}
if(percent > 40 && percent <= 70){
unsigned long startTime = millis();
while (millis() - startTime < 100) {
myStepper.step(1);
}
// Stop the motor and wait for a few seconds before repeating the process
myStepper.step(0);
}
Serial.print(percent);
}