Skip to content

Johnny Five, how to use a REPL

New Course Coming Soon:

Get Really Good at Git

This post is part of the Johnny Five series. See the first post here.

When you run a program using Johnny Five, you can see that in the terminal, we have access to a REPL, a term that means Read-Evaluate-Print-Loop.

In other words, we can write commands in here.

Let’s try by creating a repl.js file with this code:

const { Board } = require("johnny-five")
const board = new Board()

I am going to play with the LCD circuit made in the previous lesson.

Run the program with node repl.js:

Next, we’re going to write some commands in the REPL.

Start by requiring the LCD class:

const { LCD } = require("johnny-five")

Then initialize an lcd object from it:

const lcd = new LCD({ pins: [7, 8, 9, 10, 11, 12] })

Now write to the LCD display:

lcd.print("Hello!")

You’ll see a big message coming back:

Because the command returns a reference to the LCD object. This is to let us chain commands together, like this:

lcd.clear().print("Hello!")

If you don’t run clear(), any new thing you write is going to be appended to the one already there.

To write to the second row, you call cursor(1) (the default row is 0:

lcd.clear().print("Hello from")
lcd.cursor(1, 0).print("Johnny-Five!")

Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching Summer 2024. Join the waiting list!
→ Get my JavaScript Beginner's Handbook
→ Read my JavaScript Tutorials on The Valley of Code
→ Read my TypeScript Tutorial on The Valley of Code

Here is how can I help you: