Page 39 - Programming the Photon Getting Started With the Internet of Things
P. 39
analogWrite This function sets the pin to a value between 0 and 255, where 0 is the
same as setting a digital pin to LOW and 255 is the same as setting a digital pin to
HIGH. This sort of function sends a voltage between 0 and 3.3 V. Because this is a
digital board, it uses something called PWM to emulate an analog signal. A good
example would be to dim an LED by adjusting the analog value.
digitalRead This will read the digital value of a pin, which, as we know, can either
be HIGH or LOW. If you connect the pin directly to the 3.3-V pin, then the value
read will be HIGH. If you connect it to GND, then the value read will be LOW. If
the value read was anywhere in between these values, then it will give a reading of
whichever one it is closest to; this is not advisable, as the readings could never be
accurate.
analogRead This will read any analog value from the analog pins on the board. The
values read can be between 0 and 4095, where 0 is LOW and 4095 is HIGH (3.3 V).
These analog pins are from A0 to A5. Typically these pins are used to read sensor
values such as light and temperature.
Now that we know what functions we have for each pin, we can easily change the
function of the pin simply by tapping and holding on the pin number, and the function
select menu will be shown. Have a go and play around some more, flash some LEDs, read
the temperature, drive some motors, or turn some servos.
When you first receive your Photon device, the firmware loaded onto the board is the
default application for the Tinker application. If you have your own firmware loaded and
want to use the Tinker app again, you can simply revert to a factory reset and the default
firmware will load on to the board once again.
The easiest way to reflash your Photon is actually using the Tinker mobile application,
using either iOS or Android devices:
iOS Tap the list button on the upper-left side, then tap the arrow next to your device,
and then tap the Reflash Tinker button that appears in the pop-out menu.
Android With your selected Photon, tap the options button that appears in the upper-
right side and then tap the Reflash Tinker option in the drop-down box.
Tinker API
When the Tinker firmware is installed on your Photon device, it responds to certain
application programming interface (API) requests from the Tinker app on your mobile
device, which in turn replicates the four functions of the GPIO pins. These API requests
not only are read using the Tinker app, but you also can make requests from other
applications, so you can quite easily create your very own Web or mobile application
based around the firmware. The following section will guide you through making some