Bluetooth dialogue with Arduino via HC-05 modem


Circuit of Arduino breadboard assembly - made with Fritzing

Detailed instructions (Italian text only)

Source code


//////////////////////////////////////////////////////////////////////////////////
// REMIXED BY: CESARE BRIZIO FROM AN ORIGINAL BY TECHBITAR (HAZIM BITAR)
// LICENSE: PUBLIC DOMAIN
// DATE: 21/12/2017

char inByte;
int ledPin = 9; // LED on pin 9
int sensorPin = A0;    // input pin for the photoresistor
float sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  Serial.println("Type 1 to turn the LED on, 0 to turn it off");
}

void loop() {

  // send data only when you receive data:
  if (Serial.available() > 0) {
    // read the incoming byte:
    inByte = Serial.read();
    // say what you got:
    Serial.print("RECEIVED: ");
    Serial.println(inByte);
    if ( inByte == '0' ) digitalWrite(ledPin, LOW); // if it's a 0 (zero) turn LED off
    if ( inByte == '1' ) digitalWrite(ledPin, HIGH); // if it's a 1 (one) turn LED on
    // read the value from the sensor:
    sensorValue = analogRead(sensorPin);
    Serial.print("Photoresistor reading: ");
    Serial.print(sensorValue);
    Serial.print(" equal to ");
    Serial.print((sensorValue * 5) / 1024);
    Serial.println(" V");
  Serial.println("Type 1 to turn the LED on, 0 to turn it off");
}
}