LED not glowing - arduino

I a beginner to IoT. I want to glow LED connected to arduino for this I have made connections as described below, also see the image of breadboard attached here.
LED Connections
Connected
Arduino GND to one leg of LED and 3.3V to another leg of LED.
My Arduino program is-
void setup() {
Serial.begin(9600);
// connect to wifi.
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("connected: ");
Serial.println(WiFi.localIP());
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.set("LED_STATUS",0);
}
int n = 0;
void loop() {
// get value
n = Firebase.getInt("LED_STATIS");
if (n == 1) {
Serial.print("LED IS ON");
digitalWrite(D1,HIGH);
return;
}else{
Serial.print("LED IS OFF");
digitalWrite(D1,LOW);
return;
}
delay(1000);
}
Serial monitor shows LED is ON as shown in screenshot.
Serial Monitor

i think you should declare pinMode in setup, and you put LED in D0
#define LED D0 // Led in NodeMCU at pin GPIO16 (D0).
void setup() {
pinMode(LED, OUTPUT); // LED pin as output.
}
void loop() {
if (n == 1) {
Serial.print("LED IS ON");
digitalWrite(LED ,HIGH);
return;
}else{
Serial.print("LED IS OFF");
digitalWrite(LED ,LOW);
return;
}
}
note that it has built-in LED, but its active low, it should glow when logic is 0
and you should using pull-up resistor, because you can't depend current supply from GPIO pin since its only provide 12mA

Related

Communication entre ARDUINO NANO 33 BLE SENSE?

I am new and I would really need your experience and your help : Here is my project:
I’m trying to make 2 Arduino nano 33 ble sense communicate:I need to send the accelerations and gyro data from the first one and receive them in the second, How i can did ?
The idea is to do 6 Arduino nano 33 ble sense each associate with an electrochemical technology sensor (UART), and send the data tram to the central board which will retrieve all the informations from the differents sensors via BLE on each board.
Then I will associate with the central card (the one which recovers all the informations of the 6 cards) a LORA module to send the data to a PC (I would like to do it in real time).
Thank you .
Thank you for your answer, I managed to communicate two cards at the same time with the examples of arduino nano
-File → Examples → ArduinoBLE → Peripheral → LED
-File → Examples → ArduinoBLE → Central → LEDControl
But for example I haven't yet known how to transfer the information on the temperature sensor from the first Arduino nano 33 ble sense board and read them on the second
nano 33 ble sense.
Why sensor of Temperature ?
Because after I am going to have another sensor that I am going to associate with the nano 33, that's why In first, I need to try with the temperature or humidity sensor of nano 33. Thank you!
you can find an excellent short tutorial on the link below about connecting two Arduino nano using BLE. One acting as peripheral device and the other as central device.
"In this tutorial, we will learn how to exchange information between two Arduino boards, the Nano 33 BLE and the Nano 33 BLE Sense, through Bluetooth® Low Energy. For this, we will be using the ArduinoBLE library."
code for programming the central device
/*
BLE_Central_Device.ino
This program uses the ArduinoBLE library to set-up an Arduino Nano 33 BLE Sense
as a central device and looks for a specified service and characteristic in a
peripheral device. If the specified service and characteristic is found in a
peripheral device, the last detected value of the on-board gesture sensor of
the Nano 33 BLE Sense, the APDS9960, is written in the specified characteristic.
The circuit:
- Arduino Nano 33 BLE Sense.
This example code is in the public domain.
*/
#include <ArduinoBLE.h>
#include <Arduino_APDS9960.h>
const char* deviceServiceUuid = "19b10000-e8f2-537e-4f6c-d104768a1214";
const char* deviceServiceCharacteristicUuid = "19b10001-e8f2-537e-4f6c-d104768a1214";
int gesture = -1;
int oldGestureValue = -1;
void setup() {
Serial.begin(9600);
while (!Serial);
if (!APDS.begin()) {
Serial.println("* Error initializing APDS9960 sensor!");
}
APDS.setGestureSensitivity(80);
if (!BLE.begin()) {
Serial.println("* Starting BLE module failed!");
while (1);
}
BLE.setLocalName("Nano 33 BLE (Central)");
BLE.advertise();
Serial.println("Arduino Nano 33 BLE Sense (Central Device)");
Serial.println(" ");
}
void loop() {
connectToPeripheral();
}
void connectToPeripheral(){
BLEDevice peripheral;
Serial.println("- Discovering peripheral device...");
do
{
BLE.scanForUuid(deviceServiceUuid);
peripheral = BLE.available();
} while (!peripheral);
if (peripheral) {
Serial.println("* Peripheral device found!");
Serial.print("* Device MAC address: ");
Serial.println(peripheral.address());
Serial.print("* Device name: ");
Serial.println(peripheral.localName());
Serial.print("* Advertised service UUID: ");
Serial.println(peripheral.advertisedServiceUuid());
Serial.println(" ");
BLE.stopScan();
controlPeripheral(peripheral);
}
}
void controlPeripheral(BLEDevice peripheral) {
Serial.println("- Connecting to peripheral device...");
if (peripheral.connect()) {
Serial.println("* Connected to peripheral device!");
Serial.println(" ");
} else {
Serial.println("* Connection to peripheral device failed!");
Serial.println(" ");
return;
}
Serial.println("- Discovering peripheral device attributes...");
if (peripheral.discoverAttributes()) {
Serial.println("* Peripheral device attributes discovered!");
Serial.println(" ");
} else {
Serial.println("* Peripheral device attributes discovery failed!");
Serial.println(" ");
peripheral.disconnect();
return;
}
BLECharacteristic gestureCharacteristic = peripheral.characteristic(deviceServiceCharacteristicUuid);
if (!gestureCharacteristic) {
Serial.println("* Peripheral device does not have gesture_type characteristic!");
peripheral.disconnect();
return;
} else if (!gestureCharacteristic.canWrite()) {
Serial.println("* Peripheral does not have a writable gesture_type characteristic!");
peripheral.disconnect();
return;
}
while (peripheral.connected()) {
gesture = gestureDetectection();
if (oldGestureValue != gesture) {
oldGestureValue = gesture;
Serial.print("* Writing value to gesture_type characteristic: ");
Serial.println(gesture);
gestureCharacteristic.writeValue((byte)gesture);
Serial.println("* Writing value to gesture_type characteristic done!");
Serial.println(" ");
}
}
Serial.println("- Peripheral device disconnected!");
}
int gestureDetectection() {
if (APDS.gestureAvailable()) {
gesture = APDS.readGesture();
switch (gesture) {
case GESTURE_UP:
Serial.println("- UP gesture detected");
break;
case GESTURE_DOWN:
Serial.println("- DOWN gesture detected");
break;
case GESTURE_LEFT:
Serial.println("- LEFT gesture detected");
break;
case GESTURE_RIGHT:
Serial.println("- RIGHT gesture detected");
break;
default:
Serial.println("- No gesture detected");
break;
}
}
return gesture;
}
code for programming the peripheral device
/*
BLE_Peripheral.ino
This program uses the ArduinoBLE library to set-up an Arduino Nano 33 BLE
as a peripheral device and specifies a service and a characteristic. Depending
of the value of the specified characteristic, an on-board LED gets on.
The circuit:
- Arduino Nano 33 BLE.
This example code is in the public domain.
*/
#include <ArduinoBLE.h>
enum {
GESTURE_NONE = -1,
GESTURE_UP = 0,
GESTURE_DOWN = 1,
GESTURE_LEFT = 2,
GESTURE_RIGHT = 3
};
const char* deviceServiceUuid = "19b10000-e8f2-537e-4f6c-d104768a1214";
const char* deviceServiceCharacteristicUuid = "19b10001-e8f2-537e-4f6c-d104768a1214";
int gesture = -1;
BLEService gestureService(deviceServiceUuid);
BLEByteCharacteristic gestureCharacteristic(deviceServiceCharacteristicUuid, BLERead | BLEWrite);
void setup() {
Serial.begin(9600);
while (!Serial);
pinMode(LEDR, OUTPUT);
pinMode(LEDG, OUTPUT);
pinMode(LEDB, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LEDR, HIGH);
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, HIGH);
digitalWrite(LED_BUILTIN, LOW);
if (!BLE.begin()) {
Serial.println("- Starting BLE module failed!");
while (1);
}
BLE.setLocalName("Arduino Nano 33 BLE (Peripheral)");
BLE.setAdvertisedService(gestureService);
gestureService.addCharacteristic(gestureCharacteristic);
BLE.addService(gestureService);
gestureCharacteristic.writeValue(-1);
BLE.advertise();
Serial.println("Nano 33 BLE (Peripheral Device)");
Serial.println(" ");
}
void loop() {
BLEDevice central = BLE.central();
Serial.println("- Discovering central device...");
delay(500);
if (central) {
Serial.println("* Connected to central device!");
Serial.print("* Device MAC address: ");
Serial.println(central.address());
Serial.println(" ");
while (central.connected()) {
if (gestureCharacteristic.written()) {
gesture = gestureCharacteristic.value();
writeGesture(gesture);
}
}
Serial.println("* Disconnected to central device!");
}
}
void writeGesture(int gesture) {
Serial.println("- Characteristic <gesture_type> has changed!");
switch (gesture) {
case GESTURE_UP:
Serial.println("* Actual value: UP (red LED on)");
Serial.println(" ");
digitalWrite(LEDR, LOW);
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, HIGH);
digitalWrite(LED_BUILTIN, LOW);
break;
case GESTURE_DOWN:
Serial.println("* Actual value: DOWN (green LED on)");
Serial.println(" ");
digitalWrite(LEDR, HIGH);
digitalWrite(LEDG, LOW);
digitalWrite(LEDB, HIGH);
digitalWrite(LED_BUILTIN, LOW);
break;
case GESTURE_LEFT:
Serial.println("* Actual value: LEFT (blue LED on)");
Serial.println(" ");
digitalWrite(LEDR, HIGH);
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, LOW);
digitalWrite(LED_BUILTIN, LOW);
break;
case GESTURE_RIGHT:
Serial.println("* Actual value: RIGHT (built-in LED on)");
Serial.println(" ");
digitalWrite(LEDR, HIGH);
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, HIGH);
digitalWrite(LED_BUILTIN, HIGH);
break;
default:
digitalWrite(LEDR, HIGH);
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, HIGH);
digitalWrite(LED_BUILTIN, LOW);
break;
}
}
you can find the original tutorial here:
https://docs.arduino.cc/tutorials/nano-33-ble-sense/ble-device-to-device
there is also a really good explanation and how to here:
https://ladvien.com/arduino-nano-33-bluetooth-low-energy-setup/

Arduino sleep while digital pin is HIGH

I want to make the Arduino Pro Mini run on a 3.7v (4.2v when fully charged) LI-Ion battery.
The project is I will use an IR sensor to control the relay. Based on the IR code received, I will toggle the relay digital output pin (HIGH or LOW). Initially, the arduino is set to deep sleep mode and when it receives an External Interrupt (pin 2 on pro-mini), it will process the IR code and switch on the relay.
//Interrupts using Arduino
//Circuit Digest
#include "LowPower.h"
#include <IRremote.h>
volatile int output = LOW;
int i = 0;
#define RECV_PIN 2
volatile boolean sleepEnabled = true;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
irrecv.enableIRIn();
pinMode(RECV_PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(RECV_PIN), buttonPressed1, RISING); // function for creating external interrupts at pin2 on Rising (LOW to HIGH)
}
void loop()
{
Serial.println(i);
++i;
delay(1000);
output = LOW;
digitalWrite(13, output); //Turns LED ON or OFF depending upon output value
if (sleepEnabled == true) {
Serial.println("Going to sleep");
delay(1000);
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
}
delay(500);
readIR();
}
void buttonPressed1() //ISR function excutes when push button at pinD2 is pressed
{
sleepEnabled = false;
}
void readIR() {
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
if (results.value == 0xff4ab5) {
sleepEnabled = true;
}
}
irrecv.resume(); // Receive the next value
}
When the pro-mini goes back to sleep, everything is turned off.
How can I make the pro-mini consume minimal power while the relay pin is HIGH ?

How to connect to the AP in loop() in ESP8266

I have an issue with WiFi.begin() in esp8266-12F.
I'm going to connect the ESP8266 with the specific Access Point in the loop() not in the setup().
I want if a specific AP is available, ESP8266 would connect to it.
In the below code, I supposed to connect to the "abc" AP and turns on an LED and if there is no connection, it turns the LED off, but WiFi.begin("abc", "123456789"); is not working.
What I have to do in this case?
setup(){
}
loop(){
if (WiFi.status() != WL_CONNECTED){
WiFi.disconnect();
WiFi.mode(WIFI_STA);
WiFi.begin("abc", "123456789");
digitalWrite(5, HIGH);
} else {
digitalWrite(5, LOW);
}
}
No point in adding WiFi-disconnect() if you're not connected to any AP at the moment. Just connect to the AP on the setup and leave on the loop() the if (WiFi.status() != WL_CONNECTED). The ESP reconnects itself to the AP when available.
setup(){
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
WiFi.setAutoConnect(true);
Serial.print("Connecting to ");
Serial.print(ssid);
int attempt = 0;
while(WiFi.status() != WL_CONNECTED && attempt<150){ //Connecting to Wi-Fi
delay(100);
Serial.print(".");
attempt++;
}
if(WiFi.status() == WL_CONNECTED){
Serial.println("");
Serial.println("WiFi Connected!");
Serial.print("Local IP: ");
Serial.println(WiFi.localIP());
}
if(attempt == 150){
Serial.println("Failed to connect to WiFi...");
}
}
loop(){
if(WiFi.status() != WL_CONNECTED){
digitalWrite(5,HIGH);
}else{
digitalWrite(5,LOW);
}
}
But for the love of good code otimization use a flag to prevent the digitalWrite to happen hundreds of times per second
I would use the standard code for building a WiFi connection in the setup() and just set the led as HIGH/LOW in the loop() according to WiFi.status(). Reconnect should be handled automatically...

Go to link using if condition using Arduino IDE

I'm just trying to get myself here "http://192.168.1.103:30000/?k=23&v=capture" when an if condition meet its requirement.
#include <ESP8266WiFi.h>
// I purposely don't include the ssid and ssid1 here
WiFiServer server(80);
void setup() {
pinMode(1, INPUT);
Serial.begin(115200);
delay(10);
Serial.println();
WiFi.softAP(ssid1, password1);
Serial.println(WiFi.localIP());
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_AP_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Start the server
server.begin();
Serial.println("Server started");
}
void loop() {
String link = "http://192.168.1.103:30000/?k=23&v=capture";
WiFiClient client = server.available();
if (!client) {
return;
}
int var = digitalRead(1);
if (var == HIGH) {
client.print(link);
}
Let's say:
I already run Chrome.
How can that link above be called without even typing it on Chrome? I want to connect to it automatically.
Any method you could teach? I got the feeling this code itself is wrong.
Thanks.
-- EDIT --
NEW CODE FOR UNO
//language c++
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x3F // Scanning address
LiquidCrystal_I2C lcd(I2C_ADDR, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
Servo Servo1;
int servopin = 9;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
// initialize the lcd for 16 chars 2 lines, turn on backlight
lcd.backlight(); // finish with backlight on
lcd.setCursor(3, 0); //Start at character 4 on line 0
lcd.print("WAITING...");
pinMode(12, OUTPUT); // pin LaserLight
pinMode(11, INPUT); // pin LaserDetector
pinMode(10, INPUT); // pin PIR
pinMode(9, OUTPUT); // pin Servo
pinMode(8, OUTPUT); // MCU PIN GPIO2
Servo1.attach(servopin);
}
void loop() {
digitalWrite(12, HIGH);
boolean inputlaser = digitalRead(11);
boolean inputpir = digitalRead(10);
Serial.println(inputlaser);
Serial.println(inputpir);
if (inputlaser < 1) {
digitalWrite(8, HIGH);
lcd.setCursor(0, 0);
lcd.print("camera on");
lcd.setCursor(0, 1);
lcd.print("robber!");
delay(5000);
Servo1.write(180);
} else if (inputpir > 0) {
Servo1.write(180);
lcd.setCursor(0, 0);
lcd.print("robber inside!");
lcd.setCursor(0, 1);
lcd.print("HELP ROBBER!");
delay(500);
} else {
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("standby...");
delay(500);
}
}
NEW CODE FOR MCU
#include <ESP8266WiFi.h>
char server[] = "192.168.1.103";
WiFiClient client;
void setup() {
pinMode(4, INPUT);
digitalWrite(4, LOW);
Serial.begin(115200);
delay(10);
Serial.println();
WiFi.softAP(ssid1, password1);
Serial.println(WiFi.localIP());
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_AP_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
boolean var = digitalRead(4);
if (var == HIGH) {
client.connect(server, 30000);
Serial.println("connected");
// Make your API request:
client.println("GET /?k=23&v=capture");
client.println("Host: 192.168.1.103");
client.println("Connection: close");
client.println();
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
Serial.println(digitalRead(4));
}
First understand a fact that you doesn't need google chrome for requesting a website.
client.println("GET /?k=23&v=capture");
client.println("Host: 192.168.1.103");
What you do in the above line is that you request for /?k=23&v=capture addressed content in the ip address 192.168.1.103, Actully this is what you do when you use a google chrome. On PC you require a google chrome (or any other browser) for web site request because its difficult to request using commands each time (Think of requesting for a single page using a hell of http commands instead of using chrome, Ohh that's mess). So understand chrome isn't needed to access a site.

Receiving SMS using GSM and controlling LED using Arduino

Has someone come up with a solution with the above stated problem?
We are using Arduino Duemilanove and SIM 900 GSM module (http://robokits.co.in/shop/index.php?main_page=product_info&products_id=303)
We've tried to work on the similar problem of lightning LEDs from port 9-12 when we send an sms #aibicidi, where i = 0 or 1, 0 =off, 1=on. E.g. #a1b1c1d1 will switch on all the LEDs.
When we upload the code and run it through serial monitor and enter the #a1b1c1d1 in the serial monitor, we can see all the LEDs lighten up. But if we send the sms with having content "#a1b1c1d1", we don't see any function of LEDs.
It would be great if anyone can give some guidance about the same.
char inchar; //Will hold the incoming character from the Serial Port.
int led1 = 9;
int led2 = 10;
int led3 = 11;
int led4 = 12;
void setup()
{
// prepare the digital output pins
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
//Initialize GSM module serial port for communication.
Serial.begin(9600);
delay(3000); // give time for GSM module to register on network etc.
Serial.println("AT+CMGF=1"); // set SMS mode to text
delay(200);
Serial.println("AT+CNMI=3,3,0,0"); // set module to send SMS data to serial out upon receipt
delay(200);
}
void loop()
{
//If #a1b1c1d1 comes as sms, all LEDs should light up.
if(Serial.available() >0)
{
inchar=Serial.read();
if (inchar=='#')
{
delay(10);
inchar=Serial.read();
//first led
if (inchar=='a')
{
delay(10);
inchar=Serial.read();
if (inchar=='0')
{
digitalWrite(led1, LOW);
}
else if (inchar=='1')
{
digitalWrite(led1, HIGH);
}
delay(10);
//Second led
inchar=Serial.read();
if (inchar=='b')
{
inchar=Serial.read();
if (inchar=='0')
{
digitalWrite(led2, LOW);
}
else if (inchar=='1')
{
digitalWrite(led2, HIGH);
}
delay(10);
// Third led
inchar=Serial.read();
if (inchar=='c')
{
inchar=Serial.read();
if (inchar=='0')
{
digitalWrite(led3, LOW);
}
else if (inchar=='1')
{
digitalWrite(led3, HIGH);
}
delay(10);
//Fourth led
inchar=Serial.read();
if (inchar=='d')
{
delay(10);
inchar=Serial.read();
if (inchar=='0')
{
digitalWrite(led4, LOW);
}
else if (inchar=='1')
{
digitalWrite(led4, HIGH);
}
delay(10);
}
}
Serial.println("AT+CMGD=1,4"); // delete all SMS
}
}
}
}
}
First do not use delay
Serial.begin(9600);
delay(3000); // give time for GSM module to register on network etc.
This is neither necessary nor reliable. Instead of waiting some random time, you can check the network status with AT+CFUN and/or AT+COPS. If the GSM module is already attached to a network when you open the serial connection, it is a waste of time waiting like that. And if is not attached you should wait explicitly for that to happen (polling CFUN/COPS or enabling AT+CREG), otherwise you risk waiting too short time. See the 27.007 specification for more information for those commands.
Second do not use delay
Serial.println("AT+CMGF=1"); // set SMS mode to text
delay(200);
Please do not write code like this. See this answer on why using delay is such a bad idea, and this answer for suggestion to how to handle properly.

Resources