Sie sind auf Seite 1von 9

American University of Sharjah

COE 410

Lab # 9

Husam Eddin Rahhal 54460


Sharmeen Mir 54227

Date: 24/04/2017
Introduction
Photon has a total of 8 ADC input pins and 2 DAC output pins . These pins can
be connected to sensors such as temperature and humidity . Also , they can be
connected to potentiometers and LEDs and so on. The functions that are used
in photon for analog are analogRead for analog input and analogWrite for
analog output. On photon we register variables and funtions on the Particle
web server to securely access photons and have the ability to control them via
HTTP request . It also enables users to use other platforms that support HTTP
communication. Figure below demonstrates the registration of variables and
functions.

Q1 )

Code :

int temp_sensor = A0; //12 bit analog channel


double temp_value = 0.0; //to store temp value as double

void setup() {
pinMode(temp_sensor,INPUT); //set pin A0 to input
Particle.variable("temperature", temp_value); //register variable temp_value
at Particle server

void loop() {
int analogValueTemp = analogRead(temp_sensor); //read the temp sensor
double voltageTemp = 3300* ((double)analogValueTemp / 4095.0); //convert
from unit to into volt

temp_value = voltageTemp / 10; // voltage/sensor resolution


delay(1000);//temp will be updated every 1 second
}

Screenshot :

From browser :

From mobile :

Q2 )

Code :

int led = D7; //digital port 7

void setup() { //setup function call


pinMode(led, OUTPUT); //set pin to output
Particle.function("led", ledToggle); /*declare a particle function so we can
turn the LED from cloud */

void loop() {

int ledToggle (String command) { //ledToggle accepts on and off commands


and controls D7
if(command=="on") {
digitalWrite(led, HIGH); // set LED to HIGH
return 1;
}

else if (command=="off") {
digitalWrite(led, LOW); //set LED to LOW
return 0;
}

else {
return -1;
}
}

HTML file :

<!DOCTYPE>
<html>
<body>
<center>
<br>
<br>
<br>
<form
action="https://api.particle.io/v1/devices/3d001f000f47343337373737/led?
access_token=e203b98dca5f2cfe188a89753ef1de26d1e03586"
method="POST">
Tell your device what to do!<br>
<br>
<input type="radio" name="args" value="on">Turn the LED on.
<br>
<input type="radio" name="args" value="off">Turn the LED off.
<br>
<br>
<input type="submit" value="Do it!">
</form>
</center>
</body>
</html>
Screenshots :

LED off :

Led ON :
Q3 )

Code :

int led = DAC; //12-bit digital to analog converter

void setup() {
pinMode(led, OUTPUT); //set pin to output
Particle.function("an_led", ledan); //declare a particle function so we can
control led

void loop() {

int ledan (String command) { //function accepts a String which contains


intensity of LED
int var=command.toInt(); //converting string to int
analogWrite(led, var);
return 1;
}

HTML file :

<!DOCTYPE html>
<html>
<body>

<form
action="https://api.particle.io/v1/devices/3d001f000f47343337373737/an_led?
access_token=e203b98dca5f2cfe188a89753ef1de26d1e03586"
method="POST">
LED intensity value:<br>
<input type="text" name="args" value="4095">
<br><br>
<input type="submit" value="Submit">
</form>

</body>
</html>

Screenshots :
Conclusion

In this lab, we programmed the photon to allow user to take temperature

readings over internet. In addition, we were able to control the internal LED

using on and off commands over the internet. Finally, we controlled an

external LED connected to DAC by sending an int value between 0-4095 over

the internet and the value determined the intensity of LED.

Das könnte Ihnen auch gefallen