How to turn motor counter-clockwise - arduino

Currently, my code below turns on the motor, delays for a bit then starts again. This is all being done in the clockwise direction, however how can I write my code so it can turn counter-clockwise?
int motorPin = 3;
void setup()
{
pinMode(motorPin, OUTPUT);
}
void loop()
{
startStopMotor(135);
delay(1000);
startStopMotor(0);
delay(1000);
}
void startStopMotor(int speed){
analogWrite(motorPin, speed);
}

By the code you share i'am guessing you are running a low power 5v dc motor... but you should edit your answer to give us what type of hardware you are using. This is not an answer but an idea of what you should be looking for... Basically on the motor i suppose you have, you have pin 1 and pin 2. Pin 1 is connected to a PWM signal and pin 2 is connected to ground. This configuration allows you to run your motor clock'wise. To run your motor counter clock'wise you need to invert the direction of the current basically have pin 1 connected to ground and pin 2 connected to a PWM signal.
Now there are multiple ways of doing this, i am unsure of the exact code to do this on an arduino but your pin 1 and 2 will be connected each to a PWM pin. In the code you will need to tell the arduino to put Pin 1 or 2 as a pullDown pin which basically mimics a ground thus telling the direction the other pin will output a PWM
this is not example code but it will give you an idea of what it should look like
void loop(){
//move clock'wise
pin1.pullup();
pin2.pulldown();
analogWrite(pin1, 180);
//move counterclock'wise
pin2.pullup();
pin1.pulldown();
analogWrite(pin2, 180);
}

Related

Arduino - Light is staying on for more than expected duration

Code is suppose to Light the inbuit-LED at Pin 13 whenever Pin 5 is High, however i have encountered couple of problem.
While measuring Voltage though Digital Meter - one pin at arduino GND and other at 1,2,3,4. They are showing some non-zero values. Earlier triggering Pin was 4 and light was staying on all the time.
When Pin 5 is high (by connecting 5V Pin from Arduino to Pin 5) it lights the LED as it should, but if Pin 5 stays high for more than 1/2 second, light stays high for more than 0.5 second even after the Pin 5 is disconnected from 5V Pin.
int buttonState = LOW;
int light = 13;
void setup() {
// put your setup code here, to run once:
pinMode(gateopen,INPUT);
pinMode(light, OUTPUT);
}
void loop()
{
// put your main code here, to run repeatedly:
buttonState = digitalRead(gateopen);
if (buttonState == HIGH)
{
digitalWrite(light, HIGH);
}
else
{
digitalWrite(light, LOW);
}
//delayMicroseconds(500);
}
As suggested earlier, use a pull down resistor. Or change your circuit to active low and use INPUT_PULLUP.
I believe you're using delayMicroSeconds, which means the led is just blinking waaay too fast for your eyes. If you want 'half a second' as you hinted on your query, you would be using delay(500) instead.
You are experiencing electrical noise, just like Juraj said, just put a 220 Ohm pull down resistor

radio control with arduino

Hi I am attempting to read from an rc transmitter using an Arduino Uno board, I have a signal pin connected from the receiver to pin 9 on the Arduino. Here is the code I would really appreciate some help all I am trying to achieve is the read the pmw from the receiver. I am able to plug a servo into the receiver and that works fine along with a motor I am just struggling when I try and use the Arduino with the receiver. When I run my program all I get in the Serial monitor are values such as 9991,9972,10030,10050 that are completely unrelated.
I want to have a pmw value that I can map to 0-255 in order to control a motor
My Circuit:
battery -> ESC(for BEC to regulate five volts back to receiver)-> receiver -> ch3 signal pin -> Arduino uno (pin9)
void setup() {
Serial.begin(9600);
}
void loop() {
int pwm = pulseIn(9, HIGH, 25000);
Serial.println(pwm);
delay (5);
}
You are using pulseIn which returns a time (in ms). The time being how long it waited for a HIGH signal. If you want the actual value, use analogRead. You can still use pulseIn, just don't use the return value
void setup() {
Serial.begin(9600);
}
void loop() {
byte pwm = analogRead(A5) / 4;
Serial.println(pwm);
delay(5);
}

cannot turn on relay indefinitely on an arduino

I want to make a device like Knocki(https://knocki.com), which essentially is a relay control using a vibration sensor. i can detect vibrations rn but the problem is, once i knock the relay blinks on and then turns off. i understand this is a lack of programming that is causing this. could someone help me write code which makes it so that when i knock the relay is turned on indefinitely; until I knock again to turn relay off.
And yes u can probably tell that this code is copied from somewhere(https://wiki.keyestudio.com/Ks0068_keyestudio_37_in_1_Sensor_Kit_for_Arduino_Starters#Project_21:_Vibration_Sensor).I took it from the home page of the vibration sensor. the code was initially so that every time i knocked, the onboard Arduino led lit up. Also, right now the relay is blinking faintly every time i knock(Although correctly,in sync with my knocks)
#define SensorLED 13
#define SensorINPUT 3 //Connect the sensor to digital Pin 3 which is Interrupts 1.
unsigned char state = 0;
int Relay = 5;
void setup()
{
pinMode(SensorLED, OUTPUT);
pinMode(SensorINPUT, INPUT);
attachInterrupt(1, blink, FALLING);// Trigger the blink function when the falling edge is detected
}
void loop()
{ if(state!=0)
{
state = 0;
digitalWrite(SensorLED,HIGH);
delay(500);
digitalWrite(Relay,HIGH);
}
else
digitalWrite(SensorLED,LOW);
digitalWrite(Relay,lOW);
}
void blink()//Interrupts function
{ state++;
Yes its in your code: The (bad) example works only because there is a
digitalWrite(SensorLED,HIGH);
->>> delay(500);
a delay for 1/2 sec to keep the led on.So as a check put an other delay after the relay line and it should go on for 1/2 sec too (so the led is lit 1 sec in total)
digitalWrite(SensorLED,HIGH);
delay(500);
digitalWrite(Relay,HIGH);
delay(500);
Thats just for checking -> NEXT STEP:
Get rid of the delays (see blinkwithoutdelay example in
Arduino->File->Examples->2.Digital -> blinkwithoutdelay
and introduce a second state variable e.g.
bool relayStateOn = false;
to get an independent on/off of the relay and the led.(If thats - what I understand -what you want to do)
If you feed your relay from the board, that is not the problem. Please, check the voltage in your relay when you try to set it on, if your voltage falls down, it means that this output to your relay does not supply the necessary current.

Motor rotates only one direction with L293D controller

Hello I have an RC car which has two 3v motors (one for left/right and the other one for forward/back). The left and right motor is working fine but when I try to rotate the other motor it rotates only back. I've tried the motor separately and it works in both direction without the controller.
My code is the following:
int enablePinMotorAF = 3;
int in1PinMotorAF = 5;
int in2PinMotorAF = 6;
int enablePinMotorLR = 11;
int in1PinMotorLR = 10;
int in2PinMotorLR = 9;
boolean reverse = true;
void setup() {
pinMode(enablePinMotorAF, OUTPUT);
pinMode(in1PinMotorAF, OUTPUT);
pinMode(in2PinMotorAF, OUTPUT);
pinMode(enablePinMotorLR, OUTPUT);
pinMode(in1PinMotorLR, OUTPUT);
pinMode(in2PinMotorLR, OUTPUT);
}
void loop() {
//go forward ->not working
analogWrite(enablePinMotorAF, 230); //max speed
digitalWrite(in1PinMotorAF, reverse);
digitalWrite(in2PinMotorAF, !reverse);
delay(3000);
//go back -> working
analogWrite(enablePinMotorAF, 230); //max speed
digitalWrite(in1PinMotorAF, !reverse);
digitalWrite(in2PinMotorAF, reverse);
delay(3000);
//go right -> working
analogWrite(enablePinMotorLR, 230); //max speed
digitalWrite(in1PinMotorLR, !reverse);
digitalWrite(in2PinMotorLR, reverse);
delay(3000);
//go left -> working
analogWrite(enablePinMotorLR, 230); //max speed
digitalWrite(in1PinMotorLR, reverse);
digitalWrite(in2PinMotorLR, !reverse);
delay(3000);
}
Here is the wiring too:
Wiring
The green and orange wires are for a Bluetooth module.
Do you have any idea how can I solve this problem and to make it work?
Thank you.
To reverse the motors, you need four pins, two for each motor. On a readily-available L293 module, they are often labeled IN1, IN2, IN3, and IN4.
To make one motor go forward, you might set IN1 to 5V and IN2 to 0V. To reverse it, simply switch the inputs, IN1 to 0V and IN2 to 5V. In this case 5V is a digitalWrite(pin, HIGH).
Similar for the other two pins for the other motor. I start my answer with this because the wiring of what output pins to what input pins is vitally important.
The Enable pins is where you've gone wrong, it seems. Enable2 and Enable1 should be connected to the pins to which you're doing the analogWrite() but enablePinMotorAF = 3 for example connects to a motor signal input, not to Enable2 as it probably should. Start with fixing that... your two pins 3 and 11 should be connected to Enable1 and Enable2. You only need PWM on the Enable pins. The others should simply be activated with digitalWrite().
Once you get the Enable n pins connected to PWM, then you'll have a good PWM enable signal. Simply connect the other pins on the same side of the chip (IN1 and IN2 for Enable1 and IN3 and IN4 for Enable2), and turn them on and off with `digitalWrite(pin, HIGH) and you'll be good to go.

Connecting Servo to Arduino (branded) Robot

I am trying to attach a servo on an Arduino (branded) Robot but not sure whhich pin to use for the bellow code.
Most people seem to recommend to use pin 9 and 10 to control the servo for arduino Unos.
However, I can't use Pin 9 because that is already used as the Slave Select pin for the LCD.
I have tried attaching it to pins TKD0-TKD3 by calling them pins 19-22 in myservo.attach(). The code runs but the servo doesn't rotate and only gets hot and/or twitches.
Could the problem be something other than incorrect pin connection?
Thanks,
-M
I have been referencing these for the Control board pin mapping:
http://arduino.cc/en/Main/Robot)
http://fabcirablog.weebly.com/blog/grappling-with-the-arduino-robot-control-board
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 0;
void setup()
{
myservo.attach(19); // attaches the servo on pin 9 to the servo object
}
void loop()
{
for(pos = 0; pos < 60; pos += 1)
{
myservo.write(pos);
delay(15);
}
for(pos = 60; pos>=1; pos-=1)
{
myservo.write(pos);
delay(15);
}
}
You have this robot, right?
Arduino Robot
And you are using a classic servo (3 pins, yellow red and black cable)? If so.... Watch out. According to the schematic, the pinout of the connector on the board (e.g. TKD0) is
+5V
AD1
GND
While usually servos have
DATA
+5V
GND
So.. you have to make a short cable to invert the pins.
If that's not the problem.. Are you sure that 19 is the right number for the pin? I can't find references, but i suggest you to call it TKD0 (which is probably a macro defining the right pin), as arduino designers suggest you.

Resources