Arduino Uno & Respbarry Pi & Node Mcu Codes
Arduino Uno & Respbarry Pi & Node Mcu Codes
void loop() {
digitalWrite(led, HIGH) // Turn the LED on
}
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output
pinMode(inPin, INPUT); // sets the digital pin 7 as input
}
void loop()
{
val = digitalRead(inPin); // read the input pin
digitalWrite(ledPin, val); // sets the LED to the button's value
}
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
int led1 = 0;
int led2 = 1;
int led3 = 2;
int led4 = 3;
int led5 = 4;
Installation steps
Go to raspberrypi.org
Download -----> Noobs
Insert card to system
Copy and extract noobs into Card
Attached card with raspberry pi
Connect raspberry pi with Pc or HDMI
Configuration start
Installing process
After installation of Linux Operating System
Set some raspberry pi configuration
Set the Desktop text
Go to task bar set resolution ----> DMT 85
Go to interface ---> Enable all interfaces
After enabling the interfaces reboot the System
code explanation
The first line tells the Python interpreter (the thing that runs the
Python code) that it will be using a ‘library’ that will tell it how
to work with the Raspberry Pi’s GPIO pins. A ‘library’ gives a
import RPi.GPIO as GPIO programming language extra commands that can be used to do
something different that it previously did not know how to do.
This is like adding a new channel to your TV so you can watch
something different.
import time Imports the Time library so that we can pause the script later on.
GPIO.setmode(GPIO.BCM)
Each pin on the Raspberry Pi has several different names, so you
need to tell the program which naming convention is to be used.
GPIO.setwarnings(False)
This tells Python not to print GPIO warning messages to the
screen.
This line tells the Python interpreter that pin 18 is going to be
GPIO.setup(18,GPIO.OUT) used for outputting information, which means you are going to
be able to turn the pin ‘on’ and ‘off’.
print "LED on" This line prints some information to the terminal.
This turns the GPIO pin ‘on’. What this actually means is that
GPIO.output(18,GPIO.HIGH) the pin is made to provide power of 3.3volts. This is enough to
turn the LED in our circuit on.
time.sleep(1) Pauses the Python program for 1 second
print "LED off" This line prints some information to the terminal.
GPIO.output(18,GPIO.LOW)
This turns the GPIO pin ‘off’, meaning that the pin is no
longer supplying any power.
code
import RPi.GPIO as GPIO<br>
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.output(17, True)
time.sleep(3)
GPIO.output(17, False)
time.sleep(1)
GPIO.output(18, True)
time.sleep(3)
GPIO.output(18, False)
time.sleep(1)
GPIO.output(22, True)
time.sleep(3)
GPIO.output(22, False)
time.sleep(1)
GPIO.output(23, True)
time.sleep(3)
GPIO.output(23, False)
# Turn LED on
print ("LED on")
GPIO.output(21, GPIO.HIGH)
NODEMCU
https://lastminuteengineers.com/esp8266-nodemcu-arduino-tutorial/
Insight Into ESP8266 NodeMCU
Features & Using It With Arduino
IDE
The Internet of Things (IoT) has been a trending field in the world of technology.
It has changed the way we work. Physical objects and the digital world are
connected now more than ever. Keeping this in mind, Espressif Systems (A
Shanghai-based Semiconductor Company) has released an adorable, bite-sized
WiFi enabled microcontroller – ESP8266, at an unbelievable price! For less than
$3, it can monitor and control things from anywhere in the world – perfect for just
about any IoT project.
ESP-12E Module
The development board equips the ESP-12E module containing ESP8266 chip
having Tensilica Xtensa® 32-bit LX106 RISC microprocessor which operates
at 80 to 160 MHz adjustable clock frequency and supports RTOS.
ESP-12E Chip
Power Requirement
As the operating voltage range of ESP8266 is 3V to 3.6V, the board comes with
a LDO voltage regulator to keep the voltage steady at 3.3V. It can reliably supply
up to 600mA, which should be more than enough when ESP8266 pulls as much
as 80mA during RF transmissions. The output of the regulator is also broken out
to one of the sides of the board and labeled as 3V3. This pin can be used to
supply power to external components.
Power Requirement
Warning:
The ESP8266 requires a 3.3V power supply and 3.3V logic levels for
communication. The GPIO pins are not 5V-tolerant! If you want to interface the
board with 5V (or higher) components, you’ll need to do some level shifting.
I2S interface – I2S interface if you want to add sound to your project.
Multiplexed I/Os
1 ADC channels
2 UART interfaces
4 PWM outputs
SPI, I2C & I2S interface
Serial Communication
The board includes CP2102 USB-to-UART Bridge Controller from Silicon Labs,
which converts USB signal to serial and allows your computer to program and
communicate with the ESP8266 chip.
Serial Communication
CP2102 Driver
Power Pins There are four power pins viz. one VIN pin & three 3.3V pins. The VIN
pin can be used to directly supply the ESP8266 and its peripherals, if you have a
regulated 5V voltage source. The 3.3V pins are the output of an on-board voltage
regulator. These pins can be used to supply power to external components.
GND is a ground pin of ESP8266 NodeMCU development board.
I2C Pins are used to hook up all sorts of I2C sensors and peripherals in your
project. Both I2C Master and I2C Slave are supported. I2C interface functionality
can be realized programmatically, and the clock frequency is 100 kHz at a
maximum. It should be noted that I2C clock frequency should be higher than the
slowest clock frequency of the slave device.
GPIO Pins ESP8266 NodeMCU has 17 GPIO pins which can be assigned to various
functions such as I2C, I2S, UART, PWM, IR Remote Control, LED Light and Button
programmatically. Each digital enabled GPIO can be configured to internal pull-up
or pull-down, or set to high impedance. When configured as an input, it can also
be set to edge-trigger or level-trigger to generate CPU interrupts.
ADC Channel The NodeMCU is embedded with a 10-bit precision SAR ADC. The
two functions can be implemented using ADC viz. Testing power supply voltage
of VDD3P3 pin and testing input voltage of TOUT pin. However, they cannot be
implemented at the same time.
UART Pins ESP8266 NodeMCU has 2 UART interfaces, i.e. UART0 and UART1,
which provide asynchronous communication (RS232 and RS485), and can
communicate at up to 4.5 Mbps. UART0 (TXD0, RXD0, RST0 & CTS0 pins) can be
used for communication. It supports fluid control. However, UART1 (TXD1 pin)
features only data transmit signal so, it is usually used for printing log.
SPI Pins ESP8266 features two SPIs (SPI and HSPI) in slave and master modes.
These SPIs also support the following general-purpose SPI features:
Up to 64-Byte FIFO
WAKE pin – Wake pin is used to wake the chip from deep-sleep.
Fortunately, the amazing ESP8266 community took the IDE selection a step
further by creating an Arduino add-on. If you’re just getting started programming
the ESP8266, this is the environment we recommend beginning with, and the one
we’ll document in this tutorial.
This ESP8266 add-on for Arduino is based on the amazing work by Ivan
Grokhotkov and the rest of the ESP8266 community. Check out the ESP8266
Arduino GitHub repository for more information.
The first thing is having latest Arduino IDE (Arduino 1.6.4 or higher) installed on
your PC. If don’t have it, we recommend upgrading now.
http://arduino.esp8266.com/stable/package_esp8266com_index.json
Hit OK. Then navigate to the Board Manager by going to Tools > Boards > Boards
Manager. There should be a couple new entries in addition to the standard
Arduino boards. Filter your search by typing esp8266. Click on that entry and
select Install.
The board definitions and tools for the ESP8266 include a whole new set of gcc,
g++, and other reasonably large, compiled binaries, so it may take a few minutes
to download and install (the archived file is ~110MB). Once the installation has
completed, a small INSTALLED text will appear next to the entry. You can now
close the Board Manager.
We will use the on-board LED for this test. As mentioned earlier in this tutorial, D0
pin of the board is connected to on-board Blue LED & is user programmable.
Perfect!
Before we get to uploading sketch & playing with LED, we need to make sure that
the board is selected properly in Arduino IDE. Open Arduino IDE and
select NodeMCU 0.9 (ESP-12 Module) option under your Arduino IDE > Tools >
Board menu.
Now, plug your ESP8266 NodeMCU into your computer via micro-B USB cable.
Once the board is plugged in, it should be assigned a unique COM port. On
Windows machines, this will be something like COM#, and on Mac/Linux
computers it will come in the form of /dev/tty.usbserial-XXXXXX. Select this
serial port under the Arduino IDE > Tools > Port menu. Also select the Upload
Speed : 115200
Warning:
More attention needs to be given to selecting board, choosing COM port and
selecting Upload speed. You may get espcomm_upload_mem error while
uploading new sketches, if failed to do so.
void setup()
{
pinMode(D0, OUTPUT);
}
void loop()
{
digitalWrite(D0, HIGH);
delay(500);
digitalWrite(D0, LOW);
delay(500);
}
Once the code is uploaded, LED will start blinking. You may need to tap the RST
button to get your ESP8266 to begin running the sketch.
Introduction
NodeMCU Development kit provides access to these GPIOs of ESP8266. The only thing to
take care is that NodeMCU Dev kit pins are numbered differently than internal GPIO
notations of ESP8266 as shown in below figure and table. For example, the D0 pin on the
NodeMCU Dev kit is mapped to the internal GPIO pin 16 of ESP8266.
NodeMCU DevKit GPIOs
Below table gives NodeMCU Dev Kit IO pins and ESP8266 internal GPIO pins mapping
Pin Names on NodeMCU Development Kit ESP8266 Internal GPIO Pin number
D0 GPIO16
D1 GPIO5
D2 GPIO4
D3 GPIO0
D4 GPIO2
D5 GPIO14
D6 GPIO12
D7 GPIO13
D8 GPIO15
D9/RX GPIO3
D10/TX GPIO1
D11/SD2 GPIO9
D12/SD3 GPIO10
The GPIO’s shown in blue box (1, 3, 9, 10) are mostly not used for GPIO purpose on Dev Kit
ESP8266 is a system on a chip (SoC) design with components like the processor chip. The
processor has around 16 GPIO lines, some of which are used internally to interface with
other components of the SoC, like flash memory.
Since several lines are used internally within the ESP8266 SoC, we have about 11 GPIO pins
remaining for GPIO purpose.
Now again 2 pins out of 11 are generally reserved for RX and TX in order to communicate
with a host PC from which compiled object code is downloaded.
Hence finally, this leaves just 9 general purpose I/O pins i.e. D0 to D8.
As shown in above figure of NodeMCU Dev Kit. We can see RX, TX, SD2, SD3 pins are not
mostly used as GPIOs since they are used for other internal process. But we can try with
SD3 (D12) pin which mostly like to respond for GPIO/PWM/interrupt like functions.
Note that D0/GPIO16 pin can be only used as GPIO read/write, no special functions are
supported on it.
Example
Let’s write an Arduino sketch for LED blinking on pin D4 of NodeMCU Dev Kit.
void setup() {
pinMode(LED_Pin, OUTPUT); // Initialize the LED pin as an output
}
void loop() {
digitalWrite(LED_Pin, LOW); // Turn the LED on