Getting Started with the Particle Photon – Part 2

In the previous post I unpacked the photon and connected it via wifi.  Go to Part 1.  This was done using the particle app, although there are directions to do it over USB if needed.  In this post I will cover the different modes of the pins on the Photon: digitalWrite, digitalRead, analogWrite, analogRead.  This is also covered in their own documentation here.

What we need:

  • Photon (a previously connected one)
  • USB to micro USB cable
  • Power source (computer)
  • Tinker app
  • LED
  • Resistor (330-10k Ohm is fine)
  • TMP 36 temperature sensor
  • Push Button

 

So plug the photon in and you should be able to see it in the particle app now:

Photon Connect

The blank tinker app is shown here.  It looks like it will be great for testing certain setups and prototyping.

Tinker App

Lets look at digitalWrite first.  If you, like me, have done a little work with the Arduino, you will be a little familiar with these different outputs and inputs.  But the concept may still be a little vague, so I will cover it here (make sure to read through the docs as well).

This system has a maximum voltage of 3.3V.  A digitalWrite pin will either be at max voltage or zero.  So it will be either 3.3V or 0V.  It is indicated on the Tinker app by ‘high’ or ‘low’ next to the pin. Basically an on/off switch.

digitalRead is fairly simple as well.  It will tell you if the input is high or low.  This input does need to be between 3.3V and 0V.  If it is somewhere in the middle, like 1.6V, it will round up or down depending on which is closer.

analogWrite is more complicated than either of the digital ones.  Instead of sending an “on” or “off”, you can set it in anywhere in between on a scale from 0 to 255. This is straightforward, but the method by which it does this is not so simple.  It is called Pulse Width Modulation (PWM).  You can read about it here as well.  In a PWM scenario the pin is basically using ‘digitalWrite’, but it is switching on and off very quickly.  When the pin is ‘on’ for 50% of the time and ‘off’ the other 50%, it simulates a pin at half voltage (or 50% duty).

Pins A3 and A6 do not use PWM for analogWrite, however.  These pins output a continuous analog voltage at a value between 0 and 4095.  On the Particle docs it says this is useful for precise mechanisms that need steady voltage.

analogRead will read a value between 0 and 4095.  This is what we will use to read data from sensors. Pins A0 to A7 can do this.

Now its time to do some real stuff.  In the previous post we connected an LED, but now I want to connect an LED with a button.  We will also connect it with analogWrite so that it can dim.  Here is the schematic:

Button LED Schematic

The resistor I used is 330 Ohms.  Also, remember the long side of the LED is positive.  Short is ground.  “If you’re short you’re closer to the ground”.

Now I set D0 to ‘analogWrite’.  Move the slider around and then press the button as much as you desire:

thumb_IMG_0720_1024

It should light up when pressed.  Pretty cool right?  But really simple, time to do something a little more exciting.

Lets measure the temperature:

Screen Shot 2015-09-05 at 10.34.46 AM

Once hooked up like the schematic, turn D0 to ‘high’ using digitalWrite.  Now switch A0 to analogRead.  You should get a reading similar to this:

TMP36 Reading

What it is showing is the value between 0 and 4095.  896 is our value.  We need to find the voltage that corresponds to that reading.  Since our voltage ranges from 0 to 3.3V, and we know the reading ranges from 0 to 4095, we can do a simple proportion: (896/temp_voltage)(4095/3.3V). This gives us a value of .722V being returned to pin A0.  What does that mean?  Well,   After perusing the datasheet for TMP36 I found this:

TMP36 Docs

First off, we should convert to mV so were on the same page: 722.05mV.  Now there is an ‘offset voltage’ for the TMP36, so we  need to subtract 500mV off the top.  Now: 222.05mV.  Then there is 10mV per Celcius.  So dividing 222.05 by 10 we get 22.21 C.  Pretty good!  Also to get Fahrenheit: (22.21 x 1.8) +32 = 72 F.  I’d say thats pretty neat!  Especially considering the fact that this is connected to the WIFI and we could be anywhere in the world and check this temperature!  It makes you think, what else can I do with this?  Well that’s what I’m trying to find out.

Follow me on Twitter for new post updates!

Comment below, let me know how I’m doing.

I’m working on some projects, so check back! It will be exciting.

Here are some links for what I used today:

Fritzing for designing the circuit boards.

GitHub Photon and Core for Fritzing.

TMP36 datasheet.

Check out Part 3!

 

 

 

 

Leave a Comment