-->
Page 1 of 2

Strange gpio behaviour

PostPosted: Mon Dec 08, 2014 3:21 pm
by scoace
Hi

after setting gpio.mode(9,gpio.OUTPUT) and gpio.write(9,gpio.HIGH). A gpio.read(9) sets the gpio output to 0. Can anyone configm this:

> gpio.mode(9,gpio.OUTPUT)
> gpio.write(9,gpio.HIGH)
> print (gpio.read(9))
0

regards

Re: Strange gpio behaviour

PostPosted: Mon Dec 08, 2014 11:52 pm
by zeroday
scoace wrote:Hi

after setting gpio.mode(9,gpio.OUTPUT) and gpio.write(9,gpio.HIGH). A gpio.read(9) sets the gpio output to 0. Can anyone configm this:

> gpio.mode(9,gpio.OUTPUT)
> gpio.write(9,gpio.HIGH)
> print (gpio.read(9))
0

regards


gpio.read() api put the pin into input mode.

gpio.mode() api now has a third para pullup, to set internal pullup of the pin.
gpio.mode(9,gpio.OUTPUT, 1 ) will enable internal pullup r.
default: pullup disabled( float)

Re: Strange gpio behaviour

PostPosted: Tue Dec 09, 2014 1:00 pm
by scoace
zeroday wrote:
scoace wrote:Hi

after setting gpio.mode(9,gpio.OUTPUT) and gpio.write(9,gpio.HIGH). A gpio.read(9) sets the gpio output to 0. Can anyone configm this:

> gpio.mode(9,gpio.OUTPUT)
> gpio.write(9,gpio.HIGH)
> print (gpio.read(9))
0

regards


gpio.read() api put the pin into input mode.

gpio.mode() api now has a third para pullup, to set internal pullup of the pin.
gpio.mode(9,gpio.OUTPUT, 1 ) will enable internal pullup r.
default: pullup disabled( float)


Thanks for the quick answer, but that means I have to keep track of the pinstate by myself.

Regards

Re: Strange gpio behaviour

PostPosted: Wed Dec 10, 2014 1:04 pm
by jtore
I have the same problem. A gpio.read() does pull down in hardware, but returns the right value in software (high/low).
And the gpio.mode(pin,gpio.OUTPUT,1) does nothing to fix that problem.

Will have to use a variable to save the state until gpio.read() is fixed..

This example script found on this site will crash the mcu when performing gpio.read().

Code: Select alls=net.createServer(net.TCP)
 s:listen(80,function(c)
 c:on("receive",function(c,pl)
  for v,i in pairs {8,9} do
   gpio.mode(i,gpio.OUTPUT,1)
   c:send("\ngpio("..i.."):"..gpio.read(i))
   if string.find(pl,"gpio"..i.."=0") then gpio.write(i,0) end
   if string.find(pl,"gpio"..i.."=1") then gpio.write(i,1) end
   c:send("\nnew_gpio("..i.."):"..gpio.read(i))
  end
  c:send("\nTMR:"..tmr.now().." MEM:"..node.heap().."\n")
 c:on("sent",function(c) c:close() end)
 end)
end)


To have this code run I must comment out the lines involving read.

Code: Select all>>  c:on("sent",function(c) c:close() end)
>>  end)
>> end)
> H????19@H?DHJ??@H?
NodeMcu 0.9.2 build 20141209  powered by Lua 5.1.4
lua: cannot open init.lua
>

Output from sending curl "10.0.xx.xx/gpio8=1" to module.