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 ...


5

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 ...


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

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://...


4

As this image from Okdo Page on LED Driving shows, there are two ways to drive an LED from a GPIO output pin: In the Active HIGH case, a HIGH output on the GPIO will turn the LED on since that will source current out of the pin and through the LED into ground. In the Active LOW case, a LOW output on the GPIO will sink current from the +V supply into the pin....


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

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

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 ...


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 ...


3

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.)


3

You have 4 calls to analogRead() in your loop. At about 112 µs per call, that's already taking 448 µs. Add to that the time taken by the floating point calculations, and you are likely to end up with something in the order of 500 µs per loop iteration. This should give you a sampling rate close to 2 kHz, which should allow you to detect the highs and lows of ...


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

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

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....


2

Different boards operate internally at different voltages. This is dictated by the main chip on the board. That main internal operating voltage is also the voltage at which the IO pins operate. Most "old" Arduino boards operate at 5V. This includes, but is not limited to: Arduino UNO Arduino Mini Arduino Leonardo Newer more powerful boards, ...


2

I can confirm that 1023 does indeed give 100% duty cycle. I connected an output of my D1 Mini to my oscilloscope and got a steady voltage. I can also confirm that the same voltage is output for digitalWrite() and analogWrite(). However the output drivers may not be the same. With analogWrite() it may be using a weaker output driver that can't deliver as much ...


2

According to the datasheet there are multiple models. Some already have an input resistor for the led (in the opto-isolator) inside the package, but some don't. For the ones that don't have a resistor inside, you'd need to add one yourself, to limit the current to around 10mA. Some models also have a snubber circuit inside the package. The need for this ...


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(){ ...


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