Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
Uses of Nodejs
Next article icon

JavaScript for IoT

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

JavaScript (JS) is the most popular lightweight, interpreted compiled programming language. It can be used for both Client-side as well as Server-side developments. JavaScript is also known as a scripting language for web pages.

However, it has become increasingly popular for building Internet of Things (IoT) applications. IoT refers to the connection of everyday devices to the internet, allowing them to communicate with each other and with us. In this blog, we will explore how to use JavaScript to build IoT applications, including controlling hardware devices such as sensors and actuators, and how to use popular platforms such as Raspberry Pi and Arduino.

What is IoT?

IoT stands for Internet of Things. It refers to the interconnectedness of physical devices, such as appliances and vehicles, that are embedded with software, sensors, and connectivity which enables these objects to connect and exchange data. This technology allows for the collection and sharing of data from a vast network of devices, creating opportunities for more efficient and automated systems.

Why use JavaScript for IoT?

JavaScript is a popular programming language that is widely used for creating web applications. It is lightweight, flexible, and easy to learn. JavaScript also has a wide range of libraries and frameworks that can be used for IoT applications. Furthermore, JavaScript is compatible with many hardware platforms such as Raspberry Pi and Arduino, making it a great choice for IoT development.

Building IoT applications with JavaScript: To build an IoT application with JavaScript, we need to use a hardware platform that supports it. Two popular hardware platforms that support JavaScript are Raspberry Pi and Arduino.

Raspberry Pi: Raspberry Pi is a credit-card-sized computer that can be used for various applications, including IoT. It is equipped with input/output (I/O) pins that allow us to connect various sensors and actuators to it. Raspberry Pi also supports various programming languages, including JavaScript.

To build an IoT application with Raspberry Pi, we need to follow these steps:

  • Set up Raspberry Pi: We need to set up the Raspberry Pi by installing the operating system and connecting it to the internet. We can use a variety of operating systems such as Raspbian, Ubuntu, and many more. Once we have installed the operating system, we need to connect the Raspberry Pi to the internet by connecting it to a Wi-Fi network or using an Ethernet cable.
  • Set up Raspberry Pi: We need to set up the Raspberry Pi by installing the operating system and connecting it to the internet. We can use a variety of operating systems such as Raspbian, Ubuntu, and many more. Once we have installed the operating system, we need to connect the Raspberry Pi to the internet by connecting it to a Wi-Fi network or using an Ethernet cable.
  • Write JavaScript code: We can write JavaScript code to read data from the sensors and control the actuators. For example, to read the temperature values from the sensor, we can use the following code:

Example: This example shows the basic implementation of the above approach.

JavaScript
const sensor = require('ds18b20');  // Replace with your sensor ID const sensorId = '28-00000abcdefg';  sensor.temperature(sensorId, (err, value) => {     if (err) {         console.error(err);         return;     }     console.log(`Temperature: ${value}°C`); }); 

Note: This code uses the ds18b20 library to read the temperature from the sensor with the ID 28-00000abcdefg. We can then use this temperature value to control other devices, such as a fan or a heater.

Arduino: Arduino is an open-source electronics platform that is designed for building IoT applications. It is equipped with input/output (I/O) pins that allow us to connect various sensors and actuators to it. Arduino also supports various programming languages, including JavaScript.

To build an IoT application with Arduino, we need to follow these steps:

  • We need to set up the Arduino by installing the Arduino software and connecting it to the computer using a USB cable. Once we have installed the Arduino software, we can write and upload the code to the Arduino board.
  • Connect sensors and actuators: We need to connect the sensors and actuators to the Arduino using the I/O pins. For example, if we want to connect a light sensor to the Arduino, we can connect the sensor's output pin to one of the Arduino's analog input pins. We can then read the light values using JavaScript.
  • Write JavaScript code: We can write JavaScript code to read data from the sensors and control the actuators. For example, to read the light values from the sensor, we can use the following code:

Example: This example shows the basic implementation of the above approach.

JavaScript
const five = require('johnny-five'); const board = new five.Board();  board.on('ready', function () {     const light = new five.Sensor('A0');     light.on('change', function () {         console.log(`Light value: ${this.value}`);     }); }); 

Note: This code uses the Johnny-five library to read the light value from the sensor connected to the analog input pin A0. We can then use this light value to control other devices, such as a motor or a LED.

Libraries and frameworks for IoT with JavaScript: There are various libraries and frameworks available for building IoT applications with JavaScript. Some popular ones are:

  • Node-RED: Node-RED is a visual programming tool that allows us to create IoT applications using a web browser. It uses a drag-and-drop interface to connect various nodes that represent sensors, actuators, and other components. Node-RED also supports JavaScript, making it easy to write custom code to control the devices.
  • Cylon.js: Cylon.js is a JavaScript framework that provides a simple and elegant way to build IoT applications. It supports various hardware platforms, including Raspberry Pi, Arduino, and many more. Cylon.js also supports various programming languages, including JavaScript, CoffeeScript, and Ruby.
  • Johnny-Five: Johnny-Five is a JavaScript robotics and IoT platform that provides a simple and intuitive way to control hardware devices. It supports various hardware platforms, including Arduino, BeagleBone, and many more. Johnny-Five also supports various programming languages, including JavaScript and CoffeeScript.
  • Noduino: Noduino is an easy Javascript and Node.js framework that allows online applications built with HTML5, Socket.IO, and Node.js to access basic Arduino controls.

Conclusion: In this blog, we have explored how to use JavaScript to build IoT applications, including controlling hardware devices such as sensors and actuators, and how to use popular platforms such as Raspberry Pi and Arduino. JavaScript is a lightweight, flexible, and easy-to-learn programming language that is widely used for creating web applications. Its compatibility with various hardware platforms and availability of libraries and frameworks make it a great choice for IoT development. With the help of libraries such as Node-RED, Cylon.js, and Johnny-Five, we can easily build complex IoT applications with JavaScript.


Next Article
Uses of Nodejs

L

lokeshpotta20
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-Questions

Similar Reads

  • Top 5 Open Source Java Frameworks
    Almost 30 years and still Ruling the software industry, over the years Java has ranked among the top three most popular programming languages in the world with numerous applications, including back-end development projects, big data and machine learning projects, and obviously, web and Android devel
    7 min read
  • JavaScript | A medicine to every problem
    In the world full of different languages it is quite confusing knowing that what to choose and what not to. Most of the times students want to start something but when they search they have a lot of options and a lot of technologies in the market. It is good to have options but sometimes a lot of op
    4 min read
  • Node.js Projects
    Node.js is one of the most popular JavaScript runtime environments widely used in the software industry for projects in different domains like web applications, real-time chat applications, RESTful APIs, microservices, and more due to its high performance, scalability, non-blocking I/O, and many oth
    9 min read
  • Uses of Nodejs
    Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser. Node.js is used to build back-end services like APIs for Web Apps or Mobile Apps. It’s utilized in production by large companies such as PayPal, Uber, Netflix, Walmart, and many others.
    3 min read
  • Node.js vs Java
    Node.js: Node.js a library that is used to create runtime applications. It was initially written by Rayn Dahl for using the JavaScript outside the browser and later on it was managed by Joyent. Node.js is used for both front-end and back-end and developers can build mobile applications as well. With
    3 min read
  • Top Node Development Trends in 2024
    Node, created by Ryan Dahl in 2009, has emerged as the leading cross-platform, open-source JavaScript runtime, widely adopted by both small and large enterprises for building fast and scalable real-time web applications. In this article, we are going to explore the features of Node and learn about s
    6 min read
  • What are the Alternatives of Servlet?
    Pre-requisite - Introduction to Servlet Servlets are the Java programs that run on the Java-enabled web server or application server. They are used to handle the request obtained from the webserver, process the request, produce the response, then send the response back to the webserver. Below are so
    3 min read
  • Node-RED
    Node-RED is a stream-based development tool for visual programming, primarily focused on wiring the Internet of Things (IoT). This programming tool is developed to wire together hardware devices, APIs, and online services in innovative ways. Table of Content How Node-RED workInstalling NODE-RED Top
    2 min read
  • Difference between Applets and Servlets
    Prerequisite: Servlets and Applets AppletsServletsA Java applet is a small application which is written in Java and delivered to users in the form of bytecode.A servlet is a Java programming language class used to extend the capabilities of a server.Applets are executed on client side.Servlets are e
    2 min read
  • Top Programming Languages for Internet of Things
    Years back, we had never imagined that the day will come when we would just stand in front of our door and it could automatically open, the lights would automatically turn on when we enter the room, the AC temperature would be automatically adjusted, we would be testing self-driving cars that can na
    12 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences