# How do I connect an analog sensor that receives 5 V power from an external source to a GPIO pin that can receive 3.3 V max?

I'm just getting into Raspberry Pi and breadboarding. I want to make a device that measures hours of direct sunlight. My idea was to have a solar cell as a sensor, hooked up to the Pi. When sunlight strikes the cell, a program in the Pi would count the amount of seconds that the cell is activated.

Most guides on the internet use sensors (such as PIR, or temp) that receive power from the Pi, and solar cells seem to be exclusively used as a power source rather than a measuring device.

Obviously, current in the solar cell is triggered by sunlight, so instead of using the Pi to power the sensor, could I just connect the cell directly to a GPIO pin? I made a (badly drawn) design for clarification.

I know that I will need some kind of ADC (I have an MCP3008 on the way), but I am worried about the voltage of the input signal (green line in the diagram). At maximum capacity, the solar cell makes 5 V and 30 mA. The Pi's GPIO pins can accept a maximum of 3.3 V.

I remembered from physics that in a series circuit voltage decreases across terminals, while in a parallel circuit, current is divided but voltage stays the same. Using this logic, I thought I could decrease the voltage at a specific point in the circuit by adding several resistors in series. However, when I measured the voltage going through a terminal such as an LED, the voltage measured came out the same regardless of the amount of resistors I added inbetween.

I understand this question might seem rudimentary, and I might be in over my head as someone with more experience in software, but I would greatly appreciate some advice.

• research solar tracker Commented Oct 23, 2022 at 23:03

An LED does not make a good circuit element to use for your test.

An LED tends to have a fairly constant voltage in the on state (typically around 2V varying by a small fraction of a volt as current varies). This means the measured voltage won't change much as you change out the resistors.

If your R-PI board has an analog input pin, then the least complex option is to connect to that rather than an external ADC.

To make an analog signal with a 0V to 5V range go from 0V to 3.3V you can use a voltage divider. If you are sampling the output with an ADC, adding a capacitor will reduce measurement noise/error.

simulate this circuit – Schematic created using CircuitLab

The formula for calculating the output voltage in the above schematic would be. $$V_{out} = V_{in} * \frac{R_1}{R_1+R_2}$$

If you only have digital pins available on the R-PI board, then you will probably need to add an external ADC. The MCP3008 that you propose using has a SPI interface that will require 4 digital IO pins (clock, data in, data out, chip select). In addition to the SPI interface you will need a 3.3V power supply and ground connection to the R-PI board.

While you can run the MCP3008 at 5V to directly sample your 5V signal, doing so would mean that you would need level translators on your SPI bus (four of them actually) to change the 3.3V digital signals to 5V, and vice versa. Dividing down the 5V analog signal to 3.3V and then running the MCP3008 at 3.3V will probably be easier in the long run.