Connecting Servo to Arduino (branded) Robot - arduino

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.

Related

Connecting AI-Thinker ESP32-cam to Adafruit PCA9685 servo controller board

I am new to Arduino and Esp32 programming. I need to connect PCA9685 controller to ESP-32 cam to be able to control multiple servo motors but the SCL and SDA pins are occupied by the UART control board.
I searched for this and found something related to Wire.h library and wire.begin() and TwoWire but unable to implement it.
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
#define SERVOMIN 150 // This is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX 600 // This is the 'maximum' pulse length count (out of 4096)
#define USMIN 600 // This is the rounded 'minimum' microsecond length based on the minimum pulse of 150
#define USMAX 2400 // This is the rounded 'maximum' microsecond length based on the maximum pulse of 600
#define SERVO_FREQ 50 // Analog servos run at ~50 Hz updates
// our servo # counter
uint8_t servonum = 0;
void setup() {
Wire.begin();
Wire.begin(2,15,100000); // this is to map new SCL and SDA pins to 2,15
Serial.begin(9600);
Serial.println("8 channel Servo test!");
pwm.begin();
pwm.setOscillatorFrequency(27000000);
pwm.setPWMFreq(SERVO_FREQ); // Analog servos run at ~50 Hz updates
delay(10);
}
// You can use this function if you'd like to set the pulse length in seconds
// e.g. setServoPulse(0, 0.001) is a ~1 millisecond pulse width. It's not precise!
void setServoPulse(uint8_t n, double pulse) {
double pulselength;
pulselength = 1000000; // 1,000,000 us per second
pulselength /= SERVO_FREQ; // Analog servos run at ~60 Hz updates
Serial.print(pulselength); Serial.println(" us per period");
pulselength /= 4096; // 12 bits of resolution
Serial.print(pulselength); Serial.println(" us per bit");
pulse *= 1000000; // convert input seconds to us
pulse /= pulselength;
Serial.println(pulse);
pwm.setPWM(n, 0, pulse);
}
void loop() {
pwm.setPWM(0, 0, 250);
}
Here is the test code that I am trying to run.
I got the implementation idea from https://randomnerdtutorials.com/esp32-i2c-communication-arduino-ide/
There are a few problems that I encounter along with no response from the code-
When PCA9685 is connected to esp32-cam and I try to upload the code, it doesn't work and gives fatal error.
When the reset button is pressed before upload the built-in flash blinks. This is presented as a problem because while trying to upload the code without PCA9685 attached, the code is uploaded without any fatal error and there is no 'flash blink'.
I tried a code from the same website mentioned above that looks for I2C devices and prints their address. It shows no devices even though the servo controller is connected to it.
PS- I am an absolute beginner in this field.

How to turn motor counter-clockwise

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);
}

How to solve flickering WS2812?

I have a problem with flickering neopixel leds. I'm using a setup of 6 strips of 8xWS2812 5050 leds. I've connected these to a 5v power supply (USB charger) and an Arduino Uno running the Neopixel simple example code:
// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 6
#define NUMPIXELS 48
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int delayval = 50; // delay for half a second
void setup() {
// This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
// End of trinket special code
pixels.begin(); // This initializes the NeoPixel library.
}
void loop() {
for(int i=0;i<NUMPIXELS;i++){
pixels.setPixelColor(i, pixels.Color(200,200,200)); // Moderately bright green color.
pixels.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
I've connected a 500uf 10v capacitor between the 5v and the gnd. And a 470 ohm resister between the arduino and the datapin.
I've noticed that the leds also flicker when the leds are on and the datapin is disconnected. The flickering is subtle, but annoying.
I've also tried an alternative power supply. For this I used a adjustable lab power supply. But the same problem persists.
I have to use these leds for a project that I need to finish tomorrow. (I know i should have done this earlier) Is there anyone who could offer me some help? I could really use some.
EDIT: The flickering had to do with the quality of the leds. These weren't real neopixels, instead I received some bad fake ones.

Arduino UNO analogRead always returns 1023

So my problem is as title says: Arduino UNO analogRead always returns 1023.
But when I burn the same sketch in Arduino Mega 2650 everything works like a charm.
I have tried to change Atmel chips on the UNO, have tried like 3 of them (ATMEGA328P-PU) and nothing changes.
I'm trying to count signals from a hall effect sensor and display the count on a 7 segment display.
Here is the code:
#include "SevSeg.h"
SevSeg sevseg;
volatile int rpmcount;
void setup() {
Serial.begin(9600);
pinMode(2,INPUT_PULLUP);
rpmcount = 0;
sevseg.Begin(1,3,4,5,6,7,8,9,10,11,12,13);
}
int border=15;
void loop() {
int tmp=0;
tmp = analogRead(0);
if(!digitalRead(2))rpmcount=0;
Serial.println(tmp,DEC);
if(tmp<=border && res >border){
rpmcount++;
if(rpmcount>9999)rpmcount=0;
}
res=tmp;
sevseg.NewNum(rpmcount,(byte) 0);
sevseg.PrintOutput();
}
Any help would be much appreciated
This sounds to me as if you had the internal pullup resistor on the ADC pin enabled.
generic checklist:
ACD bit in ACSR is 0 (comparator enable)
MUX bits in ADMUX set properly
correct AREF selected
ADC pin set as input
internal pull up resistors are deselected

How to control speed of a Servo motor using arduino Mega

I am working on a project in which I need to change the speed of servo motors. The hardware I am using is an Arduino Mega 2560 board and I am using Servo.h library to control servos. Servo rotates from o to 180 degree. I am using 12 servo motors in the project and have to control them simultaneously. Is there a way?
You can use delay() function in a while or for loop
Example:
Servo s;
s.attach(9);
for(int i=0 ; i<180 ; i++)
{
s.write(i);
delay(10); //10 milisecond
}
If they all are the same degree, try this code below.
At the very top (Not between any "{}"s):
#include <Servo.h>
Servo S1;
Servo S2;
Servo S3;
Servo S4;
Servo S5;
Servo S6;
Servo S7;
Servo S8;
Servo S9;
Servo S10;
Servo S11;
Servo S12;
Put this in Setup:
S1.attach(1);
S2.attach(2);
S3.attach(3);
S4.attach(4);
S5.attach(5);
S6.attach(6);
S7.attach(7);
S8.attach(8);
S9.attach(9);
S10.attach(10);
S11.attach(11);
S12.attach(12);
You need to change the pin numbers.
Just put this anywhere (Not between any "{}"s):
void TurnServos(int toDegree){
servosAt = S1.read;
if(servosAt == toDegree){
}
if(servosAt > toDegree){
while(S1.read > toDegree){
int currentToDegree = S1.read - 1;
S1.write(currentToDegree);
S2.write(currentToDegree);
S3.write(currentToDegree);
S4.write(currentToDegree);
S5.write(currentToDegree);
S6.write(currentToDegree);
S7.write(currentToDegree);
S8.write(currentToDegree);
S9.write(currentToDegree);
S10.write(currentToDegree);
S11.write(currentToDegree);
S12.write(currentToDegree);
delay(10); //Adjust this to make it faster or slower.
}
}
if(servosAt < toDegree){
while(S1.read < toDegree){
int currentToDegree = S1.read + 1;
S1.write(currentToDegree);
S2.write(currentToDegree);
S3.write(currentToDegree);
S4.write(currentToDegree);
S5.write(currentToDegree);
S6.write(currentToDegree);
S7.write(currentToDegree);
S8.write(currentToDegree);
S9.write(currentToDegree);
S10.write(currentToDegree);
S11.write(currentToDegree);
S12.write(currentToDegree);
delay(10); //Adjust this to make it faster or slower.
}
}
}
void ClearServos(){
int startDegree = 90; //Change this number to anything you want.
S1.write(startDegree);
S2.write(startDegree);
S3.write(startDegree);
S4.write(startDegree);
S5.write(startDegree);
S6.write(startDegree);
S7.write(startDegree);
S8.write(startDegree);
S9.write(startDegree);
S10.write(startDegree);
S11.write(startDegree);
S12.write(startDegree);
}
How to use this:
In setup before you do anything with servos but after the part I told you to put in setup, use ClearServos(); to prepare the servos to be used. (This probably isn't necessarily, but I don't know what happens when you use S1.read without changing it and if the servos are at different positions, it will fix problems. It can be avoided if it won't cause problems, but I think you should use it if you can.) All of them will turn to 90 degrees. (90 degrees can be changed with the variable startDegree in void ClearServos.)
To turn them, use TurnServos(90);. 90 is the degree you want it to turn to.
Haven't tested this because I don't have a Mega or 12 servos. Please comment if you notice any errors since this is huge. I spent a lot of time on this so I hope I helped. :)
Maybe you can put some resistors in series to your servo's VCC pin, before your servo motor to decrease the voltage across; thus slowing it. However, this will cause your servo's to be "constant" speed.
An alternative could be to put a transistor in between your servo VCC connection and set PWM on base pin to regulate current (to regulate speed), but that would cost you an extra pin per servo if you're not using a multiplexer in between; and could make your design a little more complicated.
delayMicroseconds(value) closest to 90-null is slowest for 360 servos both pan and carriage on my timelapse rig, shoot move shoot, in time with the mechanical shutter clicker (mini standard servo).
in Servo library WriteMicroseconds(...) function sets servo speed.
for more information please click

Resources