Calling for "open hearts", he urged Americans not to cloister themselves in separate corners.
Before an audience made up mostly of law enforcement officials, Obama noted that the officers on the scene of the attack reacted to the gunfire by shielding civilians.
Sterling will be laid to rest on Friday and Castile's funeral is tomorrow. We disinvest in communities, the president said - leaving them rife with poverty and unemployment, with drugs and guns and unmet mental health needs.
"Too often we judge other groups by their worst examples, but judge ourselves by our best intentions", he said. If people are honest with themselves, he added, "oftentimes there is a presumption that black men are unsafe".
He talked about how, when he was a child growing up in Hawaii, a female neighbour once refused to go in a lift with him. But Americans, I think, have a great advantage: To renew our unity, we only need to remember our values.
Another questioner, Texas Lt. Gov. Dan Patrick, came with a different viewpoint.
Soon after Obama took office, police said, they sensed they wouldn't get the same appreciation as shown by his two predecessors, who seemed to have officers' backs.
Obama was meeting Wednesday with police officers at the White House - the second such session this week.
As much as those words have comforted blacks, they have rankled numerous nation's men and women in blue.
The White House said Obama worked late into the night on his speech, consulting Scripture for inspiration.
After years of delivering emotional pleas for peace at similar memorials, Obama acknowledged his fatigue and the limits of his words.
Obama, who cut short a Europe trip after the Dallas killings, has found himself squarely in the middle of the crisis. "Extreme emergencies shouldn't define the scope of more ordinary situations where police may want to use robots that are capable of harm".
This isn't to indict all police officers or even many, but there are "those". He laid out a series of steps that could help to improve relations between law enforcement and communities, including improving data collection and updating police training practices.
He also echoed Dallas Police Chief David Brown's sentiment that communities are increasingly looking to police to resolve problems instead of adequately funding programs that address them.
"And then we tell the police 'you're a social worker, you're the parent, you're the teacher, you're the drug counselor, '" Obama said. This is the bridge across our nation's deepest divisions.
A day later, he's schooling mayors across the country on community policing and building trust. And in Congress, lawmakers were forming a task force to examine police accountability and aggression toward police.
"Can you imagine being in a room with 2k or more officers and hearing that magnificent song and realizing for one moment...just for one moment...we all were getting along and singing praise?"