Raspberry Pi 3 with Touchscreen and Camera V2

Still in a bit of shock that the PI can not easily do any analog inputs (analogRead) and instead expects you to use I2C style boards. My problem is I have several photoresistors, flex strips and pressure sensitive resistors for my classroom. I do not need accurate measurements I just need something that works.

Here are my sensors:

I found a previous hack at https://pimylifeup.com/raspberry-pi-light-sensor/ but I have found this easier to understand trick at http://www.raspberrypi-spy.co.uk/2012/08/reading-analogue-sensors-with-one-gpio-pin/ which is the same kind of thing using a capacitor to measure its charge time but I like this one better. The steps are:

  1. Set the GPIO pin as an output and set it Low. This discharges any charge in the capacitor and ensures that both sides of the capacitor are 0V.

  2. Set the GPIO pin as an input. This starts a flow of current through the resistors and through the capacitor to ground. The voltage across the capacitor starts to rise. The time it takes is proportional to the resistance of the LDR.

  3. Monitor the GPIO pin and read its value. Increment a counter while we wait.

  4. At some point the capacitor voltage will increase enough to be considered as a High by the GPIO pin (approx 2v). The time taken is proportional to the light level seen by the LDR.

  5. Set the GPIO pin as an output and repeat the process as required.

I can probably figure out the code, but if anyone has already done it that would be useful. I am presently busy working on: a case, a web interface, and serial communication using I2C, SPI and UART with the PI.

The problem with this attempt is that your signal will get influenced by the input impedance of that circuitry.
The higher the output impedance of your signal source the worse your reading quality will be as it will limit the charging current and slow down your reading.
To avoid that you’ll need some kind of buffer/driver like a voltage follower to reduce to output impedance to charge the cap without the charging current.
And with that extra effort you may well go for an ADC board.

1 Like

Thanks @ScruffR.

That fairly sums it up for me about the Raspberry Pi and Photon:

These are just my opinions, hopefully they don't make anyone angry or have too many mistakes.

The Raspberry Pi is better for Camera, and medium RAM operations (1 GB on board), and storage (16 GB micro SD card came with mine, I assume other micro SD cards work). The PI is possibly able to do some simple on board Machine Learning. It can do (4) PWM outputs, but has no analog Inputs. It has many GPIO pins ( ~ 26 GPIO pins) for digital read and digital write operations.

Both the Photon and the Raspberry PI can do multiple I2C sensor inputs (Thanks @ScruffR max 127 I2C individual addresses ) and other serial communication using I2C, SPI and UART. Note: The Photon can connect a 32 GB micro SD card using Serial SPI communication and purchased separately.

The Photon is easier for any generic IoT sensors, variable resistors, sensor boards with a signal output. The Photon has about (~7) analog input and output pins

For AnalogWrite (PWM) use:

D0, D1, A4, A5, WKP, RX, TX

Or

D0, D1, D2, D3, WKP, RX, TX

For AnaolgRead use A0-A7
Note: The Photon also has 2 DAC analog variable voltage outputs at A6, A7

The photon however has 18 digital pins ( 8 obvious D0-D7, but the others can all be used as Digital Read and Write pins Thanks @ScruffR ).

Basically use whichever is better for your particular project.

Note: that you can always use IFTTT to let a Photon send data to the Raspberry PI using (Presently only tested on two photons using public not private and not MY_DEVICES, the below code would be for a Photon and PI claimed by the same account):

On the Photon:

Particle.publish("my-sensor-amount", "SensorAmount", 60, PRIVATE);

On the PI:

Particle.subscribe("my-sensor-amount", mySensorFunction, MY_DEVICES);

and then write the mySensorFunction on the PI.

The Photon can also work with 32GB SD cards.

I2C allows for 127 individual addresses, so if you can get sensors and other devices with unique addresses, you should be able to communicate with that many.

Actually its A0~A7 (where DAC = A6 and WKP = A7)
And the two DACs are analog voltage outputs.

Actually 18, since all A-pins and RX/TX can also act as digital I/O.

1 Like

Thanks @scruff I made the edits to my blurb.

Success I2C works with only a few headaches.

So trying out an Raspberry PI Serial I2C connection. Works fine on the Photon using a Grove 0.96" OLED display https://www.seeedstudio.com/Grove---OLED-Display-0.96%26quot%3B-p-781.html

using @peekay123 github at https://github.com/pkourany/Adafruit_SSD1306

Strangely not able to compile the library equivalent but manually adding the github .h and .cpp files works on the Photon.

If I then just change the Photon connections to the PI GPIO I2C connections

// black GND
// red 3v3 so do not need the protective resistors
// white SDA D0 on Photon = GPIO 2 on PI
// yellow SCL D1 on Photon = GPIO 3 on PI

** :grimacing: I forgot to activate I2C on the PI Preferences–>configuation–>interfaces**

So I2C worked fine.

A couple of small things with the PI.

I often get an error 500 when trying to flash the code.

I can fix it by adding a space to the .ino file, saving and flashing again. Weird.

With the PI it loses it;s IP so SSH is difficult to work with without the android APP fing which searches your wifi network. Anyone know how to use raspberrypi.local with putty instead of an IP address?

Also in the PI configuration it has settings for
SPI
I2C
SERIAL
ONE-WIRE

I think SERIAL is what I call UART using RX and TX pins

I think one-wire uses GPIO pin 8

Does anyone use one-wire? If so for what?

Quick question. Is there a safe mode for the Raspberry PI that would boot into wifi? I read something about jumper physical pins 5 and 6 but not sure if it will give SSH or WIFI.

I am having a bit of trouble with semi-automatic mode where the pi is bouncing on and off line. I got out of the issue but wonder what to do if you really screw up and get locked up.

So I got the pixy cam Pixy – PixyCam working using SPI with the Raspberry PI. I first test my code using a photon and then use the same code on the PI. I am using the console a lot to test if things are working as that is easier than getting an LED to flash.

    Particle.publish("Pixy", "Object In the middle", 60, PRIVATE);  

The on-board PI green LED (GPIO 19) is a bit of a pain since it is already flashing with internet connectivity.

Keep forgetting two things:

Activate SPI on the PI and Switch devices on the Particle IDE. The console is a huge help as it says if the flash was successful for both the Photon or the PI.

Not having much luck with UART Serial communication but I am using a Finger Print Scanner and those things are finicky on a good day. At least this time I have enabled Serial communication on the PI and have been flashing to the correct micro-processor.

Thank goodness for this command

particle-agent setup

I messed up my PI and it would not flash anymore. The above command fixed things up again.

Hey @ScruffR who is the main particle person working on the particle-agent ?

I am just curious what startup location particle-agent is called from? It does not seem to be in /home/pi/.rcbash or .local or .profile any suggestions where to look.

P.S. For the years I have used Ubuntu it is embarrassing but I have never really like grep. A better question is probably, which “grep” command would I use to find the particle-agent startup location.

The point of all this is, that I would like to be able to shutdown the particle-agent if needed so that it does not get auto called on startup. So then, I can manually call it if needed, without uninstalling it.

I believe @jvanier is.

Does which particle-agent find it?

2 Likes

Kind of: That finds the executable file. So when I run the command

particle-agent restart

it will restart the particle-agent program. But I don't think that is a startup location, I think that is just the location for all the PI users main executable files. I also do not know where the installation folder is for particle-agent?

This site has some explanations Standalone partitioning explained ¡ raspberrypi/noobs Wiki ¡ GitHub

P.S. Great job @jvanier this is an amazing program. To be able to work on the Photon and the PI using the same IDE is really fantastic.

When you say “startup location” do you mean the CWD of particle-agent when it runs your firmware?

If so, the CWD is:

/var/lib/particle/devices/YOUR_DEVICE_ID

I found this out by using this code to make a file:

#include "Particle.h"

void setup() // Put setup code here to run once
{
Process proc = Process::run("echo 'Something' > somefile.txt");
proc.wait();
}

void loop() // Put code here to loop forever
{

}

And then I searched for the file using:

sudo find "/" -name "somefile.txt" -print

Getting:

/var/lib/particle/devices/MY_DEVICE_ID/somefile.txt

A lot of the particle-agent stuff can be found in /usr/share/particle and /var/lib/particle

1 Like

Have you tried using:

export DISPLAY=:0

In your script or via ssh?

1 Like

Found what I was looking for. Tested the start and stop ability for the particle-agent by trying to flash code and they work, but I have not tested the remove from startup functions, but nice to know that I can if the PI starts acting weird.

Test stopping the program

sudo /etc/init.d/particle-agent stop

Test starting the program

sudo /etc/init.d/particle-agent start

If you ever want to remove the script from start-up, run the following command:

sudo update-rc.d -f particle-agent remove

Register script to be run at start-up
To register your script to be run at start-up and shutdown, run the following command:

sudo update-rc.d particle-agent defaults

Note: To view the startup bash file list

ls -la /etc/init.d

To view the particle-agent bash script

nano /etc/init.d/particle-agent

Probably a really good idea not to change it!

I have successfully followed this tutorial to give my pi a static IP address:

You basically edit the network interface file and put in the IP address you want it to have, and your router address.
I hope this helps!

1 Like

@rocksetta, the default hostname is raspberrypi.local and not raspberry.local.

You can change the hostname by doing:

echo "newHostname" | sudo tee /etc/hostname

@ScruffR I am trying to make code that works on both the Photon and the PI, but I need a way to tell which microprocessor I am working with from the .ino script. I can use the version number to identify which processor is loaded and it works on both platforms:

System.version().c_str());

but is there a more sensible method? Something that would return

Photon
Core
Electron
PI2
PI3

and work on all platforms?

You’d check the PLATFORM_ID for that
But AFAIK there is no difference in that between the individual Pi versions.

2 Likes