Rob
Rob

Reputation: 11788

ESP8266 ESP-12F with NodeMCU firmware reboots when using looped GPIO write

I have an ESP-12F module that I flashed with the current NodeMCU dev-branch firmware. The module is powered by a >2A power supply. I use 4 GPIO's to control the driver of a little stepper motor (this is the combo).

I wrote a little Lua script (partially based on the arduino version described here) in ESPlorer to control the motor, and the program does work, the motor turns accordingly, but it reboots the module when I call the function turn with too many steps. The limit is at around 180 steps, sometimes a little bit higher, sometimes a little bit below that number.

I'm really new to programming this kind of modules and I'm also just learning Lua, can anybody imagine what happens here and how I can avoid the reboots? BTW: I also tried supplying external 5 Volts to the driver board, but it did not change anything.

This is my script:

gpio.mode(5, gpio.OUTPUT)
gpio.mode(6, gpio.OUTPUT)
gpio.mode(7, gpio.OUTPUT)
gpio.mode(0, gpio.OUTPUT)

sg = function (n,v) gpio.write(n, (v == 0 and gpio.LOW or gpio.HIGH)) end

stepRight = function ()
    sg(5,0);sg(6,0);sg(7,0);sg(0,1);
    sg(5,0);sg(6,0);sg(7,1);sg(0,1);
    sg(5,0);sg(6,0);sg(7,1);sg(0,0);
    sg(5,0);sg(6,1);sg(7,1);sg(0,0);
    sg(5,0);sg(6,1);sg(7,0);sg(0,0);
    sg(5,1);sg(6,1);sg(7,0);sg(0,0);
    sg(5,1);sg(6,0);sg(7,0);sg(0,0);
    sg(5,1);sg(6,0);sg(7,0);sg(0,1);
    sg(5,0);sg(6,0);sg(7,0);sg(0,0);
end

turn = function (dir, steps)
  if dir == 'right' then
    for i=0,steps,1 do
      stepRight()
    end
  end
end

Here are some details about the module and the firmware:

NodeMCU custom build by frightanic.com
    branch: dev
    commit: c54bc05ba61fe55f0dccc1a1506791ba41f1d31b
    SSL: true
    modules: adc,cjson,crypto,dht,file,gpio,hmc5883l,http,i2c,l3g4200d,mqtt,net,node,ow,pwm,spi,tmr,tsl2561,uart,wifi
 build  built on: 2016-11-21 19:02
 powered by Lua 5.1.4 on SDK 1.5.4.1(39cb9a32)

This is what it looks like when I call the turn function with a too high value:

turn('right',200)

 ets Jan  8 2013,rst cause:2, boot mode:(3,7)

load 0x40100000, len 26144, room 16 
tail 0
chksum 0x95
load 0x3ffe8000, len 2288, room 8 
tail 8
chksum 0xa8
load 0x3ffe88f0, len 8, room 0 
tail 8
chksum 0x66
csum 0x66
����o�r��n|�llll`��r�l�l��

NodeMCU custom build by frightanic.com
    branch: dev
    commit: c54bc05ba61fe55f0dccc1a1506791ba41f1d31b
    SSL: true
    modules: adc,cjson,crypto,dht,file,gpio,hmc5883l,http,i2c,l3g4200d,mqtt,net,node,ow,pwm,spi,tmr,tsl2561,uart,wifi
 build  built on: 2016-11-21 19:02
 powered by Lua 5.1.4 on SDK 1.5.4.1(39cb9a32)
lua: cannot open init.lua
> 

Update: I found a solution that works, but I can't explain why. Maybe someone can shed some light on this?

I thought that I had to approach the problem by finding out when and how the reboot occurs, so I added a little timer delay to the for loop:

for i=0,steps,1 do
  stepRight()
  tmr.delay(10)
end

This does not affect the speed of the motor in any noticable way, but now I can easily crank up the numbers as high as I want ;) I can use turn('right',200000) and the reboot is completely gone, it did not reoccur even once, even if I set the delay to only 1 µs. That's great - but I'd love to know why that helps?

Upvotes: 0

Views: 452

Answers (1)

TerryE
TerryE

Reputation: 10888

You are calling the sg()7,200 times in a single turn function. You have to break your processing up to avoid time-outs. This is just the way that the ESP8266 SDK requires.

Read my FAQ in the documentation for a more detailed discussion.

Upvotes: 0

Related Questions