10

No, it's not going to work - even worse, you risk frying your Arduino -, for the following reasons: The Arduino data pins can't source (neither sink) enough current for that to work. The inductive kickback of the motor could fry Arduino pins. The right way to do what you want to do is using an H-bridge controlled by your Arduino data pins. There are lots ...


6

First, do something simpler, and eliminate possible causes. Are you certain the NodeMCU's CPU is an 8bit PWM? If it were 9bits, it would read about 50% full scale, and 10bits would be about 25% of digitalWrite (which is what I think I can see on the photo). Swap the analogue meters around, and see if the difference is consistent with the pin, or consistent ...


5

chrisl provided a very good reference answer. Here I am just adding a few bits of information about the pin mapping and the pin electrical states. Pin mapping One of the firs things you need when dealing with direct port access is the mapping between the Arduino pin names and the names of the microcontroller (MCU) pins as given by the manufacturer. For the ...


4

I just tried to time digitalWrite() against direct port access with the following code: void setup() { uint16_t overhead, start, end; Serial.begin(9600); TCCR1A = 0; TCCR1B = 1; // Time digitalWrite(). overhead = 7; cli(); start = TCNT1; digitalWrite(2, HIGH); end = TCNT1; sei(); Serial.print("digitalWrite():...


4

digitalWrite() is just a convenience function to hide away complexity from the user, so that she/he can just use a pin number and a value. This complexity takes some time to execute. If this is too slow for you, you need to go a bit deeper into the rabbit hole and use direct port manipulation: Inside the microcontroller on the Arduino all the special ...


3

On an ATmega2560, can ports have some pins used for analog inputs but others as digtial i/o? Yes, the ADC MUX controls the selection of the pin that is connected to the converter. This is independent of the digital pin control. Cheers! PS: See the Arduino core source code for further details on the ADC MUX control registers.


3

All Teensies support digitalWriteFast out of the box. digitalWriteFast compiles to just setting the bit in the right port register. You can not do that faster by direct register manipulation. However, if for some reason you do want to manipulate the registers directly here the pin/gpio relation for the T4. (Generated by the sketch described here: https://...


3

Some time ago, I timed digitalWrite() and direct port write commands by looping over 10,000 executions of each and timing them with millis(). I made both tests on a 16MHz Atmega 328p at 16MHz. For digitalWrite(13, LOW), I got 2.7usec / call. For PORTB &= 0x20;, I got 0.15usec / statement. (The direct port access was executed 10 times within the loop to ...


3

I just tried what you suggest, and it works fine, with only one (big) caveat: you can only power this way very low power motors. I did this to drive the linear motor that moves an HDD head assembly. This motor works perfectly fine with less than 18 mA. Note that I switch the Arduino pins between the HIGH and LOW states, not going through INPUT. Setting ...


2

Yes, setting a pin to OUTPUT and setting it LOW effectively connects the pin to ground (via a few ohms of resistance). There's a couple of things wrong with your code, though, which you should address: Use digitalWrite() not analogWrite(). analogWrite(pin, 0) is effectively the same as digitalWrite(pin, 0), but it's ambiguous to someone reading your code....


2

Every other value than 0 or LOW will result in setting the pin to HIGH. In the following I explain why: The function digitalWrite() can be seen in the file hardware/arduino/avr/cores/arduino/wiring_digital.c. The important lines here are: if (val == LOW) { *out &= ~bit; } else { *out |= bit; } As you can see, it checks, if the given value (...


2

I don't think that's going to work. You'll be better off using a cheap module like the Pololu Pololu DRV8838 which implements a MOSFET H-bridge. (Note: I'm not related in any way with Pololu.)


2

Personally I'd expand it out a little: if (commandbits & (1 << i)) { PORTB |= (1<<3); } else { PORTB &= ~(1<<3); } It's far more readable, and there are less shifts to mess around with. Plus the AVR has bit set and bit clear operations which that will make use of, especially since it's now using literal values. You could ...


2

A timer interrupt will be periodically toggling the PWM bit and might do so between your port read and port write. The easy solution is to turn off interrupts during the read/modify/write portion of your port update sequence. This could cause a stretched PWM cycle. If you don't turn it off, you'd just glitch the PWM - creating an extra, short cycle. Which ...


2

There are two solutions: Use the pin as a open-collector pin as in the answer of VE7JRO. Switch the pin between input (high) and output with low. I prefer to use the INPUT_PULLUP. Write the output value before setting the pinMode. All the microcontrollers of the AVR family (ATtiny and ATmega chips) allow to set the output value before setting the pin as ...


2

Setting ports directly is a great way to learn AVR programming. I might recommend this book Make: Avr Programming. First you need to set the direction of the port in the Data Direction Register as either input or output. If Input, check its state and handle it. Here is a small example. #define HIGH 1 #define LOW 0 #define SWI_PIN PORTB0 #define BUTTON ...


2

Your sketch works on a Nano (clone) that I have I am using a high-brightness white LED with a 10kΩ resistor to bring the brightness down to a bearable level, so the current drawn is about (5v-2.2v)/10kΩ = 0.28mA. This is a low level of current and should be safe for poking around with. I can see the embedded LED blink if I program the Pin 13, but no impact ...


2

The setOutputPins() function sets the pins for a pulse for a single stepper. Depending on the used stepper motor a different number of pins has to be set (normal stepper drivers have 2 pins, the ULN2003 and its siblings have 4) in a different way. For that the function first determines the number of pins to use via the _interface member variable of the class....


1

Never directly connect a motor to an Arduino IO pin You will destroy the Arduino. An Arduino cannot source (or sink in your case) enough current to drive a motor, and if the motor did manage to turn the huge amounts of EMI it generates would literally blast holes in the silicon inside the chip. You must use a motor driver of some form. If you only want one ...


1

The esp8266 must have a pull-up on io 2 for boot configuration. The Wemos D1 boards put the LED in series with the pull-up resistor on io 2. This creates the 'reversed' LED behavior. The large D1s are retired and it is hard to find a good and readable schematics. The cut out is from D1 mini schematics.


1

How can I set up outputs without using digitalWrite? So this question is actually about the fastest possible implementation of digitalWrite. As described in other answers this can be achieved by using direct port manipulation. But there are actually libraries (for instance, https://github.com/mikaelpatel/Arduino-GPIO) that does this in a portable way and ...


1

There are a number of issues here: You state you have a 560kohm resistor, this is too high a value. With 5V it will restrict the current to 8.9uA. This is too low to drive the LED, which probably requires up to 20mA (5V / 0.02A = 250Ohm at least) This is also too low for for the base of the transistor (I'm not completely sure if my understanding of ...


1

As Jot wrote, you cannot simply route a signal from pin to pin inside the chip/Arduino. So - unless you redesign the PCP, which would be the cleanest way, or use jumper wires, which is not so clean - you are down to using software PWM. There are also libraries for this, if you don't want to write this yourself, for example the SoftPWM library. These ...


1

The boot setup pins have pull-up on board (0, 2) and pull-down (15) to ensure the correct boot. io 0 (D3) is additionally wired to the reset circuit controlled over USB to put the esp8266 into bootloader a.k.a. flashing mode. io 0 must be HIGH for normal boot, io 2 must not be LOW (can float according to spec). If you attach a device to this pins, the ...


1

If you have a relay that is activated by a LOW or ground path, and is deactivated by an open circuit or HIGH signal applied, then you could try switching the pinMode to act as the HIGH or open circuit. const byte pin = 2; void setup(){ // Define the pin as INPUT_PULLUP until you are ready to use it. pinMode(pin, INPUT_PULLUP); } void loop(){ ...


1

As fas as I know, by default digital pins are set as inputs. Arduino has internally only pull-up resistors, which you can use in that way: digitalWrite(pinOut[i], HIGH); in first setup() line (without pinMode(pinOut[i], OUTPUT);). If you need LOW state on start-up (pull-down resistors), you have to use an external resistor.


1

If you like both portability, readability and performance there are a few libraries (for instance Arduino-GPIO): #include "GPIO.h" GPIO<BOARD::D11> data; // Setup data.output(); // Manual loop unrolling for speed. Could ask the compiler to do this // Member function write() data.write(commandbits & 0x80); data.write(commandbits & 0x40); ...


1

One method is to use interrupts with a state machine. Here is a minimal example: Master #define pin 10 // connect to slave digital input pin void setup(){ pinMode(pin,OUTPUT); digitalWrite(pin,LOW); } void loop(){ if( // insert your condition to trigger the slave Arduino ){ digitalWrite(pin,HIGH); delay(10); // short delay digitalWrite(pin,LOW); ...


1

The problem you are seeing here is that the Arduino can provide nowhere near as much current through I/O pins as it can power. Quoting from the Arduino website: DC Current per I/O Pin: 40 mA This means that each pin can power maybe a couple of LEDs, and that's it. Additionally, motors provide "back-EMF": When opening (cutting power) an inductive load (e....


Only top voted, non community-wiki answers of a minimum length are eligible