this is not my first time that work with servo motor on arduino mega adk, but this time something went wrong! I'm connecting my servo Motor to an arduino as it should be. and then I wrote my code as mentioned here:
#include <Servo.h>
Servo monServo;
void setup()
{
monServo.attach(7, 1000, 2000);
monServo.write(45);
}
void loop()
{
}
but my servo doesn't work correctly, it doesn't turn 45° but it turn into it's max value and then begin to make noise as it wants to turn more. I thought that the problem is from the servo motor but I tried this with 3 different motors but the same result. From where this issue is comming from?
You could have an issue with the power supply. Try powering the Arduino with a wall power supply instead of using the USB port. You can also add a large capacitor in series with the servo. See "If the servo misbehaves"
Try to remove the additional two parameters in the attach method: substitute monServo.attach(7, 1000, 2000); with monServo.attach(7);
Related
I am fiddling with an Arduino Uno and trying to get two switches to operate two servo motors. I am not able to do this right now, and I am not sure what my issue is. I know that my servos are wired correctly; however, I highly doubt that my switches are correctly wired.
Here is a diagram of my circuit and the code that I am using.
#include <Servo.h>
Servo servo_11;
Servo servo_10;
void setup()
{
servo_11.attach(11);
servo_10.attach(10);
pinMode(A3, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A2, INPUT);
}
void loop()
{
servo_11.write(0);
servo_10.write(0);
digitalWrite(A3, LOW);
digitalWrite(A2, LOW);
if (analogRead(A2) != LOW) {
servo_11.write(180);
}
delay(10);
}
Circuit image
If anyone could point out what's wrong, I would really appreciate it!!
You have floating inputs. When the button isn't pressed the pin isn't connected to anything. So the pin can read whatever noise it happens to pick up from the environment. You need pull-down or pull-up resistors. If you use pull-ups, then you can use the ones built into the chip.
You also seem to be confusing analogRead and digitalRead. It's digitalRead that gets HIGH and LOW. analogRead gets values from 0 to 1023 for 0 to 5V.
pull-down resistor circuit
try looking on this to understand the concept
it makes sure that when you dont press the pin will read ground and only when it presses the button you make voltage divider that ups the volage to a non-zero to give you HIGH
As education exercise on sending digital signals, I'm trying to code the pulse train for a servo without using the servo.h library.
The servo is a 9g micro servo. Hardware is correct, as many examples using servo.h work fine.
I have the following code. The problem is that the servo jerks around for 3 seconds rather than moving and holding still.
void loop() {
movePulse_1000();
delay(3000);
}
void movePulse_1000(){
Serial.print("Start movePulse_1000()\t\t");
for (int pulseCounter=0; pulseCounter<=150; pulseCounter++){
digitalWrite(pinServo,LOW);
delay(20); // between pulses
digitalWrite(pinServo,HIGH);
delayMicroseconds(1000);
}
Serial.println("End movePulse_1000()");
}
Using an analog servo, the average pulse width must be 1.5ms apart, and the duty cycle changes based on the desired position. To keep the servo where you want it, you must constantly refresh the servo data. This isn't a super simple task and that servo library is quite optimized. There's few reason not to use it.
It creates hardware timers and uses them to refresh the servos. This allows your regular code to appear to continue regularly even though it's being interrupted by the servo library to service the servos. Duty cycle, pwm frequency, and refresh rates all come into play. You'll have to look at the data sheet of the servo you are using to get the full details. But its not as simple you think and those delay/delaymicros functions you're using aren't always precise enough. That's why you would use times and overflow interrupts. Though most servos aren't too picky and you can get away with a ton of slop.
The library servo.h constantly sends pulses which means servo growling = battery consumption.
I changed your function to only rotate the servo without a timer from 0 to 180 degrees.
del = (7 * x) +500; - for my servo-pulse 500 to 1260us (calculated, not measured)
void movePulse(int x){
int del=(7*x)+500;
for (int pulseCounter=0; pulseCounter<=50; pulseCounter++){
digitalWrite(pinServo,HIGH);
delayMicroseconds(del);
digitalWrite(pinServo,LOW);
delay(20); // between pulses
}
}
I have been working on a project with Arduino and have come across something that I find fascinating/confusing. So, I had to test something before constructing this project. I built a simple circuit that consists of just an LED and photoresistor. What I had to test was whether the photoresistor was capable of determining the brightness of an LED that was being dimmed through PWM. My initial expectation was that this would not work (the photoresistor would either read 1023 or 0 because PWM is achieved digitally). To my surprise, the photoresistor was able to accurately read the brightness of the LED (accurately to an extent -- this is simply based off of comparing the apparent brightness of the PWM LED with an LED placed in series with a certain resistor)! This is exactly what I wanted, but I am just curious as to why this works. I am not sure if my original doubt was due to a misunderstanding of photoresistors or PWM. Any help would be much appreciated. Thank you!
Here is the code I am running (I am not using the analogWrite() function because the project I am working on requires me to have a certain level of control over the PWM):
const int LED_PIN = 9;
const int PHOTO_PIN = 0;
//These values have been altered and tested
const int HIGH_TIME = 250;
const int LOW_TIME = 2750;
void setup()
{
pinMode(LED_PIN, OUTPUT);
pinMode(PHOTO_PIN, INPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(LED_PIN, HIGH);
delayMicroseconds(HIGH_TIME);
digitalWrite(LED_PIN, LOW);
delayMicroseconds(LOW_TIME);
Serial.println(analogRead(PHOTO_PIN));
}
A "photoresistor" is a variable resistor. That is the simplest way to say it.
Just imagine your potentiometer, you can control its resistance by turning the little knob and then analogRead it. The photoresistor on the other side, changes it resistance depending on the light intensity. Because of that, the resistance will go up and down depending on your LED.
For "HOW" it actually works, see here.
Now, there are a couple of factors to consider:
1 - The ambient light of your room.
2 - The distance between your LED
So hope I helped you learn a little more about photoresistors!
The response time of the photo resistor is much slower than the PWM frequencies you are using. So it averages the on and off times of the LED and gives a resistance proportional to the average light. If you were using a photodiode with a fast response time, it would be able to "see" the LED go on and off.
I suggest that you don't try to write to the Serial port every time through the loop since it will quickly fall behind at 9600 baud. Perhaps write every 500 times through the loop.
I am using an arduino uno and I am trying to control a motor with two inputs which I found in a small car I used to have as a child.
I connected the first pin of the motor to the arduino ground and the second one to the VCC and the motor started turning.
However, when I write the following code the motor doesn't work.
void setup() {
pinMode(8,OUTPUT);
digitalWrite(8,HIGH);
}
void loop() {
}
(I have connected the first pin of the motor to the ground and the second one to pin 8 of arduino).
Does anybody know why that happens?
You can only get a certain amount of current from an Arduino output pin. In general, you can light an LED with a direct connection to an output pin, but motors require more current. A detailed discussion is here.
To control a device such as a motor which needs more current than the output pin can provide directly, you can use an external transistor. You can buy circuits that implement this idea, such as this Motor Shield for Arduino.
This is not how Arduino is supposed to work with power consuming stuff (like mhopeng said, you may use LED in such a scheme, but not something more consuming): a motor should be between GND and 5V and if you want to control it, you have to use a transistor connected to an output pin.
I had a similar question once, it may be of help, too. Also, it may be a good idea to ask further questions at arduino.SE.
I am using the V2.3 motorsheild on the arduino uno r3.
I no almost nothing about ardunio excpet from what ive read about the past week.
I have been trying to simply turn the motor with the code below and it wont work. I have the arduino plugged into my computer and 12v going to the blue power box thing.
I am using this motor https://www.servocity.com/html/900_rpm_micro_gearmotorblocks.html#.VyELIFaDFBc
I cannot get the dang thing to spin, motor works fine if i wire it straight to the 5v/grnd but not when its in the M1 connection
wiring: https://i.groupme.com/747x1328.png.54a01e30433241d4a99905bd0e8ede2b let meknow if this link doesnt work
Heres an Imgur link for the wiring http://imgur.com/J92ewnu
#include
AF_DCMotor motor(1);
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Motor test!");
// turn on motor
motor.setSpeed(200);
motor.run(RELEASE);
}
void loop() {
uint8_t i;
Serial.print("tick");
motor.run(FORWARD);
for (i=0; i<255; i++) {
motor.setSpeed(i);
delay(10);
}
for (i=255; i!=0; i--) {
motor.setSpeed(i);
delay(10);
}
Serial.print("tock");
motor.run(BACKWARD);
for (i=0; i<255; i++) {
motor.setSpeed(i);
delay(10);
}
for (i=255; i!=0; i--) {
motor.setSpeed(i);
delay(10);
}
Serial.print("tech");
motor.run(RELEASE);
delay(1000);
}
I am not expert in current particular shield and its library. But what i see when first time look at your sketch and motor connection image is that you are trying to run dc motor at a certain speed with commands, which seems to be for servo or stepper motors.
You should know that running DC motor at some speed is possible if you have speed calculation based on its current and voltage or when a speed sensor is located at motor's shaft. This motor doesn't have the sensor (i know it for sure, because used same in my projects) and i think the shield can't calculate speed. What you can is only switch it on and off and maybe change its direction.
So, first of all you should look (and use) at the library for such functions.
Looking for what the function you called "AF_DCMotor" does I see how you were confused. There is also this product, which is another adafruit motor sheild.
But it's actually a very different motor driver. Both shields have a power stage which takes pulses and amplifies them to drive a motor. The V2 apparently has a chip which generates these pulses, and you send it serial data to command it using I2C. The V1 just gets these pulses directly from the Arduino.
For yours, a V2 motor shield, try the guide Adafruit provides.
Here's the link - Adafruit Motor Sheild V2 DC motor control
And here's the code
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
AFMS.begin();
myMotor->setSpeed(100);
myMotor->run(FORWARD);
It mentions the use of I2C on the introductory page
Instead of using a latch and the Arduino's PWM pins, we have a fully-dedicated PWM driver chip onboard. This chip handles all the motor and speed controls over I2C
That's why it includes the Wire library. You could make your own code to run this, but it would have to use I2C to send commands to the PWM driver chip they are using.