How to control a motor with two inputs using arduino - arduino

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.

Related

STM32 Blue pill and Arduino IDE

I want to program Stm32 bluepill with Arduino IDE but when I want to define pins like I write " pinMode(A10, OUTPUT)" it gives error. the error is "'A10' was not declared in this scope"
I want to know how should I declare Pins in Arduino IDE for STM32
Based on the error you're reporting, you're not building your code for the correct board. I suspect you're targeting the ArduinoUNO (default) which does not have an A10.
Also, as hcheung's answer mentions, the name on the blue pill is "PA10".
Follow the instructions here to install the board configuration for the STM "blue pill" then select it and build again.
https://maker.pro/arduino/tutorial/how-to-program-the-stm32-blue-pill-with-arduino-ide
Note, the board selection as of today is now "STM32F1 series" instead of "STM32F103C series" as specified at the link.
One reason could be there is not ADC pin of number 10 for the currently selected board (check the board on tool -> boards), there might be fewer number of ADC pins, e.g. try A0.
Or maybe you have selected wrong board. Bluepill isn't included in the Arduino IDE, by default. So you have to add it to your IDE first.
There is a nice instruction here on how to do this and a sample code.
https://maker.pro/arduino/tutorial/how-to-program-the-stm32-blue-pill-with-arduino-ide
Remember that this newly installed library could have small differences in syntax compared with standard Arduino code, Like this example that is taken from the mentioned site:
void setup() {
// change pin PC13
pinMode(PC13, OUTPUT);
}
//infinite loop
void loop() {
digitalWrite(PC13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(PC13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
As you can see here the pin is selected using "PC13", and not just a number aka "13".
So in this case by installing the library used in the mentioned site you should write PA0 to PA7 for using ADC.
Here is a sample picture displaying the name of the pins and their features:
For STM32 Blue Pill with Arduino Core, digital pins are named based on its port and pin number, such as PB1, PA10.
There is no A10, I assumed that you mean PA10 (which was marked on the Blue Pill PCB as "A10" (for Port A Pin 10) due to limit space on the PCB.
To use it as a digital pin, simply use PA10 to address it, that is:
pinMode(PA10, OUTPUT);
or because PA10 internally happened to be referred as D10, you can also use:
pinMode(10, OUTPUT); //not recommended
For better understanding of all the pin assignments for STM32F103 Blue Pill, please take a look at the source code here and here.

Built-in led glowing on code of led on arduino mega

I had written a code on atmel studio for blinking a led on pin 13. After uploading the code with xloader mega's builtin led was blinking.
I uploaded fade code on my mega and the builtin led was blinking instead of led. What should i do?
I am using arduino mega 2560.
int main(void)
{
DDRB=0b00000000;
while (1)
{
PORTB=0b10000000;
_delay_ms(1000);
PORTB=0b00000000;
}
}
What you should do? Read the manual.
Please refer to https://ww1.microchip.com/downloads/en/devicedoc/atmel-2549-8-bit-avr-microcontroller-atmega640-1280-1281-2560-2561_datasheet.pdf
Chapter 13.2.
The DDxn bit in the DDRx Register selects the direction of this pin.
If DDxn is written logic one, Pxn is configured as an output pin. If
DDxn is written logic zero, Pxn is configured as an input pin.
Working with registers doesn't make sense if you don't know what they do.
DDRB=0b00000000;
Gives you inputs only.
why would you use Arduino and try to program it without its conventional macros and functions?
If you are trying to blink an led or make it breath then use the Arduino IDE and its built-in functions analogWrite() to generate a pwm pulse for your led or any led on suitable pins which support analogwrite(). You shouldn't try to do any direct modifications on registers if you have no suitable knowledge, because your risk destroying your development kit and maybe burning some other stuff around. Please use your kit's schematics to spot the pins which support analogwrite() and then use the code in examples.
That way you will achieve your goal faster and without any issues.
TL/DR: you have to set 7th bit in DDRB to one.
In AVR ports are configured by bits in two registers: DDRx and PORTx.
When the corresponding bit in the DDRx register is set to one, the port is configured as output. And the corresponding bit in the PORTx register chooses which electrical level is output on the pin. If it is 0 then internal MOSFET shorts the pin to "ground" lane, and sinks current from external source. When the bit of the PORTx is one, then the pin is connected to "VCC", sourcing big amount of current enough to lit up a LED.
But if the pin is connected to something, what consumes too much of current, or the pin is shorted to GND or VCC (let's say you have a button connected and pressed), then output MOSFETS might be overloaded and damaged.
If the bit in DDRx is set to zero, then the pin is configured as input. If the corresponding bit in the PORTx is zero, then the pin has no internal connection to power lines, it is called "Hi-impedance" state, or Tri-state. It does not source or sink any current. So, if no external source of current is connected, then pin level is floating, influenced by electrical interference. Logical level is not detectable and can change occasionally. If you want to connect, for example, a button (between the pin and GND), then logical level will be defined only when button is pressed. When it is released, the logical level will be undefined.
But! If the bit in the PORTx is set to one, then internal MOSFET connects the pin thru a resistor (about 35 kOhm) to VCC line. This make the pin to source a little amount of current, setting its logical level to high. Therefore, if a button is connected, when it is released, then pin will have defined high level. This is called "pull-up resistor". When button is pressed, it will not short and damage the MCU, because current flowing thru the button is limited by the resistor, but the logical level will be defined low.
What if instead of button you have a LED connected to the pin? Very small amount of current will flow thru the LED, makes it barely glow.
Read more in the datasheet (chapter 13. I/O-Ports)

doit.am 4-way motor & 16-way servo shield board not working with adafruit libraries

Got a servo + motor driver arduino shield with a robotic arm chassis from amazon. Found this link on the web :
https://github.com/SmartArduino/SZDOITWiKi/wiki/2-4ch-DC-motor-%26amp%3B-16ch-servo-shield---motor-and-servo-shield
Not enough details to help me control servos/ motors for my own project.
Tried the libraries given in the answer to this question:
doit 2-way motor & 16-way servo shield board
The libraries have been included, but i can't figure out how to use it properly. Ran the sample code as given in the adafruit website :
https://learn.adafruit.com/16-channel-pwm-servo-driver/using-the-adafruit-library
The 0th channel didn't move the servo as expected so I don't know what to try next.
Alright guys, here's the complete reply, with some precautions for future users.
The shield can be used via the library available at https://learn.adafruit.com/16-channel-pwm-servo-driver/using-the-adafruit-library.
(Shoutout to #djsfantasi ).
NOTE : One of the main things to ensure is that the 5v power supply for the servos must be separate from that of the arduino, as it messes up the servos somehow.
The motors can be powered via the given slots on the shield. All you have to do is supply the DIR and PWM values to the 1st 8 available arduino pins (after pin 0 and 1, ie, 2-9). It is pretty easy to figure out which pin controls which channel.
NOTE: Since the motor pins are available via male headers stacked very close together, i would suggest using female headers/jumper wires before connecting them to your motors. I burned my 1st shield's motor driver due to the +ve and -ve ending up shorted together.
Thanks for the replies everyone and best of luck to you guys reading this.

Arduino + GSM shield V2 // How to use buzzer signal

I have a GSM shield V2 for Arduino, and I want some buzzer to make some noise when there is an incoming call. At this link http://www.thaieasyelec.net/archives/Manual/M10_HD_V1.00.pdf page 44 I found that by connecting a simple transistor and a buzzer to the actual "buzzer" pin I should be able to produce sound. I tried and that does not work as expected, all I get is noise from the GND of the shield, that typical GSM noise that everyone know of.
I also tried to connect another arduino as to analog read the buzzer signal, but I get nothing that look like a ringing tone.
Has anyone any idea? Did I forget to setup some things software wise? So far it seems that the buzzer behaviour is completely unrelated to anything code wise, there is just that "buzzer" pin, and that's it, nothing more to set up.
Any help would be much appreciated !
Cheers
Here's a great beeper/alarm part I've used in a couple of recent projects. It is loud but can be muffled. It's self-driving, so all you have to do it supply it enough current at its rated voltage, like through a transistor or Darlington. It has a wide voltage range, and runs great from 3.3V up to 20, So it's ideal for microcontroller projects at 3.3V or 5V. No need to fiddle with timers or PWM to make it beep. Try out this great part.. Drive it from any output pin to a small-signal transistor with a beta of 50 or better and you'll be good to go. Turn output pin on, it starts. Turn output pin off, it stops. I made mine "chirp" like the alarm on a car security system. Easiest thing in the world.

Arduino stepper

I am building a system with the Arduino Uno, a power shield (REf to model) and a bipolar stepper motor.
I cannot initiate the motor using the stepper library from Arduino. I instantiate my stepper with
Stepper myStepper(motorSteps, motorPin1,motorPin2m motorPin3,motorPin4);
And keep on with the example code provided with the Arduino.
When I launch the code on the Arduino, the motor emits some sounds, but it does not turn.
How can determine the right motor pins to use? On the power shield, which mode is to be used, PWM or PLL?
As you suggest, the adafruit motor shield should fit the arduino stepper library, as it uses a L293D to drive the motor. It can drive 2 stepper with an current of 0.6A (good for most of little stepper you can find in printers, floppy/CD/DVD reader...).
Be careful, they seem to use their own library for this shield, you can find it here :
http://www.ladyada.net/make/mshield/download.html
And to know how to connect your stepper, look here :
http://www.ladyada.net/make/mshield/use.html
Sorry to answer your comment this way, but I don't have enough reputation to comment... so please, +1 my answer if you think it's a good answer :)
It's normal that the arduino sketch doesn't work. It drives the motor like this:
PIN1 : coil 1, forward current
PIN2 : coil 1, backward current
PIN3 : coil 2, forward current
PIN4 : coil 2, backward current
Your shield drives the stepper this way:
PIN1 : current forward/backward
PIN2 : current intensity with PWM
PIN3 : current intensity with PWM
PIN4 : current forward/backward
Not a good idea to drive a stepper motor, as you don't even need PWM to drive a stepper. It's to drive a DC motor. You may write your own sketch to drive a stepper with that shield, but you should find a shield that fit the arduino to drive a stepper. Look for something like "UL2003 stepper module", it costs few dollars.
Edit: I've got one of those modules and it works like a charm. Be careful about the power you need. Maybe you need something like a L298N module. I've got few of them too and they work fine.
If the motor emits some sound, this is good news. At least you have contact with it. Since it is just a sound but no movement, there might be three things laying underneath of this behavior;
Wrong pin connections of the motor
Insufficient current feeding from the motor driver.
Step counts determined by the code using PWM might be
too less or too high as per the motor can handle.
I can offer this URL link to determine motor pin-outs;
How Can I Determine My Stepper Motor Wiring Without the Stepper Motor Pinout
In stepper motor how many wires you have? 4 or 6.
Your connection is faulty. That's why it makes such sound.
Test with multimeter. you find two wires give high resistance. other two give half from that. Connect the first two with motor driver out1 and second two with out2.
You didn't mention which motor driver you are using. Try with L298 motor driver shield.
I think the problem you are having is related to the kind of shield you are attempting to use. The power shield that you linked is designed for DC motors and you are attempting to use a stepper motor (see this website for an explanation of the difference).
I would recommend trying another shield (like the Sparkfun EasyDriver) that supports the use of a stepper motor.
The problem seems to be wrong connected motor pins.
Are you sure, you connect true windings to motor?
Measure the motor pins with ohmmeter. There should be two windings which have pins A1-A2 and B1-B2. Between these pins you should see some resistance value.
Then be sure you connect to shield with right order.
In mi projects I use a bipolar motor (a nema17), and you can drive it easy with a A4988 pololu (or drv8825, more powerfull, and more expensive).
Those drivers has an H bridge inside, and allows you to control the motor by a simple:
while(1){
digitalWrite(PIN_STEP, HIGH);
delay(1000);
digitalWrite(PIN_STEP, LOW);
delay(1000);
//1RPM = 100 microsecond delay for a 1.8 degree angle motor (200 steps per
turn)
}

Resources