GPIO values always default to logic 1, is that normal? - raspberry-pi

I've got a Raspberry Pi Zero W v1.1 set up today and I've been trying GPIO's. Got them (I'll refer to GPIO #3) to output between 3.3v to 0.05v on command using echo 0 > /sys/class/gpio/gpio3/value with direction OUT and got them to read in logic 1 to 0 by connecting pin #7 (GPIO #3) to a ground (will replace with a sensor later on) with direction IN.
My question is: Would you consider this normal that the Raspberry Pi defaults to logic 1 as soon as you enable the export? echo 3 > /sys/class/gpio/export
Can somebody elaborate on why this is? Also, why is this exactly the opposite of this guy's scenario: Reading a sysfs GPIO input pin (direction as "in") always print a fixed value (0)
Kthx

You said there is nothing connected to the pin, yet you read value=1. This is due to internal bias on the input which is weak and can be over-driven by an external circuit if you were to connect something to the pin. The internal bias simply is a large (~1M ohm) resistor to the VCC, so you are reading this bias. Try connecting a 1K ohm resistor from the pin to ground and read it again. It should read value=0 as you were initially expecting.

Related

Understanding SPI CPOL and CPHA

I have been studying about SPI and would like to clarify few more things:
1.
I am trying to understand about CPOL and CPHA in particular:
I have a device (MCP41 that I want to write SPI drivers for). The datasheet can be found here:
https://ww1.microchip.com/downloads/en/devicedoc/11195c.pdf
According to the timing diagram:
It mentions that Data is always latched in on the rising edge of the SCK. Is that supposed to give me any hints about CPHA?
It also mentions that the Data is always clocked out of the SO pin after the falling edge of SCK. Since it mentions falling edge of the SCK, I understand that the CPHA for this device should be set to 1, is that correct?
2 What if I have 2 SPI devices connected to the same SPI bus. One device requires CPOL = 0, CPHA = 0 and the other device requires CPOL = 0 and CPHA = 1. Does that mean it will not be possible to communicate with both devices via single SPI bus?
The timing diagram shows that this device requires SPI mode 0: CPOL=0 CPHA=0.
The way to read the diagram is that the clock idles low, so CPOL=0. The data is sampled (called "latched" above) on the rising edge. This is the first edge when starting low so CPHA=0 (the rising edge would be the second edge if starting high).
The comment about output data on the falling edge simply means that the device outputs the next data starting on the second edge of the preceding pulse, this way it is ready in time for the first edge of the next clock pulse.
For your second question, yes you can mix different clock modes (polarity, phase and speed) on the same bus, as long as you make all the changes when no device is selected, ie: when all chip-selects are high. You may want to even change the mode of the chip select pins momentarily to GPIO while you deconfigure and reconfigure the SPI peripheral (I'm never sure what alt-function pins will output while you are messing with the registers of a disabled peripheral).

raspberry pi gpio or rs232 relay board for simultaneous output bit-map?

I really wanted at first an rs232 8-channel relay board that I could command devices to turn on/off with a command string.
All of the ones I found online have the same deficiency in functionality for me: you can't set a subset of relays at the exact time. I can set relay 1 on, and then relay 7 on afterwards, I can set all 8 relays at once, but there is no command structure to pick out the exact leds I would like to turn on/off.
In the past I have dealt with hardware that had a bit-map of the IO pins, and a bit-map of the states and would apply all 8 settings at once. here are some examples:
to set pins 1 and 7 to on and the other pins off (8-bit binary bit-mapping,) send the following byte: in binary: 0100 0001b
to set pins 1,2,3,6 to on and the other pins off (8-bit binary bit-mapping,) send the following byte: in binary: 0010 0111b
I couldn't find any such device to do this so I thought I could make one with a raspberry pi using a simple 8-channel relay board, something like this:
https://www.amazon.com/SainSmart-101-70-102-8-Channel-Relay-Module/dp/B0057OC5WK
but on a Raspberry Pi, I'm running into the same issue: I don't see a way to set the gpio pins as a block command, only individually setting them in a for loop. I looked all morning and can see things like gpioctl, and mmio, but I can't put it all together to a simple proof of concept program on a testboard, any help would be appreciated.
I would really like a solution in C or scripting,
Thanks,
jleslie

Raspberry Pi - How to measure 2 resistor values with GPIO

Is it possible to measure 3 specific resistor values by using GPIO / Without using a full ADC setup?
I have an alarm sensor that I want to hook up to my GPIO. This sensor has 3 specific resistors value, based on it's state:
1) Normal - 4k7
2) Alarm - 9k3
3) Tamper - infinite.
Due to long lines, I would prefer 12V power on one side.
I would like to be able to detect these states by 2 normal GPIO input pins.
Is that even possible? What would be the schematic needed for this?
Or is the only solution to use (external) ADC's?
I am thinking about a voltage diver with resistors and a 1N4148 diode to clip it to 3v3. But so far my results are unfruitfull.
Thanks.
The problem here is you have three levels to measure. If we had two we could use a simple resistor divider setup to make (say) the 4k7 and 9k3 outputs on the sensor to logic 0 (<=0.8V) or logic 1 (>=1.3V) on a single GPIO pin. We could do this on two GPIO pins if we had two "independent samples" of the sensor output rather than one.
Given the above it is possible to design some simple logic network to do the comparisons but as the other comment mentions you're off into the realms of electronics.
ADC is the simplest way to go if you want to stay in the software domain. The are other SBC devices e.g. ESP8266 which have onboard ADC functionality and built-in Wifi https://esp8266-projects.org/2015/03/internal-adc-esp8266/ or you can hook up an add-on ADC to the Rpi for example https://learn.adafruit.com/raspberry-pi-analog-to-digital-converters
Good luck

Confused regarding how much current PSOC 5LP GPIO pins can drive

I'm confused regarding how much current 5LP GPIO pins can drive; any help is appreciated.
Here's where I get confused:
The CY8CKIT-059's "User LED BLUE" is an LTST-C170TBKT from Lite-On
Inc.
This LED has a DC Forward Current of 20 mA, according to its
data sheet.
On the CY8CKIT-059, this LED is driven by a single pin
P2_1 (via a 820 ohm resister), according to the CY8CKIT-059
schematics.
Cypress AN72382, ยง 2.4 states "In general, [PSOC 5LP]
GPIO pins can source 4 mA."
My confusion: If the PSOC 5LP GPIO pins can only source 4 mA (unless ganged), then how can pin P2_1 drive enough current to light "User LED BLUE" if that LED has a DC Forward Current of 20 mA?
What am I missing here?
I'm sure it is something really obvious.
Thanks in advance,
-Chris
The key distinction is source vs. sink, the pins can source less current than they can sink. Note: The concepts should be considered in terms of conventional current flow (positive to negative.)
Sourcing current involves connecting the I/O pin to VCCIOx (positive). Sinking current involves connecting the I/O pin to GROUND.
When driving a LED we will typically connect the LED to a positive voltage source, then to the I/O pin through a resistor. When we want the LED to turn on, the pin must sink current to ground. The pins can control more current this way.
(Image from CircuitsToday.com)

Raspberry Pi Zero I2C pull up + GY-521

I'm trying to talk via I2C to the gyro sensor MPU6050 which sits on a GY 521 board. But i can't get the device being detect.
I tested another i2c device (EEPROM) and got the device address detected. So i guess it has to be a hardware problem.
As far as i could find out:
RPi has a 1.8k pullup resistor on SDA and SLC already on board
my GY 521 also has also a pullup resistor (2.2k) on each line on board
Could the resistors be the problem? A good resistor value in sum would be around 5k on each line?
(The wiring should be ok, there are a lot of instructions around the net and i've checked it multiple times. I use 5v on the GY521 since it has a voltage converter)
Any help appreciated!
The problem was bad soldering.
For the record:
Using the RPi pullups in combination with the breakout board pullups works for me.